HDU4085-Peach Blossom Spring

本文介绍了一个基于中国古典文学作品《桃花源记》的算法竞赛题目,通过构建斯坦纳树模型解决村庄间的道路修复问题,以实现特定房屋间的连通性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Peach Blossom Spring

                                                                 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                           Total Submission(s): 2856    Accepted Submission(s): 1107


Problem Description

Tao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous works is "Peach Blossom Spring", which is a fable about a chance
discovery of an ethereal village where the people lead an ideal existence in harmony with nature, unaware of the outside world for centuries. So in Chinese, "Peach Blossom Spring" means "utopia".
In the story of "Peach Blossom Spring", there was a mysterious place. In Qin dynasty, some people escaped to that place during the civil unrest and built a village. They and their descendants never left and never had any contact with the outside world since then, until centuries latter a fisherman of Jin dynasty found them.
Recently, some Chinese ACMers happened to find the relics of the village mentioned in"Peach Blossom Spring".
They also found a document about building hiding places to escape from Qin army. The document said:
There were n houses and m roads in the village. Each road connected two houses. These houses were numbered from 1 to n. There were k families, each living in a different house. 
The houses they lived were house 1, house 2, … , house k. There were also k broken houses: house n-k+1, house n-k+2, ... , house n, with secret basements so that those houses could be used as hiding places.
The problem was that all roads were broken. People wanted to repair some roads so that every family could reach a hiding place through the repaired roads. Every hiding place could only hold one family. Each road cost some labor to be repaired. The head of the village wanted to find out the minimum cost way of repairing the roads, but he didn't know how to do.
Would you solve the problem which the ancient village head never solved?
 

Input
The input begins with a line containing an integer T(T<=50), the number of test cases. For each case, the first line begins with three integers ---- the above mentioned n (4<=n<=50), m (0<=m<=1000) and k (1<=k<=5, 2k<=n). Then m lines follow, each containing three integers u,v and w, indicating that there is a broken road connecting house u an d v, and the cost to repair that road is w(1<=w<=1000).
 

Output
For each test case, if you cannot find a proper way to repair the roads, output a string "No solution" in a line. Otherwise, output the minimum cost to repair the roads in a line.
 

Sample Input
  
2 4 3 1 4 2 10 3 1 9 2 3 10 6 7 2 1 5 1000 2 6 1000 1 3 1 2 3 1 3 4 1 4 5 1 4 6 1
 

Sample Output
  
29 5
 

Source
 

Recommend
lcy
 


题意:给你n,m,k分别表示有n个点,m条边,每条边有一个权值,表示修复这条边需要的代价,从前k个点中任取一个使其和后k个点中的某一个点能联通,并且必须是一一对应,问最小的代价是多少

解题思路:斯坦纳树


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

struct node
{
	int x, y;
}pre, nt1;
int n, m, k, u, v, w;
int s[55], nt[2009], e[2009], val[2009], cnt;
int status;//表示0~n号节点都被选择时的状态+1  
int dis[55][1200], vis[55][1200], flag[1200], ans[1200];
//dis[i][j]表示以i节点为根选择点集状态为j时的最小值;vis[i][j]表示i节点为点集j时是否在队列中  
queue<node> q;

int get(int x)
{
	if (x <= k) return x - 1;
	else if (x > n - k) return x - n + 2 * k - 1;
	else return x + k - 1;
}

void init()
{
	memset(s, -1, sizeof s);
	memset(vis, 0, sizeof vis);
	cnt = 0;
	status = 1 << (2 * k);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < status; j++)
			dis[i][j] = INF;
	for (int i = 0; i < 2 * k; i++) dis[i][1 << i] = 0;
}

void SPFA()
{
	while (!q.empty())
	{
		pre = q.front();
		q.pop();
		vis[pre.x][pre.y] = 0;
		for (int i = s[pre.x]; ~i; i = nt[i])
		{
			if (dis[pre.x][pre.y] + val[i] < dis[e[i]][pre.y])
			{
				dis[e[i]][pre.y] = dis[pre.x][pre.y] + val[i];
				if (!vis[e[i]][pre.y])
				{
					nt1 = { e[i],pre.y };
					q.push(nt1);
					vis[e[i]][pre.y] = 1;
				}
			}
		}
	}
}

void Steiner_Tree()
{
	for (int i = 0; i < status; i++)
	{
		for (int j = 0; j < n; j++)
		{
			for (int k = i; k; k = (k - 1) & i)
				dis[j][i] = min(dis[j][i], dis[j][k] + dis[j][i - k]);
			if (dis[j][i] != INF)
			{
				nt1 = { j,i };
				q.push(nt1);
				vis[j][i] = 1;
			}
		}
		SPFA();
	}
}

int check(int x)
{
	int sum = 0;
	for (int i = 0; i < k; i++)
	{
		if ((1 << i) & x) sum++;
		if ((1 << (k + i)) & x) sum--;
	}
	return !sum;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d%d", &n, &m, &k);
		init();
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d%d", &u, &v, &w);
			u = get(u), v = get(v);
			nt[cnt] = s[u], s[u] = cnt, e[cnt] = v, val[cnt++] = w;
			nt[cnt] = s[v], s[v] = cnt, e[cnt] = u, val[cnt++] = w;
		}
		Steiner_Tree();
		for (int i = 0; i < status; i++)
		{
			flag[i] = check(i), ans[i] = INF;
			for (int j = 0; j < n; j++) ans[i] = min(ans[i], dis[j][i]);
		}
		for (int i = 0; i < status; i++)
		{
			if (flag[i])
			{
				for (int j = i; j; j = (j - 1) & i)
					if (flag[j]) ans[i] = min(ans[i], ans[j] + ans[i - j]);
			}
		}
		if (ans[status - 1] == INF) printf("No solution\n");
		else printf("%d\n", ans[status - 1]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值