1. 程式人生 > >斯坦納樹——hdu 4085

斯坦納樹——hdu 4085

最近打全國高校綠色計算機大賽遇到了一個斯坦納樹(決賽第二階段第三題),當時網上找了模板水過去了,賽後學習一下

【一】什麼是斯坦納樹

斯坦納樹問題是組合優化學科中的一個問題。將指定點集合中的所有點連通,且邊權總和最小的生成樹稱為最小斯坦納樹(Minimal Steiner Tree),其實最小生成樹是最小斯坦納樹的一種特殊情況。而斯坦納樹可以理解為使得指定集合中的點連通的樹,但不一定最小。

【二】如何求解斯坦納樹

可以用DP求解,dp[i][state]表示以i為根,指定集合中的點的連通狀態為state的生成樹的最小總權值。

轉移方程有兩重:

第一重,先通過連通狀態的子集進行轉移。

dp[i][state]=min{ dp[i][subset1]+dp[i][subset2] } 

列舉子集的技巧可以用 for(sub=(state-1)&state;sub;sub=(sub-1)&state)。

第二重,在當前列舉的連通狀態下,對該連通狀態進行鬆弛操作。

dp[i][state]=min{ dp[i][state], dp[j][state]+e[i][j] }

為什麼只需對該連通狀態進行鬆弛?因為更後面的連通狀態會由先前的連通狀態通過第一重轉移得到,所以無需對別的連通狀態鬆弛。鬆弛操作用SPFA即可。

複雜度 O(n*3^k+cE*2^k)

c為SPFA複雜度中的常數,E為邊的數量,但幾乎達不到全部邊的數量,甚至非常小。3^k來自於子集的轉移sum{C(i,n)*2^i} (1<=i<=n),用二項式展開求一下和。

【三】模板

/*
 *  Steiner Tree:求,使得指定K個點連通的生成樹的最小總權值
 *  st[i] 表示頂點i的標記值,如果i是指定集合內第m(0<=m<K)個點,則st[i]=1<<m 
 *  endSt=1<<K
 *  dptree[i][state] 表示以i為根,連通狀態為state的生成樹值
 */
#define CLR(x,a) memset(x,a,sizeof(x))

int dptree[N][1<<K],st[N],endSt;
bool vis[N][1<<K];
queue<int> que;

int input()
{
   /*
    *    輸入,並且返回指定集合元素個數K
    *    因為有時候元素個數需要通過輸入資料處理出來,所以單獨開個輸入函式。
    */
}

void initSteinerTree()
{
    CLR(dptree,-1);
    CLR(st,0);
    for(int i=1;i<=n;i++) CLR(vis[i],0);
    endSt=1<<input();
    for(int i=1;i<=n;i++)
        dptree[i][st[i]]=0;
}

void update(int &a,int x)
{
    a=(a>x || a==-1)? x : a;
}

void SPFA(int state)
{
    while(!que.empty()){
        int u=que.front();
        que.pop();
        vis[u][state]=false;
        for(int i=p[u];i!=-1;i=e[i].next){
            int v=e[i].vid;
            if(dptree[v][st[v]|state]==-1 || 
                dptree[v][st[v]|state]>dptree[u][state]+e[i].w){

                dptree[v][st[v]|state]=dptree[u][state]+e[i].w;
                if(st[v]|state!=state || vis[v][state]) 
                    continue; //只更新當前連通狀態
                vis[v][state]=true;
                que.push(v);
            }
        }
    }
}

void steinerTree()
{
    for(int j=1;j<endSt;j++){
        for(int i=1;i<=n;i++){
            if(st[i] && (st[i]&j)==0) continue;
            for(int sub=(j-1)&j;sub;sub=(sub-1)&j){
                int x=st[i]|sub,y=st[i]|(j-sub);
                if(dptree[i][x]!=-1 && dptree[i][y]!=-1)
                    update(dptree[i][j],dptree[i][x]+dptree[i][y]);
            }
            if(dptree[i][j]!=-1) 
                que.push(i),vis[i][j]=true;
        }
        SPFA(j);
    }
}

【四】hdu4085

題意:n個點編號為1~n,m條雙向邊,每條邊都有一個邊權,n個點中前k個點構成點集s1,後k個點構成點集s2,現在要選擇一些邊使得兩個集合中的點可以一一對應(這個一一對應可以理解為s1中的點全部出發,順著選好的邊走,每個點都可以在s2那裡找到一個歸宿,且不重複),總邊權之和最小是多少?

思路:比較明顯的斯坦納樹,但是最後的邊不一定非要連通,可能是一個森林,所以求完斯坦納樹後還要進行一遍狀壓dp,這個狀壓dp合併狀態時只能合併合法狀態,合法狀態的定義為已選擇的s1中的點的個數要等於已選擇的s2中的點的個數

#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
const int maxm = 2000 + 10;
const int maxs = 12;
const int INF = 0x3f3f3f3f;
int N, M, K;
int st[maxn], endst, dp[maxn][1<<maxs];
bool vis[maxn][1<<maxs];
queue<int> que;
int tot, head[maxn];
struct Edge { int to, cost, next; }edges[maxm];
int f[1<<maxs];

void init_edges() {
    tot = 0;
    memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cost) {
    edges[tot].to = v; edges[tot].cost = cost; edges[tot].next = head[u]; head[u] = tot++;
}
void initstn() {
    memset(st, 0, sizeof(st));
    memset(dp, -1, sizeof(dp));
    memset(vis, 0, sizeof(vis));
    int bit = 0;
    for (int i = 1; i <= K; i++) {
        st[i] = (1<<bit);
        bit++;
    }
    for (int i = N-K+1; i <= N; i++) {
        st[i] = (1<<bit);
        bit++;
    }
    endst = (1<<bit);
    for (int i = 1; i <= N; i++) {
        dp[i][st[i]] = 0;
    }
}
void update(int &a, int x) {
    a = (a > x || a == -1) ? x : a;
}
void spfa(int state) {
    while (!que.empty()) {
        int u = que.front();
        que.pop();
        vis[u][state] = false;
        for (int i = head[u]; ~i; i = edges[i].next) {
            int v = edges[i].to, cost = edges[i].cost;
            if (dp[v][st[v]|state] == -1 || dp[v][st[v]|state] > dp[u][state] + cost) {
                dp[v][st[v]|state] = dp[u][state] + cost;
                if ((st[v]|state) != state || vis[v][state]) continue;
                vis[v][state] = true;
                que.push(v);
            }
        }
    }
}
void stn() {
    for (int j = 1; j < endst; j++) {
        for (int i = 1; i <= N; i++) {
            if (st[i] && (st[i]&j) == 0) continue;
            for (int sub = (j-1)&j; sub; sub = (sub-1)&j) {
                int x = st[i]|sub, y = st[i]|(j-sub);
                if (dp[i][x] != -1 && dp[i][y] != -1) {
                    update(dp[i][j], dp[i][x]+dp[i][y]);
                }
            }
            if (dp[i][j] != -1) {
                que.push(i); vis[i][j] = true;
            }
        }
        spfa(j);
    }
}
bool judge(int s) {
    int cnt1 = 0;
    for (int i = 0; i < K; i++) {
        if (s & (1<<i)) cnt1++;
    }
    int cnt2 = 0;
    for (int i = K; i < 2*K; i++) {
        if (s & (1<<i)) cnt2++;
    }
    return cnt1 == cnt2;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d%d", &N, &M, &K);
        init_edges();
        for (int i = 0; i < M; i++) {
            int u, v, cost;
            scanf("%d%d%d", &u, &v, &cost);
            addedge(u, v, cost);
            addedge(v, u, cost);
        }
        initstn();
        stn();
        for (int i = 0; i < endst; i++) {
            f[i] = INF;
            for (int j = 1; j <= N; j++) {
                if (dp[j][i] != -1) f[i] = min(f[i], dp[j][i]);
            }
        }
        for (int s = 1; s < endst; s++) {
            if (!judge(s)) continue;
            for (int sub = (s-1)&s; sub; sub = (sub-1)&s) {
                if (!judge(sub)) continue;
                f[s] = min(f[s], f[sub] + f[s-sub]);
            }
        }
        if (f[endst-1] == INF) puts("No solution");
        else printf("%d\n", f[endst-1]);
    }
    return 0;
}

參考博文:https://www.cnblogs.com/ECJTUACM-873284962/p/7643445.html