• NEFU 663 Emergency 最短路 floyd


    Emergency

    Time Limit 3000ms

    Memory Limit 65536K

    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.
    
    							
    --------------------------------------

    以下摘自郑大神的博客


    题意:很明显,给你一个图,刚开始所有点都不经过,但是随着查询操作,点会一个个开放,然后问你每次查询的时候在当前点能通过的路径的情况下的多源最短路径问题。

    思路:Q很大有100000,那么每次更新操作做多只能O(n^2)了,那么可以考虑用FLOYD。对于每次的更新,每更新与加入点有关的边,就可以更新与这条边有关的最短多源路径了,然后查询x到y的距离的复杂度也就是O(1)了。最高复杂度O(N*N*N);

    这里刚开始我一直WA了,在更新最短路的时候,我是将所有的与加入点有关的边先更新后再从新更新最短路径,其实是不正确的,因为不能保证dis[i][x]或者dis[x][j]是最短的,x是新加入的点。所以必须每次加入一条边的时候就更新与这条边有关的最短路径。


    -----------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int maxn=333;
    const int OO=1e9;
    
    int n,m,Q;
    int a[maxn][maxn];
    int f[maxn][maxn];
    bool v[maxn];
    
    int main()
    {
        int cnt=1;
        while (~scanf("%d%d%d",&n,&m,&Q))
        {
            if (n==0&&m==0&&Q==0) break;
            memset(a,0,sizeof(a));
            memset(f,0,sizeof(f));
            memset(v,0,sizeof(v));
            for (int i=0; i<n; i++)
            {
                for (int j=0; j<n; j++)
                {
                    if (i==j) a[i][j]=0;
                    else a[i][j]=OO;
                    f[i][j]=a[i][j];
                }
            }
            for (int i=1; i<=m; i++)
            {
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                if (z<a[x][y])
                {
                    a[x][y]=z;
                }
            }
            printf("Case %d:\n",cnt++);
            while (Q--)
            {
                int tp,x,y;
                scanf("%d",&tp);
                if (tp==0)
                {
                    scanf("%d",&x);
                    if (v[x])
                    {
                        printf("City %d is already recaptured.\n",x);
                        continue;
                    }
                    v[x]=true;
                    //
                    for (int i=0; i<n; i++)
                    {
                        if (v[i])
                        {
                            f[x][i]=min( f[x][i], a[x][i] );
                            for (int j=0; j<n; j++)
                            {
                                if (v[j])
                                {
                                    f[x][j]=min( f[x][j], f[x][i]+f[i][j] );
                                }
                            }
                        }
                    }
                    //
                    for (int i=0; i<n; i++)
                    {
                        if (v[i])
                        {
                            f[i][x]=min( f[i][x], a[i][x] );
                            for (int j=0; j<n; j++)
                            {
                                if (v[j])
                                {
                                    f[j][x]=min(f[j][x], f[j][i]+f[i][x]);
                                }
                            }
                        }
                    }
                    //
                    for (int i=0; i<n; i++)
                    {
                        if (v[i])
                        {
                            for (int j=0; j<n; j++)
                            {
                                if (v[j])
                                {
                                    f[i][j]=min(f[i][j], f[i][x]+f[x][j]);
                                }
                            }
                        }
                    }
                }
                else if (tp==1)
                {
                    scanf("%d%d",&x,&y);
                    if (v[x]&&v[y])
                    {
                        if (f[x][y]<OO)
                        {
                            printf("%d\n",f[x][y]);
                        }
                        else
                        {
                            printf("No such path.\n");
                        }
                    }
                    else
                    {
                        printf("City %d or %d is not available.\n",x,y);
                    }
                }
            }
            printf("\n");
        }
        return 0;
    }
    




  • 相关阅读:
    CentOS7配置本地yum源和在线yum源
    Centos7中安装samba服务器
    phpmydmain登录问题
    java实现简单的加法器
    我的偶像 凯文 米特尼克 简介
    安全好的地方分享
    a标签
    Vmware虚拟机 的工作模式
    java面板
    java的套接字实现远程连接
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226339.html
Copyright © 2020-2023  润新知