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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值