HDU 4085 Peach Blossom Spring(斯坦納樹+dp)
Peach Blossom Spring
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3081 Accepted Submission(s): 1208 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
比裸的斯坦納樹多加了一個check,並且最後在dp一下
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 55;
const int MAXM = 5018;
const int MAX_STATUS = 1 << 15;
const int INF = 0x3f3f3f3f;
int tot,head[MAXN],n,m,k;
int dp[MAXN][MAX_STATUS],st[MAXN],endSt;
int Dp[MAX_STATUS];
bool vis[MAXN][MAX_STATUS];
struct Edge
{
int from,to,Next,w;
}edge[MAXM];
struct node
{
int x,y;
};
queue<node> q;
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
memset(st,0,sizeof(st));
memset(vis,0,sizeof(vis));
memset(dp,INF,sizeof(dp));
endSt = 1 << (2 * k);
for(int i = 0; i < k; i++) {
st[i] = (1 << i);
dp[i][st[i]] = 0;
st[n - k + i] = 1 << (k + i);
dp[n - k + i][st[n - k + i]] = 0;
}
}
void addedge(int u,int v,int w)
{
edge[tot].from = u;
edge[tot].to = v;
edge[tot].w = w;
edge[tot].Next = head[u];
head[u] = tot++;
}
void SPFA()
{
struct node t,ne;
while(!q.empty()) {
t = q.front();
q.pop();
vis[t.x][t.y] = 0;
for(int i = head[t.x]; i != -1; i = edge[i].Next) {
int v = edge[i].to;
int sta = t.y | st[v];
if(dp[t.x][t.y] + edge[i].w < dp[v][sta]) {
dp[v][sta] = dp[t.x][t.y] + edge[i].w;
if(sta == t.y && !vis[v][sta]) {
ne.x = v;
ne.y = sta;
q.push(ne);
vis[v][sta] = 1;
}
}
}
}
}
void Steiner_Tree()
{
struct node t;
for(int i = 0; i < endSt; i++) {
for(int j = 0; j < n; j++) {
for(int k = i; k; k = (k - 1) & i) {
dp[j][i] = min(dp[j][i],dp[j][k | st[j]] + dp[j][(i - k) | st[j]]);
}
if(dp[j][i] != INF) {
t.x = j;
t.y = i;
q.push(t);
vis[j][i] = 1;
}
}
SPFA();
}
}
bool check(int state)
{
int t = 0;
for(int i = 0; i < k; i++) {
if(state & (1 << i)) t++;
if(state & (1 << (k + i))) t--;
}
return t == 0;
}
int DP()
{
for(int i = 0; i < endSt; i++) {
Dp[i] = INF;
for(int j = 0; j < n; j++) {
Dp[i] = min(Dp[i],dp[j][i]);
}
}
for(int i = 1; i < endSt; i++) {
if(!check(i)) continue;
for(int j = (i - 1) & i; j; j = (j - 1) & i) {
if(!check(j)) continue;
Dp[i] = min(Dp[i],Dp[j] + Dp[i - j]);
}
}
if(Dp[endSt - 1] == INF) printf("No solution\n");
else printf("%d\n",Dp[endSt - 1]);
}
int main(void)
{
int p,u,v,w;
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--,v--;
addedge(u,v,w);
addedge(v,u,w);
}
Steiner_Tree();
DP();
}
return 0;
}