HDU - 3311 Dig The Wells (斯坦纳树)

本文探讨了斯坦纳树问题,一种复杂的组合优化问题,旨在寻找最短网络以连接特定点集,允许引入额外点以降低总成本。文章详细介绍了问题背景及应用场景,并提供了基于状态压缩动态规划的解决方案,包括两种状态转移方法。通过实例代码展示了如何实现斯坦纳树的求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dig The Wells

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1579    Accepted Submission(s): 706


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 3 1 2 3 4 1 4 2 2 4 2 3 4 4 4 1 4 5 5 5 5 1 1 5 1 2 5 1 3 5 1 4 5 1

Sample Output

6 5

Author

dandelion

Source

HDOJ Monthly Contest – 2010.02.06

斯坦纳树问题是组合优化问题,与最小生成树相似,是最短网络的一种。最小生成树是在给定的点集和边中寻求最短网络使所有点连通。而最小斯坦纳树允许在给定点外增加额外的点,使生成的最短网络开销最小。

这个问题比较现实,没有太多需要解释的地方 我们直接看解决方法:

首先我们知道,最优解必然是一棵树,这棵树又是由若干棵子树合并成的,

于是我们可以状态压缩,把kk个节点的连通状态用一个二进制数j表示

dp[i][j]dp[i][j]表示以i为根,与对应状态为jj的节点连通的子树的最小权值

有两种转移方法:

枚举子树的形态:dp[i][j]=min(dp[i][j],dp[i][k]+dp[i][l])dp[i][j]=min(dp[i][j],dp[i][k]+dp[i][l]),其中kk和ll是对j的一个划分

按照边进行松弛:dp[i][j]=min(dp[i][j],dp[i′][j]+w[i][i′])dp[i][j]=min(dp[i][j],dp[i′][j]+w[i][i′]),其中ii和i′i′之间有边相连

对于第一种转移,我们直接枚举子集就行了

对于第二种转移,我们仔细观察可以发现这个方程和最短路的约束条件是很类似的,于是我们可以用spfa或者dijkstra来进行状态转移 枚举子集的复杂度=n∗∑C(k,i)∗2i=n∗3k=n∗∑C(k,i)∗2i=n∗3k,spfa的复杂度为n∗2kn∗2k 所以总复杂度为O(n∗3k)

这道题建立一个源点,源点与所有点之间连接一条边,边权为这个点的点权,然后就是斯坦纳树的模板题了

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1010;
const int MAXM = 10100;
const int MAX_STATUS = 1 << 6;
const int INF = 0x3f3f3f3f;
int tot,head[MAXN],n,m;
int dp[MAXN][MAX_STATUS],st[MAXN],endSt;
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));
    endSt = 1 << (n + 1);
    for(int i = 0; i <= n + m; i++) {
        for(int j = 0; j <= endSt; j++) {
            dp[i][j] = INF;
        }
     }
     for(int i = 0; i <= n; i++) {
        st[i] = (1 << i);
        dp[i][st[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 + m; 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();
    }
}
int DP()
{
    int res = INF;
    for(int j = 0; j <= n + m; j++) {
        res = min(res,dp[j][endSt - 1]);
    }
    return res;
}
int main(void)
{
    int p,u,v,w;
    while(scanf("%d %d %d",&n,&m,&p) != EOF) {
        init();
        for(int i = 1; i <= n + m; i++) {
            scanf("%d",&w);
            addedge(0,i,w);
            addedge(i,0,w);
        }
        for(int i = 1; i <= p; i++) {
            scanf("%d %d %d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        Steiner_Tree();
        printf("%d\n",DP());
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值