2155 Problem Description(floyd算法变型)

  • pqdong 

Problem 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

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.

解题思路:

1.Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法
2.这里我们只需要对每个刚修复的点进行两重循环(图中每两个点之间的距离通过新加入的点来更新)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define INF 1000000000
using namespace std;
int maps[310][310];
int book[310];
int d[310][310];
int main(){
    int n,m,q;
    int t=0;
    while(scanf("%d%d%d",&n,&m,&q)){
        if(n==0&&m==0&&q==0){
            break;
        }
        t++;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j){
                    maps[i][j]=0;
                    d[i][j]=INF;
                }
                else{
                    maps[i][j]=INF;
                    d[i][j]=INF;
                }
            }
        }
        memset(book,0,sizeof(book));
        int x,y,z;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&x,&y,&z);
            if(maps[x][y]>z){//需要特别注意这一点!!!!
                maps[x][y]=z;
            }
        }
        printf("Case %d:\n",t);
        int f,a,b;
        for(int i=0;i<q;i++){
            scanf("%d",&f);
            if(f==0){
                scanf("%d",&a);
                if(book[a]==1){
                    printf("City %d is already recaptured.\n",a);
                }
                else{
                    book[a]=1;
                    for(int j=0;j<n;j++){
                        if(book[j]==1){
                            if(d[a][j]>maps[a][j]){
                                d[a][j]=maps[a][j];
                            }
                            if(d[j][a]>maps[j][a]){
                                d[j][a]=maps[j][a];
                            }
                        }
                    }
                    for(int k=0;k<n;k++){
                        for(int j=0;j<n;j++){
                            d[a][k]=min(d[a][k],d[a][j]+d[j][k]);
                            d[k][a]=min(d[k][a],d[k][j]+d[j][a]);
                        }
                    }
                    for(int k=0;k<n;k++){
                        for(int j=0;j<n;j++){
                            d[a][k]=min(d[a][k],d[a][j]+d[j][k]);
                            d[k][a]=min(d[k][a],d[k][j]+d[j][a]);
                            d[k][j]=min(d[k][j],d[k][a]+d[a][j]);
                            d[j][k]=min(d[j][k],d[j][a]+d[a][k]);
                        }
                    }
                    /*wa了,因为忽略了经过a点更新最小路
                    for(int k=0;k<n;k++){
                        for(int j=0;j<n;j++){
                            d[a][k]=min(d[a][k],d[a][j]+d[j][k]);
                            d[k][a]=min(d[k][a],d[k][j]+d[j][a]);
                        }
                    }
                    /*超时了
                    for(int p=0;p<n;p++){
                        for(int k=0;k<n;k++){
                            for(int j=0;j<n;j++){
                                d[k][j]=min(d[k][j],d[k][p]+d[p][j]);
                            }
                        }
                    }
                    */
                }
            }
            else{
                scanf("%d%d",&a,&b);
                if(book[a]==0||book[b]==0){
                    printf("City %d or %d is not available.\n",a,b);
                }
                else{
                    if(d[a][b]!=INF){
                        printf("%d\n",d[a][b]);
                    }
                    else{
                        printf("No such path.\n");
                    }
                }
            }
        }
        printf("\n");
    }
    return 0;
}

另附上队友的代码,更加简洁明了。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn=310;

int book[maxn];
int mapp[maxn][maxn];
const int inf=10000000;
int n,m,q;


int main() {
    //ios::sync_with_stdio(false);
    int ca=0;
    while(scanf("%d%d%d",&n,&m,&q)) {
        if(n==0&&m==0&&q==0) {
            break;
        }
        int u,v,w;
        memset(book,0,sizeof(book));
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                if(i==j)mapp[i][j]=0;
                else mapp[i][j]=inf;
            }
        }

        for(int i=0; i<m; i++) {
            scanf("%d%d%d",&u,&v,&w);
            if(w<mapp[u][v])
                mapp[u][v]=w;
        }
        ca++;
        printf("Case %d:\n",ca);
        int a,b,c;

        for(int i=0; i<q; i++) {
            scanf("%d",&a);
            if(a==0) {
                scanf("%d",&b);
                if(book[b]==0) {
                    book[b]=1;

                    for(int k=0; k<n; k++) {
                        for(int j=0; j<n; j++) {
                            if(mapp[k][j]>mapp[k][b]+mapp[b][j])
                                mapp[k][j]=mapp[k][b]+mapp[b][j];
                            // cout<<mapp[k][j]<<" ";
                        }
                        // cout<<endl;
                    }
                }


                else {
                    printf("City %d is already recaptured.\n",b);
                }
            }
            else {
                scanf("%d%d",&b,&c);

                if(book[b]==0||book[c]==0)printf("City %d or %d is not available.\n",b,c);
                else if(mapp[b][c]==inf)cout<<"No such path."<<endl;
                else {
                    cout<<mapp[b][c]<<endl;
                }
            }
        }
        cout<<endl;
    }


    return 0;
}
标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注