您的位置:首页 > 编程语言 > Go语言

poj_2263 Heavy Cargo (Dijkstra)

2012-08-09 16:23 363 查看
Heavy Cargo

Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 2264
Accepted: 1252
题目链接:http://poj.org/problem?id=2263
Description
Big Johnsson Trucks Inc. is acompany specialized in manufacturing big trucks. Their latest model, theGodzilla V12, is so big that the amount of cargo you can transport with it isnever limited by the truck itself. It is only limited by the weight
restrictionsthat apply for the roads along the path you want to drive.

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

Input
The input will contain one ormore 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 thetwo cities connected by the segment and giving the weight limit for trucks thatuse this segment. Names are not longer than 30 characters and do not containwhite-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, printthree 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
Source
UlmLocal 1998
解题思路:

这道题目是求两点之间的路径的最小子路径中是最大值,根据输入可以将其转换成图,在用Dijkstra算法来算出单元最短路径,如果该条路径的最小值大于另外一条路径到该点的最小值,那么取两个最小值中的最大值,这样就可以求出起点到任意一点的值

代码:



#include <iostream>
#include<cstdio>
#include<cstring>
#define maxv 203
#define maxe 20000
#define VALUE 0xffffff
using namespace std;

struct Trie
{
    char str[21];
    int id;
};

Trie trie[maxv];
int g[maxv][maxv];
int visited[maxe];
int d[maxv];//源点到任意点的最短距离
int minValue[maxv];//源点到任意点最短距离的最小值
int n,m;
int num;//字典个数
//查找某个单词的id
int getTrieId(char *str)
{
    for(int i=0;i<num;i++)
    {
        if(strcmp(trie[i].str,str)==0)
            return trie[i].id;
    }    
    strcpy(trie[num].str,str);
    trie[num].id=num;
	num++;
    return num-1;
}

int maxVal(int a,int b)
{
	return a>b?a:b;
}

int minVal(int a,int b)
{
	return a<b?a:b;
}

void dijkstra(int s,int e)
{
	int i;
	for(i=0;i<=n;i++)
	{
		visited[i]=0;
		d[i]=VALUE;
		minValue[i]=VALUE;
	}
	d[s]=0;
	while(true)
	{
		int t=-1;
		for(i=0;i<n;i++)
		{
			if(!visited[i] && (t==-1 || d[i]<d[t]))
				t=i;
		}
		if(t==-1)
			break;
		visited[t]=1;

		for(i=0;i<n;i++)
		{
			if((d[i]>=d[t]+g[i][t]) && g[i][t]!=0)
			{
				d[i]=d[t]+g[i][t];
				if(minValue[i]!=VALUE)
				{
					minValue[i]=maxVal(minValue[i],minVal(minValue[t],g[i][t]));
				}
				else
				{
					minValue[i]=minVal(minValue[t],g[i][t]);
				}
			}
			else if(d[i]<d[t]+g[i][t] && g[i][t]!=0)
			{
				minValue[i]=maxVal(minValue[i],minVal(minValue[t],g[i][t]));
			}
			
		}
	} 
}

int main()
{
    char str1[21];
    char str2[21];
    int ton;
    int i;
	int ts=0;
    while(true)
    {
        scanf("%d%d",&n,&m);
        if(!n && !m)
            break;
		num=0;
		ts++;
		memset(g,0,sizeof(0));
        for(i=0;i<m;i++)
        {
            //建图
            cin>>str1>>str2>>ton;
            int id1=getTrieId(str1);
            int id2=getTrieId(str2);
            g[id1][id2]=ton;
            g[id2][id1]=ton;
        }
		cin>>str1>>str2;
		int id1=getTrieId(str1);
        int id2=getTrieId(str2);
		dijkstra(id1,id2);
		printf("Scenario #%d\n",ts);
		printf("%d tons\n\n",minValue[id2]); 

    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: