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


你可能感兴趣的:(Algorithm,C++)