InputThe input consists of several test cases. Each test case contains an idiom dictionary. The dictionary is started by an integer N (0 < N < 1000) in one line. The following is N lines. Each line contains an integer T (the time Tom will take to work out) and an idiom. One idiom consists of several Chinese characters (at least 3) and one Chinese character consists of four hex digit (i.e., 0 to 9 and A to F). Note that the first and last idioms in the dictionary are the source and target idioms in the game. The input ends up with a case that N = 0. Do not process this case.
OutputOne line for each case. Output an integer indicating the shortest time Tome will take. If the list can not be built, please output -1.Sample Input
5 5 12345978ABCD2341 5 23415608ACBD3412 7 34125678AEFD4123 15 23415673ACC34123 4 41235673FBCD2156 2 20 12345678ABCD 30 DCBF5432167D 0
Sample Output
17 -1
这个题的坑点在字符串的长度上
代码:
vector版
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<set> #include<map> #include<vector> #include<cmath> #define Inf 0x3f3f3f3f const int maxn=1e5+5; typedef long long ll; using namespace std; char str[1005][105]; int w[1005]; struct node { int pos; int w; node (int x,int y) { pos=x; w=y; } bool friend operator < (node x,node y) { return x.w>y.w; } }; vector<node>vec[1005]; int dis[1005]; int vis[1005]; int n,m; void init() { for(int t=1;t<=n;t++) { dis[t]=Inf; } memset(vis,0,sizeof(vis)); } void Dijkstra(int s) { priority_queue<node>q; q.push(node(s,0)); dis[s]=0; while(!q.empty()) { node now=q.top(); q.pop(); if(vis[now.pos])continue; vis[now.pos]=1; for(int t=0;t<vec[now.pos].size();t++) { node to=vec[now.pos][t]; if(to.w+dis[now.pos]<dis[to.pos]) { dis[to.pos]=to.w+dis[now.pos]; to.w=dis[to.pos]; q.push(to); } } } } bool ok(int i, int j) { int len=strlen(str[i]); if(str[i][len-1] == str[j][3] && str[i][len - 2] == str[j][2] && str[i][len-3] == str[j][1] && str[i][len-4] == str[j][0]) { return true; } return false; } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) { break; } init(); for(int t=1;t<=n;t++) { vec[t].clear(); } for(int t=1;t<=n;t++) { scanf("%d %s",&w[t],str[t]); } for(int t=1;t<=n;t++) { for(int j=1;j<=n;j++) { if(ok(t,j)) vec[t].push_back(node(j,w[t])); } } Dijkstra(1); if(dis[n]!=Inf) printf("%d ",dis[n]); else { puts("-1"); } } return 0; }
邻接表版
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<set> #include<vector> #include<map> #include<cmath> #define Inf 0x3f3f3f3f const int maxn=1e5+5; typedef long long ll; using namespace std; struct edge { int u,v,w; int next; }Edge[10*maxn]; int w[1005]; char str[1005][105]; struct node { int pos,w; node(int x,int y) { pos=x; w=y; } bool friend operator < (node x,node y) { return x.w>y.w; } }; int head[1005],dis[1005],vis[1005],cnt; void add(int u,int v,int w) { Edge[cnt].u=u; Edge[cnt].v=v; Edge[cnt].w=w; Edge[cnt].next=head[u]; head[u]=cnt++; } void Dijkstra(int s) { dis[s]=0; priority_queue<node>q; q.push(node(s,0)); while(!q.empty()) { node now=q.top(); q.pop(); if(vis[now.pos])continue; vis[now.pos]=1; for(int i=head[now.pos];i!=-1;i=Edge[i].next) { if(dis[now.pos]+Edge[i].w<dis[Edge[i].v]) { dis[Edge[i].v]= dis[now.pos]+Edge[i].w; q.push(node(Edge[i].v,dis[Edge[i].v])); } } } return ; } bool ok(int i, int j) { int len=strlen(str[i]); if(str[i][len-1] == str[j][3] && str[i][len - 2] == str[j][2] && str[i][len-3] == str[j][1] && str[i][len-4] == str[j][0]) { return true; } return false; } int main() { int n,m; while(scanf("%d",&n)!=EOF) { if(n==0) { break; } cnt=0; memset(head,-1,sizeof(head)); memset(dis,Inf,sizeof(dis)); memset(vis,0,sizeof(vis)); for(int t=1;t<=n;t++) { scanf("%d %s",&w[t],str[t]); } for(int t=1;t<=n;t++) { for(int j=1;j<=n;j++) { if(ok(t,j)) add(t,j,w[t]); } } Dijkstra(1); if(dis[n]!=Inf) printf("%d ",dis[n]); else { printf("-1 "); } } return 0; }