HDU 1546 Idiomatic Phrases Game 求助!help!!!

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


Problem Description
Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends with the two given idioms. For every two adjacent idioms, the last Chinese character of the former idiom should be the same as the first character of the latter one. For each time, Tom has a dictionary that he must pick idioms from and each idiom in the dictionary has a value indicates how long Tom will take to find the next proper idiom in the final list. Now you are asked to write a program to compute the shortest time Tom will take by giving you the idiom dictionary.
 

 

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
 

 

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
*/

 

posted @ 2018-04-28 19:43  一蓑烟雨任生平  阅读(184)  评论(0编辑  收藏  举报