10年山东省赛-E-最短路

题目连接:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2155&cid=1430

题意:输入一个n个节点,m条边的图,然后k条记录,纪录可能为:

0 x:添加上x这个节点;

1 x y :输出从x到y的最短路;

思路:floyd;

使我更加了解了floyd的思想,就是每加入一个点,更新一次最短路。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define INF 1<<29
 4 #define repu(i,a,b) for(int i=a;i<b;i++)
 5 #define N 500
 6 int vis[N],p[N][N];
 7 int main()
 8 {
 9     int n,m,k,kase = 1;
10     while(scanf("%d%d%d",&m,&n,&k)&&m&&n&&k)
11     {
12         printf("Case %d:\n",kase++);
13         memset(vis,0,sizeof(vis));
14         int u,v,d,a,b,st,ed;
15         repu(i,0,m)
16         {
17             repu(j,0,m)
18             p[i][j] = INF;
19             p[i][i]=0;///坑啊,一定得记住啊
20         }
21         repu(i,0,n)
22         {
23             scanf("%d%d%d",&u,&v,&d);
24             if(p[u][v] > d)
25                 p[u][v] = d;
26         }
27         while(k--)
28         {
29             scanf("%d",&a);
30             if(a==1)
31             {
32                 scanf("%d%d",&st,&ed);
33                 if(!vis[st] || !vis[ed])
34                     printf("City %d or %d is not available.\n",st,ed);
35                 else
36                 {
37                     if(p[st][ed] != INF)
38                         printf("%d\n",p[st][ed]);
39                     else
40                         printf("No such path.\n");
41                 }
42             }
43             else
44             {
45                 scanf("%d",&b);
46                 if(vis[b])
47                     printf("City %d is already recaptured.\n",b);
48                 else
49                 {
50                     vis[b] = 1;
51                     ///更新所有以b为过路点的最小值
52                     repu(i,0,m)
53                     repu(q,0,m)
54                     p[i][q] = min(p[i][q],p[i][b]+p[b][q]);
55                 }
56             }
57         }
58         printf("\n");
59     }
60     return 0;
61 }
View Code

 

posted @ 2015-04-20 22:02  一麻袋码的玛侬  阅读(83)  评论(0编辑  收藏  举报