CSU 1885: 条条大道通罗马 1886: Phone List 1887: Cuckoo Hashing 1888: Optimal Parking 1889: Copying DNA

1885: 条条大道通罗马

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

const int maxn=100005;
const int maxm=400005;
const long long inf=1e20;

int head[maxn];
typedef struct Edge {
	int to;
	int cost;
	int next;
} edge;
edge edges[maxm];
int cnt,inque[maxn];
long long dis[maxn];
int n,m;
void init()
{
	memset(head, -1, sizeof(head));
	memset(inque, 0, sizeof(inque));
	cnt = 0;
}

void add_edge(int from , int to, int cost)
{
	edges[cnt].to = to;
	edges[cnt].cost = cost;
	edges[cnt].next = head[from];
	head[from] = cnt++;
}

queue<int> que;
void spfa()
{
	while(que.size()) que.pop();
	for(int i = 1; i <= n; i++) dis[i] = inf;
	dis[1] = 0,inque[1] = 1;
	que.push(1);
	while(que.size()) {
		int u = que.front();
		que.pop();
		inque[u] = 0;
		for(int i = head[u]; i != -1; i = edges[i].next) {
			if(dis[edges[i].to] > dis[u] + edges[i].cost) {
				dis[edges[i].to] = dis[u] + edges[i].cost;
				if(!inque[edges[i].to]) {
					que.push(edges[i].to);
					inque[edges[i].to] = 1;
				}
			}
		}
	}
	return ;
}

int main(void)
{
        std::ios::sync_with_stdio(false);
	while(cin>>n && n) {
		cin>>m;
		init();
		int u,v,w;
		for(int i = 0; i < m; i++) {
			cin>>u>>v>>w;
			add_edge(u,v,-w);
		}
		spfa();
		cout<<-dis[n]<<endl;
	}
	return 0;
}

1886: Phone List

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
struct P{
    char s[101];
}a[10001];
bool cmp(P a,P b)///排序
{
    if(strcmp(a.s,b.s)>0) return false;
    else return true;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s",&a[i].s);
        sort(a,a+n,cmp);
        int f=0;
        for(int i=0;i<n;i++)
        {
            if(strncmp(a[i].s,a[i+1].s,strlen(a[i].s))==0)
            {f=1; break;}
        }
        if(f) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

1887: Cuckoo Hashing

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
const int maxn = 10000+5;
int h1[maxn],h2[maxn];
int vis[maxn];
int pos[maxn];
int T,m,n;
int dfs(int a)
{
    for(int i = 0;i<2;i++)
    {
       int tmp;
       if(!i)
        tmp = h1[a];
       else
        tmp = h2[a];

        if(!vis[tmp])
        { //cout<<a<<' '<<tmp<<' '<<"pos"<<tmp<<' '<<pos[tmp]<<endl;
            vis[tmp] = 1;
            if(pos[tmp]==-1||dfs(pos[tmp]))
            {
                pos[tmp] = a;
                return 1;

            }
        }
    }
    return 0;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(pos,-1,sizeof(pos));
        for(int i = 0;i <n ;i++)
        {
            scanf("%d%d",&h1[i],&h2[i]);
        }
        int ok = 1;
        for(int i = 0; i< n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(!dfs(i))
            {
               // cout<<i<<endl;
                ok = 0;
                break;
            }
        }
        if(ok)
        {
            puts("successful hashing");
        }
        else
        {
            puts("rehash necessary");
        }
    }
    return 0;
}

1888: Optimal Parking

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int main(){
    int i,j,n,t,max,min,ans,p;
    scanf("%d",&t);
    for(i=1;i<=t;i++){
        ans=max=0;
        min=200;
        scanf("%d",&n);
        for(j=1;j<=n;j++){
            scanf("%d",&p);
            if(p>max)max=p;
            if(p<min)min=p;
        }
        ans=2*(max-min);
        printf("%d\n",ans);
    }
    return 0;
}

1889: Copying DNA

#include <iostream>
#include <cstring>
#include <algorithm>
#include <bitset>

using namespace std;
const int maxm=20;

int memo[1<<maxm],n,m,times,match(int i,int g,char* str,int e),solve(int found);
char s[maxm],sr[maxm],t[maxm];

int main(){
    ios_base::sync_with_stdio(0);
    cin>>times;
    while(times--){
        cin>>s>>t;
        m=strlen(s),n=strlen(t);
        reverse_copy(s,s+m,sr);
        memset(memo,-1,sizeof(int)*(1<<n));
        memo[(1<<n)-1]=0;
        if(solve(0)==0x3f3f3f3f)
            cout<<"impossible\n";
        else
            cout<<solve(0)<<endl;
    }
    return 0;
}

int match(int i, int g, char* str, int e){
    int longest=0;
    for(int k=0,h=0;k<e;++k,h=0){
        while(h<g&&k+h<e&&str[k+h]==t[i+h])++h;
        longest=max(h,longest);
    }
    return longest;
}

int solve(int found){
    if(memo[found]!=-1)
        return memo[found];

    char u[maxm]{},ur[maxm]{};
    int best=0x3f3f3f3f,last_h=0;
    for(int i=0;i<n;++i)
    if(found&(1<<i))u[i]=t[i];
    reverse_copy(u,u+n,ur);
    for(int i=0,g=0,h=0;i<n;++i,g=h=0){
        while(i+g<n&&!(found&(1<<i+g)))++g;
        if(g==0)
            last_h=0;
        else{
            h=max(max(match(i,g,sr,m),match(i,g,s,m)),max(match(i,g,u,n),match(i,g,ur,n)));
            if(h==0)return 0x3f3f3f3f;
            if(h>last_h-1)
                best=min(best,1+solve(found|((1<<i+h)-1)^((1<<i)-1)));
            if(best==0x3f3f3f3f)
                break;
            last_h=h;
        }
    }
    return memo[found]=best;
}

猜你喜欢

转载自blog.csdn.net/nameofcsdn/article/details/80317204
今日推荐