UVA 567 By ACReaper
这道题目,坑死我了,特别是数据输出的时候有句话 The test result line should contain the start country code A right-justified in columns 1 and 2; the string `` to " in columns 3 to 6; 呜呜,害哥格式错误,找了个半天,你妈呀!!!。题目的大意思就是给你一个图,求任意两点之间,要连同所要征服的国家的最少数目,其实这就是最短路问题。由于是任意两点之间,当然采用Floyd-Warshall算法。呜呜。复习下其伪代码:
d[i][j] = 0 (i == j)
d[i][j] = d[i][k] + d[k][j] (i != j)
Floyd-Warshall(){
n = rows(W);
D(0) = W;
for(int k = 1; k <= n; k++){
for(int i = 1; i <=n ;i++){
for(int j = 1; j <= n; j++)
d[i][j] <?= d[i][k] + d[k][j];
}
}
}
#include <stdio.h> #define MAXN 50 #define INF 1 << 25 //#define TEST #define OJ int w[MAXN][MAXN]; int d[MAXN][MAXN]; int main(){ #ifdef TEST freopen("X:in.txt","r",stdin); freopen("X:out567.txt","w",stdout); #endif int n; int testcase = 1; while(1){ bool flag = false; for(int i = 1; i <= 20; i++){ for(int j = 1; j <= 20; j++){ w[i][j] = d[i][j] = i == j?0:INF; } } bool first = true; for(int j = 1; j <= 19; j++){//结点V1---V19 int m,v; if(scanf("%d",&m)== EOF){ flag = true; break; } for(int i = 1; i <= m; i++){//每个结点按顺序的邻接点 scanf("%d",&v); d[j][v] = d[v][j] = w[j][v] =w[v][j] = 1; } } if(flag) break; else{ //Graph-algo-count; for(int k = 1; k <= 20; k++){ for(int i = 1; i <= 20; i++){ for(int j = 1; j <= 20; j++){ d[i][j] = d[i][j] < d[i][k] + d[k][j] ? d[i][j] : d[i][k] + d[k][j] ; } } } printf("Test Set #%d\n",testcase); scanf("%d",&n); for(int i = 1; i <= n ; i++){ int u,v,temp; scanf("%d%d",&u,&v); printf("%2d to %2d: %d\n",u,v,d[temp = u > v?v:u][u + v - temp]); } printf("\n"); testcase++; } } return 0; }