Emergency(山东省第一届ACM程序设计真题+Floyd算法变型)

题目描述

Kudo’s real name is not Kudo. Her name is Kudryavka Anatolyevna Strugatskia, and Kudo is only her nickname.

Now, she is facing an emergency in her hometown:

Her mother is developing a new kind of spacecraft. This plan costs enormous energy but finally failed. What’s more, because of the failed project, 

the government doesn’t have enough resource take measure to the rising sea levels caused by global warming, lead to an island flooded by the sea.

Dissatisfied with her mother’s spacecraft and the government, civil war has broken out. 

The foe wants to arrest the spacecraft project’s participants and the “Chief criminal” – Kudo’s mother – Doctor T’s family.

At the beginning of the war, all the cities are occupied by the foe. But as time goes by, the cities recaptured one by one.

To prevent from the foe’s arrest and boost morale, Kudo and some other people have to distract from a city to another. Although they can use some other means to transport, 

the most convenient way is using the inter-city roads. Assuming the city as a node and an inter-city road as an edge, you can treat the map as a weighted directed multigraph. An inter-city road is available if both its endpoint is recaptured.

Here comes the problem.

Given the traffic map, and the recaptured situation, can you tell Kudo what’s the shortest path from one city to another only passing the recaptured cities?

输入

The input consists of several test cases.

The first line of input in each test case contains three integers N (0<N≤300), M (0<M≤100000) and Q (0<Q≤100000), which represents the number of cities, the numbers of inter-city roads and the number of operations.

Each of the next M lines contains three integer x, y and z, represents there is an inter-city road starts from x, end up with y and the length is z. You can assume that 0<z≤10000.

Each of the next Q lines contains the operations with the following format:

a) 0 x – means city x has just been recaptured.

b) 1 x y – means asking the shortest path from x to y only passing the recaptured cities.

The last case is followed by a line containing three zeros.

输出

For each case, print the case number (1, 2 …) first.

For each operation 0, if city x is already recaptured (that is,the same 0 x operation appears again), print “City x is already recaptured.”

For each operation 1, if city x or y is not recaptured yet, print “City x or y is not available.” otherwise if Kudo can go from city x to city y only passing the recaptured cities, 

print the shortest path’s length; otherwise print “No such path.”

Your output format should imitate the sample output. Print a blank line after each test case.

样例输入

3 3 6
0 1 1
1 2 1
0 2 3
1 0 2
0 0
0 2
1 0 2
1 2 0
0 2

0 0 0

样例输出

Case 1:
City 0 or 2 is not available.
3
No such path.
City 2 is already recaptured.

 1 /*
 2 问题
 3 输入顶点数n,路径数m和操作次数q 
 4 如果操作数是0,x如果没有被收回时,输出City %d is already recaptured.
 5 如果操作数是1,x和y如果有一个没有被收回,输出City x or y is not available.
 6 如果存在最短路径输出最短路径,不存在路径输出No such path. 
 7 
 8 解题思路
 9 由于询问次数可能很多和可能重复而且是任意两点间的最短路,所以每次加入一个点时进行一次Floyd,输出相应的结果即可。 
10 */ 
11 #include<cstdio>
12 #include<cstring>
13 const int INF=99999999;
14 int n,m,q;
15 int e[500][500],book[500];
16 void floyd(int x){
17     int i,j;
18     for(i=0;i<n;i++){
19         for(j=0;j<n;j++){
20             if(e[i][j] > e[i][x] + e[x][j]){
21                 e[i][j] = e[i][x] + e[x][j];
22             }
23         }
24     }
25     /*for(i=0;i<n;i++){
26         for(j=0;j<n;j++){
27             printf("%9d",e[i][j]);
28         }
29         printf("
");
30     }*/
31 }
32 int main()
33 {
34     int u,v,w,op,x,y,i,j,t=1;
35     while(scanf("%d%d%d",&n,&m,&q) == 3 && n+m+q != 0){
36         printf("Case %d:
",t++);
37          
38         for(i=0;i<n;i++){
39             for(j=0;j<n;j++){
40                 e[i][j] =  i==j?0:INF;
41             }
42         }
43          
44         while(m--){
45             scanf("%d%d%d",&u,&v,&w);
46             if(e[u][v] > w)
47                 e[u][v] = w;
48         }
49         /*for(i=0;i<n;i++){
50             for(j=0;j<n;j++){
51                 printf("%9d",e[i][j]);
52             }
53             printf("
");
54         }*/
55         memset(book,0,sizeof(book));
56         while(q--){
57             scanf("%d",&op);
58             if(op == 0){
59                 scanf("%d",&x);
60                 if(book[x])
61                     printf("City %d is already recaptured.
",x);
62                 else{
63                     floyd(x);
64                     book[x]=1;
65                 }
66             }
67             else
68             {
69                 scanf("%d%d",&x,&y);
70                 if(book[x] == 0 || book[y] == 0){
71                     printf("City %d or %d is not available.
",x,y);
72                     continue;
73                 }
74                 if(e[x][y] < INF)
75                     printf("%d
",e[x][y]);
76                 else
77                     printf("No such path.
");
78             }
79         }
80         printf("
");
81     }
82     return 0;
83 }
原文地址:https://www.cnblogs.com/wenzhixin/p/8976501.html