山东省第一届ACM大学生程序设计竞赛Emergency【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 integersN (0<N300),M (0<M100000) and Q (0<Q100000), 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 fromx, end up withy and the length isz. You can assume that 0<z10000.
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 cityx is already recaptured (that is,the same 0 x operation appears again), print “Cityx is already recaptured.”
For each operation 1, if city x or y is not recaptured yet, print “Cityx 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.

题意:给定城市数n(0~n-1)和单项连通路m条,有两种指令,输入0时,再输入x,表示占据城市x,如果城市已经被占据,输出

City 2 is already recaptured.
输入1时,再输入x,y,表示求从x到y的最短路径,如果x或者y城市没有被占据,则输出
City x or y is not available.

如果没有找到从x到y的路径,输出“No such path.”,否则输出最短路

思路:开始用dijstra写,后来发现超时,最后才知道是floyed,每次占据一个点时,就将所以以该点为中转点的城市道路全部松弛一遍,询问时,直接输出路径长度即可。


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 330;
#define inf 0x3f3f3f3f
int map[maxn][maxn];
int dis[maxn],book[maxn],citybook[maxn];
int n,m,q,ans;

void floyed(int x)
{
	int j,k;
	for(j = 0; j < n; j ++)
		for(k = 0; k < n; k ++)
		{
			if(map[j][k] > map[j][x] + map[x][k])
				map[j][k] = map[j][x] + map[x][k];
		}
}

int main()
{
	int i,j,t;
	int x,y,w;
	int flag;
	while(scanf("%d%d%d",&n,&m,&q),n!=0||m!=0||q!=0)
	{
		memset(citybook,0,sizeof(citybook));
		for(i = 0;i <= n; i ++)
			for(j = 0;j <= n; j ++)
				if(i == j)
					map[i][j] = 0;
				else
					map[i][j] = inf;
		for(i = 0; i < m; i ++)
		{
			scanf("%d%d%d",&x,&y,&w);
			map[x][y] = w;
		}
		printf("Case %d:\n",++t);
		for(i = 0; i < q; i ++)
		{
			scanf("%d",&flag);
			if(flag == 0)
			{
				scanf("%d",&x);
				if(citybook[x])
					printf("City %d is already recaptured.\n",x);
				else
				{
					citybook[x] = 1;
					floyed(x);
				}
					
			}
			else if(flag == 1)
			{
				scanf("%d%d",&x,&y);
				if(!citybook[x]||!citybook[y])
					printf("City %d or %d is not available.\n",x,y);
				else
				{
					ans = map[x][y];
					if(ans== inf||x>=n||y>=n||x<0||y<0)
						printf("No such path.\n");
					else
						printf("%d\n",ans);
				}
			}
		}
		printf("\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值