hznu 1439 Emergency(不断加入点,更新最短路)

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<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 from x, end up with y and the length is z. You can assume that 0<z10000.

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

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

b)      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 cityx 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  0到1的权值是1
1 2 1
0 2 3
1 0 2  询问1:求0到2的最短路
0 0    询问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.

想到了要用Floyd做,不过当时不知道如果这个点是一条路线的中间点时该怎么更新这条路的长度。其实Floyd最外面的for循环就是遍历这样的中间点,因为这个点是确定的,所以三个for循环变成了两个for循环。
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<map>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#define ll long long
using namespace std;
int n,m,a,b,c,p;
int x[301][301];
int main(){
    int cnt=0;
    while(scanf("%d%d%d",&n,&m,&p)&&(n!=0||m!=0||p!=0)){
        printf("Case %d:\n", ++cnt);
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j)
                x[i][j]=100000000;
            x[i][i]=0;
        }
        for(int i=0;i<m;++i){
            scanf("%d %d %d", &a, &b, &c);
            if(x[a][b]>c){  //这里要注意,可能输入会多次出现同一条路径
                x[a][b]=c;
        //      x[b][a]=c;
            }
        }
        map<int,int> r;
        for(int i=0;i<p;++i){
            scanf("%d", &a);
            if(a==0){
                scanf("%d", &b);
                r[b]++;
                if(r[b]>1){
                     printf("City %d is already recaptured.\n", b);
                    continue;
                }
                for(int i=0;i<n;++i){
                    for(int j=0;j<n;++j){
                        if(x[i][j]>x[i][b]+x[b][j])
                            x[i][j]=x[i][b]+x[b][j];
                    }
                }
            }
            else{
                scanf("%d %d", &b, &c);
                if(r[b]==0||r[c]==0)
                    printf("City %d or %d is not available.\n", b, c);
                else if(x[b][c]==100000000)
                    printf("No such path.\n");
                else
                    printf("%d\n",x[b][c]);
            }
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值