POJ-2263Heavy Cargo(优先队列)

Heavy Cargo

Time Limit: 1000MS      Memory Limit: 65536K
Total Submissions: 4535     Accepted: 2373

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
/*
    学下别人的代码,别光想着用stl。
    关于这种字符串的搜索:要用另外的数组下标来表示。
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 205;
const int rmax = 20000;
typedef struct node
{
    int city;
    int weight;
    bool operator < (const node &a)const
    {
        return weight<a.weight;
    }
}way;
int n,m; // n个城市,m条道路。
int start,end; //开始和结束的城市。
int maze[maxn][maxn];
int vis[maxn][maxn];
char city[maxn][35];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(maze,0,sizeof(maze));
    int k=0,k1,k2;
    char s1[35],s2[35];
    int tons;
    while(m--)
    {
        scanf("%s%s%d",s1,s2,&tons);
        for(k1=0;strcmp(s1,city[k1])&&k1<k;k1++);
        if(k1==k) strcpy(city[k++],s1);
        for(k2=0;strcmp(s2,city[k2])&&k2<k;k2++);
        if(k2==k) strcpy(city[k++],s2);
        maze[k1][k2]=maze[k2][k1]=tons;
    }
    scanf("%s%s",s1,s2);
    for(start=0;strcmp(city[start],s1);start++);
    for(end=0;strcmp(city[end],s2);end++);
    //cout<<start<<" "<<end<<endl;
}
int bfs()
{
    priority_queue<way> qu;
    way cur ={start,rmax};
    qu.push(cur);
    while(!qu.empty())
    {
        cur = qu.top();
        if(cur.city==end) return cur.weight;  //应当在这里判断。
        qu.pop();
        for(int i=0;i<n;i++)
        {
            if(maze[cur.city][i]&&!vis[cur.city][i])
            {
                vis[cur.city][i]=vis[i][cur.city]=1;
                int wi = min(cur.weight,maze[cur.city][i]);
               // if(cur.city==end) return wi;   把最终地点到达判断放到这里是错误的,因为是优先队列,
               // 先到达的,再压回队列时,顺序就不一样了。即出队顺序有误。
                way to = {i,wi};
                qu.push(to);
            }
        }
    }
    return 0;
}
int main()
{
    int t=0;
    while(scanf("%d%d",&n,&m),n||m)
    {
        init();
        int ans = bfs();
        printf("Scenario #%d\n",++t);
        printf("%d tons\n\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值