HDU 1546 Idiomatic Phrases Game 求助!help!!!
阿新 • • 發佈:2018-04-28
ID win please val div style ast IE fine
Idiomatic Phrases Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4000 Accepted Submission(s):
1363
Input The 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.
Output One 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
Author ZHOU, Ran
Source Zhejiang Provincial Programming Contest 2006
Recommend linle | We have carefully selected several similar problems for you: 1548 1317 1535 3339 1217 思路:很簡單的spfa,但是wa了好多次,而且不知道哪裏錯了,望大佬不吝賜教。
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 1000010 using namespace std; int n,tot; queue<int>que; char s[1010][110]; int dis[MAXN],vis[MAXN],num[MAXN]; int to[MAXN],net[MAXN],cap[MAXN],head[MAXN]; void add(int u,int v,int w){ to[++tot]=v;cap[tot]=w;net[tot]=head[u];head[u]=tot; } int judge(int u,int v){ int len1=strlen(s[u]); int len2=strlen(s[v]); for(int i=0,j=len2-4;i<4,j<=len2-1;i++,j++) if(s[u][j]!=s[v][i]) return false; return true; } void spfa(){ memset(vis,0,sizeof(vis)); memset(dis,0x7f,sizeof(dis)); while(!que.empty()) que.pop(); que.push(1);vis[1]=1;dis[1]=0; while(!que.empty()){ int now=que.front(); que.pop();vis[now]=0; for(int i=head[now];i;i=net[i]) if(dis[to[i]]>dis[now]+cap[i]){ dis[to[i]]=dis[now]+cap[i]; if(!vis[to[i]]){ vis[to[i]]=1; que.push(to[i]); } } } } int main(){ while(scanf("%d",&n)&&n!=0){ for(int i=1;i<=n;i++) cin>>num[i]>>s[i]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(judge(i,j)) add(i,j,num[i]); spfa(); if(dis[n]<2139062143) cout<<dis[n]<<endl; else cout<<"-1"<<endl; tot=0; memset(cap,0,sizeof(cap)); memset(head,0,sizeof(head)); } } /* 5 5 12345978ABCD2341 5 23415608ACBD3412 7 34125678AEFD4123 15 23415673ACC34123 4 41235673FBCD2156 2 20 12345678ABCD 30 DCBF5432167D 5 5 12345978ABCD1234 5 23415608ACBD3412 7 34125678AEFD4123 5 41235673FBCD1234 5 12345978ABCD1234 1 5 123456781234 0 */
HDU 1546 Idiomatic Phrases Game 求助!help!!!