Peach Blossom Spring - HDU 4085 斯坦纳树
来源:互联网 发布:linux c 绝对路径 编辑:程序博客网 时间:2024/05/22 04:48
Peach Blossom Spring
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1752 Accepted Submission(s): 662
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
24 3 14 2 103 1 92 3 106 7 21 5 10002 6 10001 3 12 3 13 4 14 5 14 6 1
Sample Output
295
思路:其实……这个我也是刚学的,讲不好,就不误导大家了,仅仅贴出代码供自己借鉴。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define MAX 1200#define INF 1000000000struct node{ int v,len; node *next;}*head[MAX*2],tree[MAX*2];queue<int> qu;bool in[100][MAX];int cost[100][MAX],dp[MAX],st[MAX],n,nn,m,k,ptr,ans;void init(){ int i,j; ptr=0; nn=1<<(2*k); memset(st,0,sizeof(st)); memset(in,0,sizeof(in)); memset(head,NULL,sizeof(head)); for(j=0;j<n;j++) for(i=0;i<nn;i++) cost[j][i]=INF; for(i=0;i<k;i++) { st[i]=1<<i; cost[i][st[i]]=0; st[n-k+i]=1<<(i+k); cost[n-k+i][st[n-k+i]]=0; }}void AddEdge(int a,int b,int c){ tree[ptr].v=b; tree[ptr].len=c; tree[ptr].next=head[a]; head[a]=&tree[ptr++];}void Spfa(){ int i,j,v,nst,len; while(!qu.empty()) { j=qu.front()/MAX; i=qu.front()%MAX; qu.pop(); in[j][i]=false; node *p=head[j]; while(p!=NULL) { v=p->v; len=p->len; nst=i|st[v]; if(cost[j][i]+len<cost[v][nst]) { cost[v][nst]=cost[j][i]+len; if(nst==i && !in[v][nst]) { in[v][nst]=true; qu.push(v*MAX+nst); } } p=p->next; } }}void Steiner_Tree(){ int i,j,t,s; for(i=0;i<nn;i++) { for(j=0;j<n;j++) { for(t=(i-1)&i;t;t=(t-1)&i) cost[j][i]=min(cost[j][i],cost[j][t|st[j]]+cost[j][(i-t)|st[j]]); if(cost[j][i]<INF) { qu.push(j*MAX+i); in[j][i]=true; } } Spfa(); }}bool Check(int s){ int i,cnt=0; for(i=0;i<k;i++) { if(s&(1<<i)) cnt++; if(s&(1<<(k+i))) cnt--; } return cnt==0;}int Solve_DP(){ int i,j,t; for(i=0;i<nn;i++) { dp[i]=INF; for(j=0;j<n;j++) dp[i]=min(dp[i],cost[j][i]); } for(i=1;i<nn;i++) if(Check(i)) for(t=(i-1)&i;t;t=(t-1)&i) if(Check(t)) dp[i]=min(dp[i],dp[t]+dp[i-t]); return dp[nn-1];}int main(){ int t,i,j,a,b,c; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); init(); for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); a--;b--; AddEdge(a,b,c); AddEdge(b,a,c); } Steiner_Tree(); ans=Solve_DP(); if(ans<INF) printf("%d\n",ans); else printf("No solution\n"); }}
0 0
- HDU 4085 Peach Blossom Spring(DP,斯坦纳树)
- Peach Blossom Spring - HDU 4085 斯坦纳树
- hdu 4085 Peach Blossom Spring - 最小斯坦纳树
- HDU 4085 Peach Blossom Spring(斯坦纳树+dp)
- hdu 4085 Peach Blossom Spring(斯坦纳树)
- HDU 4085 Peach Blossom Spring(斯坦纳树)
- HDU 4085 Peach Blossom Spring 斯坦纳树
- 【斯坦纳树】 HDOJ 4085 Peach Blossom Spring
- HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA
- HDU 4085 Peach Blossom Spring 记忆化搜索枚举子集 斯坦纳树
- UVALive 5717 & HDU 4085 Peach Blossom Spring(DP+斯坦纳树)
- UVALive 5717 Peach Blossom Spring(斯坦纳树 STNT)
- UVALive 5717 Peach Blossom Spring 斯坦纳树
- 【HDU4085】Peach Blossom Spring【斯坦纳树】【状态压缩】
- [HDU4085]Peach Blossom Spring(斯坦纳树+dfs)
- [子集DP 斯坦纳树 模板题] HDU 4085 Peach Blossom Spring & BZOJ 4006 [JLOI2015]管道连接
- Peach Blossom Spring HDU
- hdu4085 Peach Blossom Spring (斯坦纳树,状态dp)spfa
- 单链表相关面试笔试题
- 笔试题积累
- hdu 1867 A + B for you again KMP算法
- 觉得帮助到你了,可以转载保存分享给身边的朋友。
- 关于集合Set里每一个元素前会自动添加一个空格,不知道为什么,有待研究
- Peach Blossom Spring - HDU 4085 斯坦纳树
- tuples
- 使用Tomcat配置域名
- android.permission权限请求汇总
- POJ2352_Stars(线段树/单点更新)
- window应用移植到Linux下(应用移植)
- Android:相册中获取图片以及路径
- Wormhole Transport - ZOJ 3613 斯坦纳树
- CF#259 (Div. 2) B.