hdu 3631(floyd思想的运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3631

思路:由于只能用标记的点去更新,并且又要求任意两点之间的最短距离,显然floyd是最合适的。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<vector>
 7 using namespace std;
 8 #define MAXN 333
 9 #define inf 1<<30
10 bool mark[MAXN];
11 int map[MAXN][MAXN];
12 int N,M,Q;
13 
14 void Floyd(int x){
15     for(int i=0;i<N;i++){
16         for(int j=0;j<N;j++){
17             if(map[i][x]!=inf&&map[x][j]!=inf&&map[i][j]>map[i][x]+map[x][j]){
18                 map[i][j]=map[i][x]+map[x][j];
19             }
20         }
21     }
22 }
23 
24 
25 int main(){
26     int _case=1,u,v,w,x,y,flag;
27     while(~scanf("%d%d%d",&N,&M,&Q),(N+M+Q)){
28         if(_case>1)puts("");
29         for(int i=0;i<N;i++){
30             map[i][i]=0;
31             for(int j=i+1;j<N;j++){
32                 map[i][j]=map[j][i]=inf;
33             }
34         }
35         for(int i=1;i<=M;i++){
36             scanf("%d%d%d",&u,&v,&w);
37             if(w<map[u][v])map[u][v]=w;
38         }
39         memset(mark,false,sizeof(mark));
40         printf("Case %d:\n",_case++);
41         while(Q--){
42             scanf("%d",&flag);
43             if(flag==0){
44                 scanf("%d",&x);
45                 if(mark[x]){ printf("ERROR! At point %d\n",x);continue; }
46                 mark[x]=true;
47                 Floyd(x);
48             }else {
49                 scanf("%d%d",&x,&y);
50                 if(!mark[x]||!mark[y]){
51                     printf("ERROR! At path %d to %d\n",x,y);
52                 }else if(map[x][y]<inf){
53                     printf("%d\n",map[x][y]);
54                 }else 
55                     puts("No such path");
56             }
57         }
58     }
59     return 0;
60 }
View Code

 

posted @ 2013-05-23 19:56  ihge2k  阅读(464)  评论(0编辑  收藏  举报