ACM: 最大流问题 图论题 poj 2263

来源:互联网 发布:网络安全工程师年薪 编辑:程序博客网 时间:2024/05/22 02:25
                                         Heavy Cargo

 

Description

Big Johnsson TrucksInc. is a company specialized in manufacturing big trucks. Theirlatest model, the Godzilla V12, is so big that the amount of cargoyou can transport with it is never limited by the truck itself. Itis only limited by the weight restrictions that apply for the roadsalong the path you want to drive.

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

Input

The input willcontain one or more test cases. The first line of each test casewill contain two integers: the number of cities n(2<=n<=200) and the number of roadsegments r (1<=r<=19900) making upthe street network.
Then r lines will follow, each one describing one road segment bynaming the two cities connected by the segment and giving theweight limit for trucks that use this segment. Names are not longerthan 30 characters and do not contain white-space characters.Weight limits are integers in the range 0 - 10000. Roads can alwaysbe travelled in both directions.
The last line of the test case contains two city names: start anddestination.
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 testcase
  • a line saying "y tons" where y is the maximum possibleload
  • 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

题意: 找出一条路起点到终点的最大载货量, 但是每条路径都有重量限制, 要你求出最大的载货量.

 

解题思路:

         1. 要求出最大路径里面, 每条路径要求最小的的载货量. 因为一条路的载货量要看最小的一段.

         2. 有点像凸包的思路. (做题目的时候类比一下).

         3. floyd算法修改: g[i][j] = max(g[i][j],min(g[i][k],g[k][j]));

 

代码:

#include<cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 205

char a[MAX], b[MAX];
int g[MAX][MAX];
char str[MAX][55];
int num;
int start, end;
int n, m;

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

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

int find(char *ch)
{
 int i;
 for(i = 0; i < num; ++i)
 {
  if(strcmp(ch,str[i]) ==0)
   returni;
 }
 strcpy(str[num],ch);
 num++;
 return num-1;
}

void read_graph()
{
 int i, j, w;
 num = 0;
 for(i = 0; i < n; ++i)
  for(j = 0; j <n; ++j)
   g[i][j] =-1;
 for(i = 0; i < m; ++i)
 {
  scanf("%s %s%d",a,b,&w);
  int t1 = find(a);
  int t2 = find(b);
  g[t1][t2] = g[t2][t1] =w;
 }
 scanf("%s %s",a,b);
 start = find(a);
 end = find(b);
}

void floyd()
{
 for(int k = 0; k < n; ++k)
 {
  for(int i = 0; i< n; ++i)
  {
   for(int j =0; j < n; ++j)
    g[i][j]= max(g[i][j],min(g[i][k],g[k][j]));
  }
 }
}

int main()
{
// freopen("input.txt","r",stdin);
 int k = 1;
 while(scanf("%d%d",&n,&m) != EOF&& n !=0&& m != 0)
 {
  read_graph();
  floyd();
  printf("Scenario #%d\n%dtons\n\n",k++,g[start][end]);
 }
 return 0;
}


 

0 0
原创粉丝点击