ZOJ 1952 Heavy Cargo【SPFA】

94 篇文章 0 订阅
8 篇文章 0 订阅
//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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值