poj2263 Heavy Cargo(dijstra 变形)

Description

Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive. 

Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities. 

Input

The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network. 
Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions. 
The last line of the test case contains two city names: start and destination. 
Input will be terminated by two values of 0 for n and r.

Output

For each test case, print three lines: 
  • a line saying "Scenario #x" where x is the number of the test case 
  • a line saying "y tons" where y is the maximum possible load 
  • a blank line

Sample Input

4 3
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Muenchen
5 5
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Hamburg 220
Hamburg Muenchen 170
Muenchen Karlsruhe
0 0

Sample Output

Scenario #1
80 tons
 
Scenario #2
170 tons
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int map[205][205],inQ[205],node[205],n;
void set()
{
    for(int i=1;i<=n;i++)
    {
        inQ[i]=0; node[i]=0;
        for(int j=1;j<=n;j++)
        map[i][j]=0;
    }
}
int dijkstra(int stra,int end)
{
    int now=stra,nw=stra,max=0,min;
    inQ[stra]=1;
    for(int i=1;i<=n;i++)
    {
        node[i]=map[now][i];
        if(max<node[i])
        {max=node[i];nw=i;}
    }
    inQ[nw]=1; now=nw;
    if(inQ[end]) return node[end];
    for(int k=2;k<=n;k++)
    {
        max=0;
        for(int i=1;i<=n;i++)
        if(inQ[i]==0)
        {
            min=map[now][i]>node[now]?node[now]:map[now][i];
            if(node[i]<min)
            node[i]=min;

            if(max<node[i])
            {
                max=node[i]; nw=i;
            }
        }
        inQ[nw]=1; now=nw;
        if(inQ[end])break;
    }
    return node[end];
}
int main()
{
    int k,m,p,st,en,i,t=1;
    char s[35],end[35],str[205][35];
    while(scanf("%d%d",&n,&m)>0&&n+m!=0)
    {
        set();k=0; m++;
        getchar();
        while(m--)
        {
            scanf("%s%s",s,end);
            if(m!=0)
            scanf("%d",&p);
            for(i=1;i<=k;i++)
            if(strcmp(s,str[i])==0)
            break;
            if(i>k){strcpy(str[++k],s); st=k;}
            else st=i;

            for(i=1;i<=k;i++)
            if(strcmp(end,str[i])==0)
            break;
            if(i>k){strcpy(str[++k],end); en=k;}
            else en=i;

            if(map[st][en]<p&&m!=0)
                map[st][en]=map[en][st]=p;

        }

        printf("Scenario #%d\n",t++);
        printf("%d tons\n\n",dijkstra(st,en));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值