Picnic Planning POJ - 1639 度限制生成树

The Contortion Brothers are a famous set of circus clowns, known worldwide for their incredible ability to cram an unlimited number of themselves into even the smallest vehicle. During the off-season, the brothers like to get together for an Annual Contortionists Meeting at a local park. However, the brothers are not only tight with regard to cramped quarters, but with money as well, so they try to find the way to get everyone to the party which minimizes the number of miles put on everyone’s cars (thus saving gas, wear and tear, etc.). To this end they are willing to cram themselves into as few cars as necessary to minimize the total number of miles put on all their cars together. This often results in many brothers driving to one brother’s house, leaving all but one car there and piling into the remaining one. There is a constraint at the park, however: the parking lot at the picnic site can only hold a limited number of cars, so that must be factored into the overall miserly calculation. Also, due to an entrance fee to the park, once any brother’s car arrives at the park it is there to stay; he will not drop off his passengers and then leave to pick up other brothers. Now for your average circus clan, solving this problem is a challenge, so it is left to you to write a program to solve their milage minimization problem.
Input
Input will consist of one problem instance. The first line will contain a single integer n indicating the number of highway connections between brothers or between brothers and the park. The next n lines will contain one connection per line, of the form name1 name2 dist, where name1 and name2 are either the names of two brothers or the word Park and a brother’s name (in either order), and dist is the integer distance between them. These roads will all be 2-way roads, and dist will always be positive.The maximum number of brothers will be 20 and the maximumlength of any name will be 10 characters.Following these n lines will be one final line containing an integer s which specifies the number of cars which can fit in the parking lot of the picnic site. You may assume that there is a path from every brother’s house to the park and that a solution exists for each problem instance.
Output
Output should consist of one line of the form
Total miles driven: xxx
where xxx is the total number of miles driven by all the brothers’ cars.
Sample Input
10
Alphonzo Bernardo 32
Alphonzo Park 57
Alphonzo Eduardo 43
Bernardo Park 19
Bernardo Clemenzi 82
Clemenzi Park 65
Clemenzi Herb 90
Clemenzi Eduardo 109
Park Herb 24
Herb Eduardo 79
3
Sample Output
Total miles driven: 183

MST 都会,那么现在将一个点的度数加以限制,该怎样处理呢?

考虑1这个点,除去这个点之后,图会被分成几个联通块,对于这几个联通块我们可以用Kruskal 求其MST;
那么我们现在用1与这几个联通块连边,假设形成了 m度生成树,
对于 m+1 度,连一个边后会形成环,那么我们在环上找出尽量大的边,然后替换掉,
当然不必K度数才退出这个查找过程,当我们发现能够减去的权值<=0,此时退出即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 20005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define sq(x) (x)*(x)
#define eps 1e-6
const int N = 2500005;

inline int read()
{
	int x = 0, k = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-')k = -1; c = getchar(); }
	while (c >= '0' && c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return x * k;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}

struct Edge {
	int u, v, w;
	Edge(){}
	Edge(int u,int v,int w):u(u),v(v),w(w){}
	bool operator <(const Edge&rhs)const {
		return w < rhs.w;
	}
};

int n, m, k;
int cnt, ans;
int pt[maxn];
vector<Edge>edge;
Edge dp[maxn];
int g[50][50];
int minedge[maxn];
bool tree[100][100];
map<string, int>mp;

void init() {
	ms(tree); memset(g, 0x3f, sizeof(g)); memset(minedge, 0x3f, sizeof(minedge));
	cnt = 1; ans = 0;
	m = 0;
	mp["Park"] = 1;
	for (int i = 0; i < maxn; i++)pt[i] = i;
}

int find(int p) {
	if (p == pt[p])return p;
	return pt[p] = find(pt[p]);
}

void Union(int p, int q) {
	pt[find(p)] = find(q);
}

void kruskal() {
	sort(edge.begin(), edge.end());
	for (int i = 0; i < edge.size(); i++) {
		int p = edge[i].u, q = edge[i].v;
		if (p == 1 || q == 1)continue;
		if (find(p) != find(q)) {
			Union(p, q);
			ans += edge[i].w;
			tree[p][q] = tree[q][p] = 1;
		}
	}
}

void dfs(int cur, int last) {
	for (int i = 2; i <= cnt; i++) {
		if (i == last || !tree[cur][i])continue;
		if (dp[i].w == -1) {
			if (dp[cur].w > g[cur][i])dp[i] = dp[cur];
			else {
				dp[i].u = cur; dp[i].v = i; dp[i].w = g[cur][i];
			}
		}
		dfs(i, cur);
	}
}

void sol() {
	int keypot[maxn];

	for (int i = 2; i <= cnt; i++) {
		if (g[1][i] != inf) {// The edge is existed
			int col = find(i);
			if (minedge[col] > g[1][i]) {
				minedge[col] = g[1][i];
				keypot[col] = i;
			}
		}
	}
	int idx;
	for (int i = 1; i <= cnt; i++) {
		if (minedge[i] != inf) {
			m++;//  m degrees MST
			tree[1][keypot[i]] = tree[keypot[i]][1] = 1; ans += g[1][keypot[i]];
		}
	}
	
	for (int i = m + 1; i <= k; i++) {
         int minn = inf;
	     memset(dp, -1, sizeof(dp)); dp[1].w = -inf;
	     for (int j = 2; j <= cnt; j++) {
		     if (tree[1][j])dp[j].w = -inf;
	      }
	      dfs(1, -1);
		  for (int j = 2; j <= cnt; j++) {
			  if (minn > g[1][j] - dp[j].w) {
				  minn = g[1][j] - dp[j].w;
				  idx = j;
			  }
		  }
		  if (minn >= 0)break;
		  tree[1][idx] = tree[idx][1] = 1;
		  tree[dp[idx].v][dp[idx].u] = tree[dp[idx].u][dp[idx].v] = 0;
		  ans += minn;
	}
}


int main()
{
	n = read();
	init();
	int d;
	string s1, s2;
	for (int i = 1; i <= n; i++) {
		cin >> s1 >> s2;
		d = read();
		if (!mp[s1])mp[s1] = ++cnt; if (!mp[s2])mp[s2] = ++cnt;
		int u = mp[s1], v = mp[s2];
		edge.push_back(Edge(u, v, d));
	//	tree[u][v] = tree[u][v] = 1;
		g[u][v] = g[v][u] = min(g[u][v], d);
	}
	k = read();
	kruskal(); sol();
	printf("Total miles driven: %d\n", ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值