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

原创 2016年08月30日 12:21:24

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 2263 Heavy Cargo (SPFA+Dijkstra,最短路变形)

Dijkstra算法的变形 。 之前学图论的时候这个算法学的太快 没怎么练过,,以为用SPFA完全可以替代,,然而这两种算法的思想并不一样。 Dijkstra算法是从起点开始 ,每次在它相邻的节...

poj 2263 Heavy Cargo

这题是dijkstra的变形,dp[i]表示到城市i的最大负载量,比如a-----b,if dp[b]

poj2263 Heavy Cargo(dijstra 变形)

Description Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their ...

POJ-2263Heavy Cargo(优先队列)

Heavy CargoTime Limit: 1000MS Memory Limit: 65536K Total Submissions: 4535 Accepted: 2373 D...

poj 2263||zoj 1952Heavy Cargo(short path floyd)

题目是说要找到所有可达路径中的路的最小容量的最大值。==拗口 将字符串转换成数组下标,用了个index函数索引。 递推公式可简述成:A[i][j]=MAX(A[i][j],MIN(A[i][k],...
  • cqlf__
  • cqlf__
  • 2012年01月24日 13:06
  • 523

POJ 2263 Heavy Cargo(Floyd变形)

POJ 2263 Heavy Cargo(Floyd变形) http://poj.org/problem?id=2263 题意:给你一个无向图,图中每条路有一个负重限制v.现在要你求从A点到B的的所有...

POJ2263 Heavy Cargo

Floyd + Map

poj2263&zoj1952Heavy Cargo(floyd)

题目请戳这里 题目大意:给一张无向图,边权为正整数,代表两端点之间路上能承载的最大重量,给定一个起点和一个终点,问起点到终点最大载货量。 题目分析:此题方法很多。floyd比较好想。每条路都有最大...

[floyd]POJ 2263 Heavy Cargo

传送门:Heavy Cargo Heavy Cargo Time Limit: 1000MS   Memory Limit: 65536K Total Sub...

POJ 2263 Heavy Cargo(二分+并查集)

题目地址:POJ 2263 这题是在网上的一篇关于优先队列的博文中看到的。。但是实在没看出跟优先队列有什么关系。。我用的二分+并查集做出来了。。。 二分路的载重量。然后用并查集检查是否连通。 代...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ2263 Heavy Cargo用最短路得到最大流
举报原因:
原因补充:

(最多只允许输入30个字)