http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2930
题意:所有点到Z点的最短距离。因为岛名由大小写字母组成,所以岛最多有52。
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define N 1000001
using namespace std;
int n;
int map[60][60];
void init()
{
for(int i=0; i<=52; i++)
{
for(int j=0; j<=52; j++)
{
map[i][j]=N;
map[j][i]=N;
}
map[i][i]=0;
}
}
void floy()
{
for(int k=0; k<=52; k++)
{
for(int i=0; i<=52; i++)
{
for(int j=0; j<=52; j++)
{
if(map[i][j]>map[i][k]+map[k][j])
{
map[i][j]=map[i][k]+map[k][j];
}
}
}
}
}
int main()
{
int xx,yy,zz;
char x[100],y[100];
while(scanf("%d",&n)!=EOF)
{
init();
while(n--)
{
scanf("%*c%s%s%d",x,y,&zz);//注意本题的坑
if(x[0]>='A'&&x[0]<='Z')
{
xx=x[0]-'A';
}
else xx =x[0]-'a'+26;
if(y[0]>='A'&&y[0]<='Z')
{
yy=y[0]-'A';
}
else yy=y[0]-'a'+26;
if(map[xx][yy]>zz)
{
map[xx][yy]=zz;
map[yy][xx]=zz;
}
}
floy();
int k=0,min=N;
for(int i=0; i<25; i++)
{
if(map[i][25]<min)
{
min=map[i][25];
k=i;
}
}
printf("%c %d\n",'A'+k,min);
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.