UVA - 10099

The Tourist Guide

Mr. G. works as a tourist guide. His current assignment is to take some tourists from one city to
another. Some two-way roads connect the cities. For each pair of neighboring cities there is a bus
service that runs only between those two cities and uses the road that directly connects them. Each
bus service has a limit on the maximum number of passengers it can carry. Mr. G. has a map showing
the cities and the roads connecting them. He also has the information regarding each bus service. He
understands that it may not always be possible for him to take all the tourists to the destination city
in a single trip. For example, consider the following road map of 7 cities. The edges connecting the
cities represent the roads and the number written on each edge indicates the passenger limit of the bus
service that runs on that road.
Now, if he wants to take 99 tourists from city 1 to city 7, he will require at least 5 trips and the
route he should take is : 1 - 2 - 4 - 7.
But, Mr. G. finds it difficult to find the best route all by himself so that he may be able to take all
the tourists to the destination city in minimum number of trips. So, he seeks your help.
在这里插入图片描述

Input
The input will contain one or more test cases. The first line of each test case will contain two integers:
N (N ≤ 100) and R representing respectively the number of cities and the number of road segments.
Then R lines will follow each containing three integers: C1, C2 and P. C1 and C2 are the city numbers
and P (P > 1) is the limit on the maximum number of passengers to be carried by the bus service
between the two cities. City numbers are positive integers ranging from 1 to N. The (R + 1)-th line
will contain three integers: S, D and T representing respectively the starting city, the destination city
and the number of tourists to be guided.
The input will end with two zeroes for N and R.
Output
For each test case in the input first output the scenario number. Then output the minimum number
of trips required for this case on a separate line. Print a blank line after the output of each test case.
Sample Input
7 10
1 2 30
1 3 15
1 4 10
2 4 25
2 5 60
3 4 40
3 6 20
4 7 35
5 7 20
6 7 30
1 7 99
0 0
Sample Output
Scenario #1
Minimum Number of Trips = 5
题意
导游想要把游客从某一城市送到另一城市,每条路上有乘客限制,选取最优路线,使游客到目的城市乘坐车辆次数最少
分析:
该题求的是最小值里的最大值
只需要求一次拉乘客最大数量,就可求得最少拉乘客次数

核心代码

for(k=1;k<=n;k++)
		{
			for(i=1;i<=n;i++)
			{
				for(j=1;j<=n;j++)
				{
					e[i][j]=max(e[i][j],min(e[i][k],e[k][j]));
				}
			}
		}


分析:
min(e[i][k],e[k][j])
**此时是还未找到到达终点通行的路
//表示:假如1- ->4 城市,我们可以从1 直接到 4,也可以借助 2 作为中间城市到达4,此时由于城市有之间的高速有乘客人数限制 而且乘客不能中途下车,所以只能选取这一条路上最少通行乘客数作为这条路通行量
e[i][j] = max ( e[i][j] , min( e[i][k] , e[k][j] ) )
**此时是找到了许多满足条件的路,这些路都是能走的,但要选取最大的那个。
选取在min( e[i][k] , e[k][j] ) 形成通行的道路中选取最大的那个最为最优路线

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int e[200][200];
int main()
{
	int m,n,i,j,k,z=0;
	while(~scanf("%d %d",&n,&m))
	{
		if(n==0&&m==0)
			break;
		z++;	
		memset(e,0,sizeof(e));
		int t1,t2,t3;
		for(i=1;i<=m;i++)
		{
			scanf("%d %d %d",&t1,&t2,&t3);
			e[t1][t2]=e[t2][t1]=t3;
		}
		int a,b,s,h,g; 
		//printf("********\n");
		scanf("%d %d %d",&a,&b,&s);
		for(k=1;k<=n;k++)
		{
			for(i=1;i<=n;i++)
			{
				for(j=1;j<=n;j++)
				{
					e[i][j]=max(e[i][j],min(e[i][k],e[k][j]));
				}
			}
		}
		h=e[a][b]-1;//每次导游都要在车上,每次的最大载人量减一; 
		g=(s+h-1)/h;	//总人数/一次的人数==次数 
		printf("Scenario #%d\n",z);
		printf("Minimum Number of Trips = %d\n\n",g);
	}
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值