//2627641 2011-08-14 23:54:06 Accepted 1952 C 10 324 ylwh@Unknown
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 202
#define MAX 200000000
int n, m, map[N][N], cnt;
char s[N][31];
int find_city(char x[])
{
int i;
for(i=1; i<=cnt; i++)
if(strcmp(x, s[i])==0)
return i;
strcpy(s[++cnt], x);
return cnt;
}
int get_max(int x, int y)
{
return x > y ? x : y;
}
int get_min(int x, int y)
{
return x > y ? y : x;
}
int main()
{
int i, j, k, temp, st, end, now, cases=0;
int d[N], staue[N], Q[N], head, tail;
char s1[31], s2[31];
while(scanf("%d%d", &n, &m) , n)
{
cnt = 0;
cases++;
memset(map, 0,sizeof(map));
getchar();
for(i=0; i<m; i++)
{
scanf("%s%s%d", s1, s2, &temp);
j = find_city(s1);
k = find_city(s2);
map[j][k] = map[k][j] = temp;
}
scanf("%s%s", s1,s2);
st = find_city(s1);
end = find_city(s2);
/*for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
printf("%5d", map[i][j]);
printf("\n");
}
for(i=1; i<=cnt; i++)
puts(s[i]);*/
for(i=1; i<=n; i++)
{
staue[i] = 1;
d[i] = 0;
}
head = 0;
tail = 1;
Q[head] = st;
staue[st] = 0;
d[st] = MAX;
while(head != tail)
{
now = Q[head];
for(i=1; i<=n; i++)
if(map[now][i] && get_max(get_min(d[now], map[now][i]), d[i])!= d[i])
{
d[i] = get_min(d[now], map[now][i]);
if(staue[i])
{
Q[tail++] = i;
staue[i] = 0;
tail %= N;
}
}
head++;
head %= N;
}
printf("Scenario #%d\n%d tons\n\n", cases, d[end]);
}
return 0;
}
ZOJ 1952 Heavy Cargo【SPFA】
最新推荐文章于 2015-03-04 16:59:35 发布