hdu 3986 Harry Potter and the Final Battle (最短路径)

Harry Potter and the Final Battle

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2118    Accepted Submission(s): 580


Problem Description
The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case.
 
Input
First line, case number t (t<=20). 
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities.
 
Output
Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”.
 
Sample Input
3
4 4
1 2 5
2 4 10
1 3 3
3 4 8
3 2
1 2 5
2 3 10
2 2
1 2 1
1 2 2
 
Sample Output
15 -1 2
 
Author
tender@WHU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3987 3988 3983 3984 3985 
 
 1 //453MS    3616K    2184 B    G++
 2 /*
 3 
 4     题意:
 5         给出n个点,m条边的图,可去掉其中任意一条边,求最坏情况下 点1到点n 的最短路径 
 6 
 7     最短路径:
 8         先一次spfa求出最短路,然后保存路径,保存路径后遍历该路径,从而求解 
 9          
10 */
11 #include<iostream>
12 #include<vector>
13 #include<queue>
14 #define inf 0x7ffffff
15 #define N 1005
16 using namespace std; 
17 struct node{
18     int v,w,id; //id记录其为第几条边 
19     node(int a,int b,int c){
20         v=a;w=b;id=c;
21     }
22 };
23 int d[N],q[N],path[N]; //q[i]记录最短路中第i个点用到的边,path记录最短路径 
24 bool vis[N],pre[50*N]; //pre[i]将第i条边暂时隐去 
25 int n,m;
26 vector<node>V[N];
27 bool flag; //求最短路与遍历最短路的开关 
28 void spfa()
29 {
30     queue<int>Q;
31     memset(vis,false,sizeof(vis));
32     for(int i=0;i<=n;i++) d[i]=inf;
33     d[0]=0;
34     vis[0]=true;
35     Q.push(0);
36     while(!Q.empty()){
37         int u=Q.front();
38         Q.pop();
39         vis[u]=false;
40         int n0=V[u].size();
41         for(int i=0;i<n0;i++){
42             int v=V[u][i].v;
43             int w=V[u][i].w;
44             int id=V[u][i].id;
45             if(pre[id]) continue; //如果遍历到此边跳过 
46             if(d[v]>d[u]+w){
47                 d[v]=d[u]+w;
48                 if(flag){
49                     path[v]=u; q[v]=id;
50                 }
51                 if(!vis[v]){
52                     Q.push(v);
53                     vis[v]=true;
54                 }
55             }
56         }
57     }  
58 }
59 int main(void)
60 {
61     int t;
62     int a,b,c;
63     scanf("%d",&t);
64     while(t--)
65     {
66         scanf("%d%d",&n,&m);
67         for(int i=0;i<=n;i++) V[i].clear();
68         for(int i=0;i<m;i++){
69             scanf("%d%d%d",&a,&b,&c);
70             a--;b--;
71             V[a].push_back(node(b,c,i));
72             V[b].push_back(node(a,c,i));
73         }
74         flag=true;
75         memset(pre,false,sizeof(pre));
76         spfa();
77         flag=false;
78         if(d[n-1]==inf){
79             puts("-1");continue;
80         }
81         int ans=inf;
82         bool tflag=true;
83         for(int i=n-1;i!=0;i=path[i]){
84             pre[q[i]]=true;  //最短路边的开关 
85             spfa();
86             pre[q[i]]=false;
87             if(d[n-1]==inf){
88                 ans=inf;break;
89             }
90             if(tflag){
91                 ans=d[n-1]; tflag=false;
92             }else ans=max(ans,d[n-1]);
93         }
94         if(ans==inf) puts("-1");
95         else printf("%d
",ans);
96     }
97     return 0;
98 }
原文地址:https://www.cnblogs.com/GO-NO-1/p/3431856.html