HDU3311-Dig The Wells

Dig The Wells

                                                                   Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                           Total Submission(s): 1420    Accepted Submission(s): 642


Problem Description
You may all know the famous story “Three monks”. Recently they find some places around their temples can been used to dig some wells. It will help them save a lot of time. But to dig the well or build the road to transport the water will cost money. They do not want to cost too much money. Now they want you to find a cheapest plan.
 

Input
There are several test cases.
Each test case will starts with three numbers n , m, and p in one line, n stands for the number of monks and m stands for the number of places that can been used, p stands for the number of roads between these places. The places the monks stay is signed from 1 to n then the other m places are signed as n + 1 to n + m. (1 <= n <= 5, 0 <= m <= 1000, 0 <=p <= 5000)
Then n + m numbers followed which stands for the value of digging a well in the ith place.
Then p lines followed. Each line will contains three numbers a, b, and c. means build a road between a and b will cost c.
 

Output
For each case, output the minimum result you can get in one line.
 

Sample Input
  
  
3 1 3 1 2 3 4 1 4 2 2 4 2 3 4 4 4 1 4 5 5 5 5 1 1 5 1 2 5 1 3 5 1 4 5 1
 

Sample Output
  
  
6 5
 

Author
dandelion
 

Source
 

Recommend
wxl
 


题意:选择一些点使得1~n的点与井直接或间接相连,使得总花费最小。1~n+m号点都可以成为井。每个点成为井的花费不同,且修每条路的花费也不同

解题思路:斯坦纳树,建立一个源点0,连接各点,而与各点之间的路程的花费就是各点成为井的花费


#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, p, u, v, w;
int s[1500], nt[100009], e[100009], val[100009], cnt;
int status;//表示0~n号节点都被选择时的状态+1  
int dis[1500][200], vis[1500][200];
//dis[i][j]表示以i节点为根选择点集状态为j时的最小值;vis[i][j]表示i节点为点集j时是否在队列中  
queue<node> q;

void init()
{
	memset(s, -1, sizeof s);
	memset(vis, 0, sizeof vis);
	cnt = 0;
	status = 1 << (n + 1);
	for (int i = 0; i <= n + m; i++)
		for (int j = 0; j < status; j++)
			dis[i][j] = INF;
	for (int i = 0; i <= n; 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 + m; 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 main()
{
	while (~scanf("%d%d%d", &n, &m, &p))
	{
		init();
		for (int i = 1; i <= m + n; i++)
		{
			scanf("%d", &w);
			nt[cnt] = s[0], s[0] = cnt, e[cnt] = i, val[cnt++] = w;
			nt[cnt] = s[i], s[i] = cnt, e[cnt] = 0, val[cnt++] = w;
		}
		for (int i = 0; i < p; i++)
		{
			scanf("%d%d%d", &u, &v, &w);
			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();
		int ans = INF;
		for (int j = 0; j <= n + m; j++)
			ans = min(ans, dis[j][status - 1]);
		printf("%d\n", ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值