POJ2263 Heavy Cargo用最短路得到最大流

Heavy Cargo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4380 Accepted: 2288

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

这道理简洁明了,给定n个城市r条路,这些路是双向的,最后给出两个城市,要求求从A城市到B城市可以用最大吨位的卡车是多少。其实感觉是个网络流的问题,不过不用网络流也行,直接改一改dijkstra,改成最长路再改一改松弛操作,就出来了。运算量也不大,因为最多才200个点,dijkstra的复杂度是n²。我这用的时间是172ms,不知道是不是他的数据太多了。如果是网络流的话,复杂度是n*m,或许就会爆掉,因为m可以到2万。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;

#define MAX 9999999

#define LEN 210

int G[LEN][LEN]; //某点到某点两点间的的距离
int dist[LEN]; //记录当前点到源点的最短路径长度
int mark[LEN]; //加入进来的点的集合

//初始化map为正无穷大
void init()
{
   	int i,j;
   	for(i=0;i<LEN;i++)
 	{
        for(j=0;j<LEN;j++)
        {
            G[i][j]=0;
        }
   	}
}

//n:多少条路 start:起始点 
//dist[i],最后存储着start 到i点的最短距离
void myDijstra(int n,int start)
{
   	int i,j,max,pos;
   	for(i=1;i<=n;i++)
	{
        mark[i]=0;//没有点加入
        dist[i]=G[start][i];//把start 附近点 dis[]初始化 
   	}
	
	mark[start]=1;//把起始点加进来
	dist[start]=0;

    for(i=1;i<=n;i++)
 	{
        max=0;
        for(j=1;j<=n;j++)
        {
            if(!mark[j] && dist[j] > max)
            {
                max=dist[j];
                pos=j;//标记
            }
        }
            
        if(max == 0)//已经不能通了
            break;

        mark[pos]=1;//把K加进来

        //做松弛操作
        for(j=1;j<=n;j++)
        {
            if(!mark[j] && G[pos][j] ) //start->j or start->pos,pos->j 
            {
        		if(dist[pos] > G[pos][j])
            		dist[j] = G[pos][j] > dist[j] ? G[pos][j] : dist[j];
            	else
            		dist[j] = dist[pos] > dist[j] ? dist[pos] : dist[j];
            }
        }
    }
}

int main()
{
   	int i,ppp = 1;

   	int s, e, len, k = 1;

	char s1[35],s2[35];
   	map<string, int>m;
	int n, r;
	while(scanf("%d%d", &n, &r), n + r)
	{
		k = 1;
	  	init();
		for(i = 0; i < r; i++)
		{
			scanf("%s%s%d", s1, s2, &len);
			if(m.find(s1) == m.end())
				m[s1] = s = k++;
			else
				s = m[s1];
			if(m.find(s2) == m.end())
				m[s2] = e = k++;
			else
				e = m[s2];
			G[s][e] = G[e][s] = len;
		}
		scanf("%s%s",s1,s2);
		s = m[s1];
		e = m[s2];
		myDijstra(n,s);//调用方法(点数,起始点)
		printf("Scenario #%d\n",ppp++);
		printf("%d tons\n",dist[e]);
		printf("\n"); 
		m.clear();
	}
   	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值