问题 I: Emergency
时间限制: 1 Sec 内存限制: 128 MB
提交: 30 解决: 8
[提交] [状态] [命题人:外部导入]题目描述
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.
本来训练的时候是用dijkstra来写的,不过剩的时间不够了,没写出来。
训练结束后,继续写,感觉自己的思路写出来的有点乱。不是很清晰,于是借鉴网上大佬的思路,orz
大佬就是强Emergency(变形弗洛伊德 Floyd)_hunt_er的博客-优快云博客_变身emergency链接在这里_(:з」∠)_
#include<bits/stdc++.h>
using namespace std;
const int INF=99999999;
int n,m,q;
int e[500][500],book[500];
void floyd(int x)
{
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(e[i][j] > e[i][x] + e[x][j]){
e[i][j] = e[i][x] + e[x][j];
}
}
}
}
int main()
{
int u,v,w,op,x,y,i,j,t=1;
while(scanf("%d%d%d",&n,&m,&q) == 3 && n+m+q != 0){
printf("Case %d:\n",t++);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j)
e[i][j]=0;
else
e[i][j]=INF;
}
}
while(m--){
scanf("%d%d%d",&u,&v,&w);
if(e[u][v] > w)
e[u][v] = w;
}
memset(book,0,sizeof(book));
while(q--)//q个操作
{
scanf("%d",&op);//先分是哪个操作
if(op == 0)
{
scanf("%d",&x);
if(book[x])
printf("City %d is already recaptured.\n",x);
else{
floyd(x);
book[x]=1;
}
}
else
{
scanf("%d%d",&x,&y);
if(book[x] == 0 || book[y] == 0){
printf("City %d or %d is not available.\n",x,y);
continue;
}
if(e[x][y] < INF)
printf("%d\n",e[x][y]);
else
printf("No such path.\n");
}
}
printf("\n");
}
return 0;
}
代码粘贴的,方便回顾来看,如有冒犯,联系删除。(*^▽^*)
这篇博客讨论了一个紧急情况下的最短路径问题。Kudo的家乡由于全球变暖导致海平面上升,城市被洪水淹没,同时因不满政府处理,爆发了内战。在这样的背景下,Kudo需要在被解放的城市间寻找最短路径。博主最初打算使用Dijkstra算法,但由于时间不足,改用了Floyd-Warshall算法的变形。博客提供了输入输出样例,并分享了参考代码,解释了如何通过变形的Floyd-Warshall算法解决这个问题。
431

被折叠的 条评论
为什么被折叠?



