UVA_10099_The Tourist Guide_kruscal

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::sort;
class EdgeType
{
public:
	pair<int, int>vextex;
	int weight;
	int vexnum;
	int edgenum;
	EdgeType()
	{
		vextex.first = vextex.second = weight = 0;
	}
};
vector<EdgeType>getGraph(EdgeType &object)
{
	cin >> object.vexnum >> object.edgenum;
	if (!object.vexnum&&!object.edgenum)
	{
		exit(0);
	}
	vector<EdgeType>edge(object.edgenum);
	for (int i = 0; i < object.edgenum; i++)
	{
		cin >> edge[i].vextex.first >> edge[i].vextex.second >> edge[i].weight;
	}
	return edge;
}
bool compare(const EdgeType&a, const EdgeType&b)
{
	if (a.weight != b.weight)
	{
		return a.weight > b.weight;
	}
	return a.vextex.first < b.vextex.first;
}
int getMark(int curmark, const vector<int>&mark)
{
	if (curmark == mark[curmark])
	{
		return curmark;
	}
	return getMark(mark[curmark], mark);
}
int kruscal(EdgeType &object)
{
	auto edge = getGraph(object);
	sort(edge.begin(), edge.end(), compare);
	int initial, terminal; cin >> initial >> terminal;
	vector<int>mark(object.vexnum);
	for (int i = 0; i < object.vexnum; i++)
	{
		mark[i] = i;
	}
	for (int i = 0; i < object.edgenum; i++)
	{
		auto mark_first = getMark(edge[i].vextex.first-1, mark);
		auto mark_second = getMark(edge[i].vextex.second-1, mark);
		if (mark_first != mark_second)
		{
			mark[mark_second] = mark_first;
			if (getMark(initial-1,mark)== getMark(terminal-1,mark))
			{
				return edge[i].weight;
			}
		}
	}
	return 0;
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int count = 0;
	while (1)
	{
		EdgeType edge;
		auto result = kruscal(edge);
		int weight;
		cin >> weight;
		printf("Scenario #%d\nMinimum Number of Trips = %.0lf\n\n",++count, ceil((double)weight / (double)(result - 1)));
	}
    return 0;
}

你可能感兴趣的:(uva,kruscal)