zoukankan      html  css  js  c++  java
  • Emergency(最短路,floyd)

    Emergency

    【Description】

    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?


    【Input】
    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.

    【Output】
    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.

    【Sample Input】
    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
    【Sample Output】
    Case 1:
    City 0 or 2 is not available.
    3
    No such path.
    City 2 is already recaptured.

    分析:

    这是山东第一届省赛的题(好像)。。更新新加入的点就OK了。

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    
    using namespace std;
    
    #define MAXN 500
    
    const int INF = (1<<29);
    
    int G[MAXN][MAXN], n;
    bool recap[MAXN];
    
    int main(){
        int m, q, i, j, u, v, w, flag, x, y, cnt=0, c;
    
        while(scanf("%d %d %d",&n, &m, &q) == 3){
            cnt++;
            if(n == 0 && m == 0 && q == 0) break;
            printf("Case %d:\n", cnt);
    
            for(i=0; i<n; i++){
                for(j=0; j<n; j++){
                    G[i][j] = INF;
                }
                G[i][i] = 0;
            }
    
            memset(recap, false, sizeof(recap));
    
            for(i=0; i<m; i++){
                scanf("%d %d %d", &u, &v, &w);
                if(G[u][v] > w) G[u][v] = w;
            }
    
            for(c=0; c<q; c++){
                scanf("%d", &flag);
                if(flag){
                    scanf("%d %d", &x, &y);
                    if(!recap[x] || !recap[y]) printf("City %d or %d is not available.\n", x, y);
                    else{
                        if(G[x][y] != INF) printf("%d\n", G[x][y]);
                        else printf("No such path.\n");
                    }
                }
                else{
                    scanf("%d", &x);
                    if(recap[x]){
                        printf("City %d is already recaptured.\n", x);
                    }
                    else{
                        recap[x] = true;
    
                        for(i=0; i<n; i++){
                            //if(recap[i])
                            for(j=0; j<n; j++){
                                //if(recap[j]){
                                    if(G[i][j] > G[i][x] + G[x][j]){
                                        G[i][j] = G[i][x] + G[x][j];
                                    }
                                }
                            }
                        }
                    }
                }
            }
            putchar('\n');
        }
    
        return 0;
    }
  • 相关阅读:
    计算机语言的学习之道
    单麦克风远场语音降噪解决方案
    ESP8266 SPI 开发之软件驱动代码分析
    ESP8266 SPI 开发之软硬基础分析
    蓝牙5.0协议及下载地址
    python中往json中添加文件的方法
    Python isinstance() 函数含义及用法解析
    从一线方案商的角度来看高通QCC3020芯片
    Ubuntu18.04声卡配置问题解决
    python 音频通道分离的源码实现
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3019495.html
Copyright © 2011-2022 走看看