hdu 2112 floyd+map容器

HDU Today

TimeLimit: 15000/5000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 23336    Accepted Submission(s): 5597

Problem Description

Input

note：一组数据中地名数不会超过150个。

Output

Sample Input

6

xiashawestlake

xiashastation 60

xiashaShoppingCenterofHangZhou 30

stationwestlake 20

ShoppingCenterofHangZhousupermarket 10

xiashasupermarket 50

supermarketwestlake 10

-1

Sample Output

50

Hint:

Thebest route is:

xiasha->ShoppingCenterofHangZhou->supermarket->westlake

**和**从此还是过上了幸福的生活。

――全剧终――

Author

lgx

Source

ACM程序设计_期末考试（时间已定！！）

AC代码：

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#define INF 0x3f3f3f3f
#define MAX 200
using namespace std;
int vis[MAX],s[MAX],dis[MAX][MAX];
char a[32], b[32];
int c,ans,k,n;
void dij()
{
memset(s,0,sizeof(s));
for(int i = 1; i <= ans; i++)
vis[i] = dis[1][i];
s[1] = 1;
vis[1] = 0;
int min,k;
for(int i = 1; i <= ans; i++)
{
min = INF;
for(int j = 1; j <= ans; j++)
{
if(!s[j]&&vis[j] < min)
{
min = vis[j];
k = j;
}
}
if(min == INF)
break;
s[k] = 1;
for(int j = 1; j <= ans; j++)
{
if(s[j]==0&&vis[j] > vis[k] + dis[k][j])
{
vis[j] = vis[k] + dis[k][j];
}
}
}
}
int main()
{
map<string,int>s;
while(~scanf("%d",&n))
{
if(n==-1)
break;
k = 0, ans = 3;
s.clear();
scanf("%s%s",a,b);
if(!strcmp(a,b))
k = 1;
s[a] = 1, s[b] = 2;
for(int i = 1; i <= 150; i++)
{
for(int j = 1; j <= 150; j++)
dis[i][j] = INF;
dis[i][i] = 0;
}
for(int i = 1; i <= n; i++)
{
scanf("%s%s%d",a,b,&c);
if(!s[a]) s[a] = ans++;
if(!s[b]) s[b] = ans++;
if(c < dis[s[a]][s[b]])
dis[s[a]][s[b]] = dis[s[b]][s[a]] = c;
}
if(k == 1)
printf("0\n");
else
{
dij();
if(vis[2]==INF)
printf("-1\n");
else
printf("%d\n",vis[2]);
}
}
return 0;
}


09-07 3874

08-17 521

05-31 145

05-31 158

03-26 443

03-30 471

09-18 1023

08-25 1567

10-08 358

11-13 139