Peach Blossom Spring
http://acm.hdu.edu.cn/showproblem.php?pid=4085
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题意:让前k个点分别与后k个点联通的最小代价
因为是分别连通,所以是斯坦纳森利
合并的时候判断是否前面的点与后面的点相等
#include<queue> #include<cstdio> #include<cstring> #define M (1<<10)+2 using namespace std; int n,m,k,t,now,mx; int front[51],to[2001],nxt[2001],tot,val[2001]; int f[51][M],g[M]; bool v[51]; int sum; queue<int>q; void add(int u,int v,int w) { to[++tot]=v;nxt[tot]=front[u];front[u]=tot; val[tot]=w; to[++tot]=u;nxt[tot]=front[v];front[v]=tot; val[tot]=w; } bool check(int s) { sum=0; for(int i=1;i<=k;i++) if(s&(1<<(i-1))) sum++; for(int i=k+1;i<=mx;i++) if(s&(1<<(i-1))) sum--; return !sum; } int main() { scanf("%d",&t); int u,vv,w; while(t--) { memset(front,0,sizeof(front)); tot=0; scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=m;i++) { scanf("%d%d%d",&u,&vv,&w); add(u,vv,w); } memset(f,0x3f3f3f3f,sizeof(f)); memset(g,0x3f3f3f3f,sizeof(g)); mx=k<<1; for(int i=1;i<=k;i++) f[i][1<<(i-1)]=0; for(int i=k+1;i<=mx;i++) f[n-(mx-i)][1<<(i-1)]=0; for(int S=1;S<(1<<mx);S++) { for(int i=1;i<=n;i++) { for(int T=S-1;T;T=(T-1)&S) f[i][S]=min(f[i][S],f[i][T]+f[i][S^T]); if(f[i][S]<f[0][0]) q.push(i),v[i]=true; } while(!q.empty()) { now=q.front(); q.pop(); v[now]=false; for(int i=front[now];i;i=nxt[i]) if(f[to[i]][S]>f[now][S]+val[i]) { f[to[i]][S]=f[now][S]+val[i]; if(!v[to[i]]) { q.push(to[i]); v[to[i]]=true; } } } if(!check(S)) continue; for(int i=1;i<=n;i++) g[S]=min(g[S],f[i][S]); for(int T=S-1;T;T=(T-1)&S) g[S]=min(g[S],g[T]+g[S^T]); } if(g[(1<<mx)-1]==0x3f3f3f3f) printf("No solution\n"); else printf("%d\n",g[(1<<mx)-1]); } }