UVA - 567 Risk

题意:求图上两点的最短路程,Floyd的模板题,输入的时候是按1-19对应的连接点输入的

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 30;

int d[MAXN][MAXN],N,cas;

void init(){
    memset(d,0x3f3f3f3f,sizeof(d));
    for (int i = 1; i <= 20; i++)
        d[i][i] = 0;
}

bool read(){
    init();
    int n,a;
    if (scanf("%d",&n) == EOF)
        return 0;
    for (int i = 0; i < n; i++){
        scanf("%d",&a);
        d[1][a] = 1;
        d[a][1] = 1;
    }
    for (int i = 2; i <= 19; i++){
        scanf("%d",&n);
        for (int j = 0; j < n; j++){
            scanf("%d",&a);
            d[i][a] = 1;
            d[a][i] = 1;
        }
    }
    return 1;
}

void Floyd(){
    for (int k = 1; k <= 20; k++)
        for (int i = 1; i <= 20; i++)
            for (int j = 1; j <= 20; j++)
                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}

void solve(){
    printf("Test Set #%d\n",cas++);
    scanf("%d",&N);
    int u,v;
    while (N--){
        scanf("%d%d",&u,&v);
        printf("%2d to %2d: %d\n",u,v,d[u][v]);
    }
    printf("\n");
}

int main(){
    cas = 1;
    while (read()){
        Floyd();
        solve();
    }
}



你可能感兴趣的:(UVA - 567 Risk)