HDU3311-Dig The Wells

来源:互联网 发布:域名别名查询 编辑:程序博客网 时间:2024/05/22 13:17

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 31 2 3 41 4 22 4 23 4 4 4 1 45 5 5 5 11 5 12 5 13 5 14 5 1
 

Sample Output
65
 

Author
dandelion
 

Source
HDOJ Monthly Contest – 2010.02.06
 

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

原创粉丝点击