HDU 4085 Peach Blossom Spring(斯坦纳树)

87 篇文章 0 订阅
34 篇文章 1 订阅

Peach Blossom Spring

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2808    Accepted Submission(s): 1087


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
lcy

题目大意:

    给你一张无向带权图,其中k个位置各有一个人,还有k个位置是他们的目的地,现在要选择一些边使得每个人都可以到达目的地之一,且每个人到达的地方不同。


解题思路:

    非常明显的斯坦纳树,只不过最后可能是一个由多棵斯坦纳树构成的森林,所以我们需要在求出斯坦纳树之后再dp一下。

    关于斯坦纳树的求解,一般使用转压dp,由于我们只关心部分点的连通情况,所以我们只需要对关心的点的连通情况进行状压即可。dp[i][j]表示当前在节点i,必须连通的点已经连接上的状态的压缩为j时的最小花费。

    状态转移有两种:1、枚举子树的状态:dp[i][j]=min(dp[i][j], dp[i][k]+dp[i][l],其中k和l是对j的一个划分。2、按照边进行松弛:dp[i][j]=min(dp[i][j], dp[i'][j]+cost[i'][i])其中i和i'有边相连。

    对于第一种转移,我们只需要枚举子集即可。第二个状态转移可能出现环,所以不同的转移方式不好处理,但它是满足三角形不等式的,所以我们可以使用spfa进行转移。


AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <list>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXV=50+3;
const int MAXS=(1<<10)+3;

struct Edge
{
    int to, cost;
    Edge(int t, int c):to(t), cost(c){}
};

vector<Edge> G[MAXV];
int dis[MAXV][MAXS];//当前在i,要求的点的连通状态为j时最小的花费
int state[MAXV];//第i个节点在状压中的位置(不要求必须连通的点不存在状压位置)
int V, E, K, S;
queue<int> que;
bool in_que[MAXV][MAXS];
int dp[MAXS];//要求的点的连通状态为j时的最小花费

void init()//初始化
{
    for(int i=0;i<=V;++i)
    {
        G[i].clear();
        state[i]=0;
    }
    for(int i=0;i<=V;++i)
        for(int j=0;j<S;++j)
            dis[i][j]=INF;
    for(int i=0;i<K;++i)
    {
        state[i]=1<<i;
        dis[i][state[i]]=0;//只包含目标点的状态花费为0
        state[V-1-i]=1<<(K+i);
        dis[V-1-i][state[V-1-i]]=0;
    }
    for(int i=0;i<S;++i)
        dp[i]=INF;
}

bool check(int s)//检查居民数和避难所的数是否相同
{
    int cnt=0;
    for(int i=0;s;++i, s>>=1)
        cnt+=(s&1)*(i<K?1:-1);
    return cnt==0;
}

void spfa()//利用spfa进行状态转移
{
    while(!que.empty())
    {
        int u=que.front()/10000, s=que.front()%10000; que.pop();
        in_que[u][s]=false;
        for(int i=0;i<G[u].size();++i)
        {
            int v=G[u][i].to;
            if(dis[u][s]+G[u][i].cost<dis[v][s|state[v]])
            {
                dis[v][s|state[v]]=dis[u][s]+G[u][i].cost;//第二种转移
                if(s==(s|state[v]) && !in_que[v][s])//只更新当前层,减少无用的更新
                {
                    in_que[v][s]=true;
                    que.push(v*10000+s);
                }
            }
        }
    }
}

int main()
{
    int T_T;
    scanf("%d", &T_T);
    while(T_T--)
    {
        scanf("%d%d%d", &V, &E, &K);
        S=1<<(2*K);
        init();
        while(E--)
        {
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            --u;
            --v;
            G[u].push_back(Edge(v, c));
            G[v].push_back(Edge(u, c));
        }
        for(int s=0; s<S;++s)//枚举连通状态
        {
            for(int u=0;u<V;++u)
            {
                for(int i=(s-1)&s;i;i=(i-1)&s)//枚举子集
                    dis[u][s]=min(dis[u][s], dis[u][i|state[u]]+dis[u][(s-i)|state[u]]);//第一种转移
                if(dis[u][s]<INF)
                {
                    que.push(u*10000+s);//状压表示当前点和连通状态
                    in_que[u][s]=true;
                }
            }
            spfa();
        }
        //已经求出斯坦纳树,接下来用dp找到合适的森林
        for(int s=0;s<S;++s)
            for(int u=0;u<V;++u)
                dp[s]=min(dp[s], dis[u][s]);
        for(int s=1;s<S;++s)
            if(check(s))
                for(int i=s&(s-1);i;i=(i-1)&s)//枚举子集,可能会有重复的边但是一定不会影响最终答案
                    if(check(i))
                        dp[s]=min(dp[s], dp[i]+dp[s-i]);
        if(dp[S-1]>=INF)
            puts("No solution");
        else printf("%d\n", dp[S-1]);
    }
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值