#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9
#define INF 1e8
int distanceMatrix[52][52];
int char2Int(char cha){
if (cha >= 'A' && cha <= 'Z')
return cha - 'A';
else
return cha - 'a' + 26;;
}
int main(){
#if DEBUG
int testCase;
for (testCase = 1; testCase <= TESTCASES; testCase++){
char inputFileName[20] = "input7.txt";
//inputFileName[5] = '1' + (testCase - 1);
freopen(inputFileName, "r", stdin);
printf("\n#%d\n", testCase);
#endif
int from, to;
for (from = 0; from < 52; from++)
for (to = from; to < 52; to++)
if (from == to)
distanceMatrix[from][to] = 0;
else
distanceMatrix[from][to] = distanceMatrix[to][from] = INF;
int numOfPaths;
scanf("%d", &numOfPaths);
char cha;
int path;
for (path = 1; path <= numOfPaths; path++){
char onePasture, anotherPasture;
int distance;
scanf("%c", &cha);
scanf("%c %c %d", &onePasture, &anotherPasture, &distance);
from = char2Int(onePasture);
to = char2Int(anotherPasture);
if (distance < distanceMatrix[from][to])
//相同的两个牧场之间可能有多条路,选最短的
distanceMatrix[from][to] = distanceMatrix[to][from] = distance;
}
int pass;
for (pass = 0; pass < 52; pass++)
for (from = 0; from < 52; from++)
for (to = from + 1; to < 52; to++)
if (distanceMatrix[from][pass] + distanceMatrix[pass][to] < distanceMatrix[from][to])
distanceMatrix[from][to] = distanceMatrix[to][from] = distanceMatrix[from][pass] + distanceMatrix[pass][to];
char result;
int minDistance = INF;
to = 25;
for (from = 0; from < 25; from++)
if (distanceMatrix[to][from] < minDistance){
result = from;
minDistance = distanceMatrix[to][from];
}
printf("%c %d\n", 'A' + result, minDistance);
#if DEBUG
}
#endif
return 0;
}
USACO 2.4 Bessie Come Home (Floyd最短路径)
最新推荐文章于 2020-02-08 18:05:22 发布