poj 1639(有限制的最小生成树)

http://poj.org/problem?id=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

做这种最小k度的最小生成树,有做法:

1.忽略顶点V0。我们对其他顶点做一次最小生成树,此时形成一颗森林。

2.对步骤1中形成的每一颗最小生成树离顶点最近的点连起来。此时顶点的度数为m.

3.由m度->m+1度

(1)先dp预处理出当前生成树中与顶点V0到Vi的路径中与V0不关联的权值最大的边。dp[i].d记录的是权值,dp[i].u和dp[i].v记录的是边两边的点。

(2)对于每一个不在生成树的把他们加进去最小生成树中,那么久会生成一个环。我们把环中的最大的权值删去。即1中得到的dp[v].d。就会形成m+1个度

(3)对于(2)中我们枚举每一个顶点v。当minnum=min(g[v0][v]-dp[v].d)最小的时候。点v就是所求的点。 (4)重复步骤(3)直到k。其实当某一步的minnum>=0的时候就可以停止算法了。因为就算在增加度数生成树的权值也不会减小

 

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 22;
struct Edge
{
	int u, v, d;
	Edge() {}
	Edge(int a, int b, int c) :u(a), v(b), d(c) {}
	bool operator <(const Edge &e) const {
		return dnodes;
vectoredges;
int g[maxn][maxn];
bool tree[maxn][maxn];
int minEdge[maxn];
Edge dp[maxn];
int find(int p)
{
	if (p == parent[p])
		return parent[p];
	return parent[p] = find(parent[p]);
}
void un(int p, int q)
{
	parent[find(p)] = find(q);
}
void kruskal()
{
	sort(edges.begin(), edges.end());
	for (int i = 0; ig[cur][i])
				dp[i] = dp[cur];
			else
			{
				dp[i].u = cur;
				dp[i].v = i;
				dp[i].d = g[cur][i];
			}
		}
		dfs(i, cur);
	}
}
void slove()
{
	int keyPoint[maxn];
	//memset(keyPoint,0,sizeof(keyPoint)); 
	for (int i = 2; i <= cnt; i++)         //记录与1相连的最小的值
	{
		if (g[1][i] != INF)
		{
			int color = find(i);
			if (minEdge[color]>g[1][i])
			{
				minEdge[color] = g[1][i];
				keyPoint[color] = i;

			}
		}
	}
	for (int i = 1; i <= cnt; i++)  
	{
		if (minEdge[i] != INF)  //1号节点与m个子图连接m条最小值边
		{
			m++;
			tree[1][keyPoint[i]] = tree[keyPoint[i]][1] = 1;
			ans += g[1][keyPoint[i]];
		}
	}
	for (int i = m + 1; i <= k; i++)
	{
		memset(dp, -1, sizeof(dp));
		dp[1].d = -INF;  
		for (int j = 2; j <= cnt; j++)   
		{
			if (tree[1][j])
			{
				dp[j].d = -INF;     //区分是否与1相连
			}
		}
		dfs(1, -1);
		int idx, minnum = INF;
		for (int j = 2; j <= cnt; j++)     //枚举每一个顶点,找出差值最小的
		{
			if (minnum>g[1][j] - dp[j].d)
			{
				minnum = g[1][j] - dp[j].d;
				idx = j;
			}
		}
		if (minnum >= 0)
			break;
		tree[1][idx] = tree[idx][1] = 1;
		tree[dp[idx].u][dp[idx].v] = tree[dp[idx].v][dp[idx].u] = 1;
		ans += minnum;
	}
}
void init()
{
	memset(g, INF, sizeof(g));
	memset(tree, 0, sizeof(tree));
	memset(minEdge, INF, sizeof(minEdge));
	m = 0;
	cnt = 1;
	ans = 0;
	nodes["Park"] = 1;
	for (int i = 0; i> s1 >> s2 >> d;
		if (!nodes[s1])
			nodes[s1] = ++cnt;
		if (!nodes[s2])
			nodes[s2] = ++cnt;
		int u = nodes[s1];
		int v = nodes[s2];
		edges.push_back(Edge(u, v, d));
		g[u][v] = g[v][u] = min(g[u][v], d);
	}
	scanf("%d", &k);
	kruskal();
	slove();
	printf("Total miles driven: %d\n", ans);
	return 0;
}

 

你可能感兴趣的:(最小生成树)