programming-challenges The Tourist Guide (110903) 题解

这片文章讲的思路很清晰。当然,用bfs也是一样可以解决这个问题的。最坑的是每次过不要忘记导游自己也占了一个名额。我就是想了好久都不明白为什么示例的答案是5而不是4,把导游忘了。

http://www.algorithmist.com/index.php/UVa_10099

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

const int MX = 110; 

int G[MX][MX]; 
int nodes[MX]; 

int bfs(int s, int d, int n) {
	int ans = 0; 

	for (int i = 0; i < n; i++) {
		if (G[s][i] > 0) {
			int capability = min(G[s][i], nodes[s]);
			if (capability > nodes[i]) {
				nodes[i] = capability; 
				if (i == d) {
					ans = max(ans, capability); 
				}
				else {
					ans = max(ans, bfs(i, d, n)); 
				}
			}
		}
	}

	return ans; 
}

int main() {
	int n, r; 
	int q = 0; 

	while (cin >> n >> r) {
		if (n == 0 && r == 0) break;
		q++; 
		memset(G, 0, sizeof(G)); 
		memset(nodes, 0, sizeof(nodes)); 

		for (int i = 0; i < r; i++) {
			int n1, n2, p; cin >> n1 >> n2 >> p; 
			G[n1-1][n2-1] = G[n2-1][n1-1] = p;
		}

		int s, d, t; cin >> s >> d >> t; 
		s--; d--; 
		nodes[s] = t; 

		int ans = bfs(s, d, n) - 1; 
		cout << "Scenario #" << q << endl;
		if (t % ans == 0) ans = t / ans; 
		else ans = t / ans + 1; 
		cout << "Minimum Number of Trips = " << ans << endl; 
		cout << endl; 
	}

	return 0; 
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值