HDU 4085

https://cn.vjudge.net/problem/HDU-4085

题意:

给你 n 房子, m 条路, k 户人家,每条路有一个权值,表示修复这条边需要的代价,起初 k 户人家依次住在第 1个,第 2 个,,,第 k 个房子里,现要求让着前 k 个房子和后
k 个房子一一对应连通建路,问最小的代价是多少。

分析:

可以先简单看成一个斯坦纳树问题:n 个点选给定的 2 * k 个点的最小生成树。但注意题意要求一一对应,所以最后还需要 dp2[],这很好理解,举个例子,四个点的最小
生成树权和不一定等于分割开的两棵最小生成树的权和。

dp[i][j] 表示以 j 为起点的包含点的状态为 i 的最小生成树。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
typedef long long int ll;
const int MOD = (int)1e9 + 7;
const int INF = 99999999;
using namespace std;

struct Edge
{
    int v;
    int w;
    int next;
};

int t;
int n, m, k;
int cnt;
int top;
int s[55];
int vis[1 << 10][55];
int dp1[1 << 10][55];
int dp2[1 << 10];
Edge edge[2005];
int head[55];
queueint, int> > Q;

void add_edge(int u, int v, int w)
{
    edge[cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;

    edge[cnt].v = u;
    edge[cnt].w = w;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}

bool check(int s)
{
    int res = 0;

    for (int i = 0; s != 0; i++)
    {
        if (s & 1)
            res += (i < k) ? 1 : -1;

        s >>= 1;
    }

    return (res == 0);
}

void spfa()
{
    while (!Q.empty())
    {
        int state = Q.front().first;
        int u = Q.front().second;
        Q.pop();

        vis[state][u] = 0;

        for (int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v;
            int w = edge[i].w;

            if (dp1[state | s[v]][v] > dp1[state][u] + w)
            {
                dp1[state | s[v]][v] = dp1[state][u] + w;

                if ((state | s[v]) == state && !vis[state][v])
                {
                    Q.push(make_pair(state, v));
                    vis[state][v] = 1;
                }
            }
        }
    }
}

int main()
{
    scanf("%d", &t);

    while (t--)
    {
        scanf("%d %d %d", &n, &m, &k);

        cnt = 0;
        top = 1 << (2 * k);

        memset(s, 0, sizeof(s));
        memset(vis, 0, sizeof(vis));
        memset(head, -1, sizeof(head));

        for (int i = 0; i < top; i++)
            for (int j = 0; j < n; j++)
                dp1[i][j] = INF;

        int u, v, w;
        for (int i = 0; i < m; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            add_edge(u - 1, v - 1, w);
        }

        for (int i = 0; i < k; i++)
        {
            s[i] = 1 << i;
            dp1[s[i]][i] = 0;

            s[n - 1 - i] = 1 << (k + i);
            dp1[s[n - 1 - i]][n - 1 - i] = 0;
        }

        for (int i = 0; i < top; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int t = (i - 1) & i; t != 0; t = (t - 1) & i)
                    dp1[i][j] = min(dp1[i][j], dp1[t | s[j]][j] + dp1[(i - t) | s[j]][j]);

                if (dp1[i][j] < INF)
                {
                    Q.push(make_pair(i, j));
                    vis[i][j] = 1;
                }
            }

            spfa();
        }

        for (int i = 0; i < top; i++)
        {
            dp2[i] = INF;

            for (int j = 0; j < n; j++)
            {
                if (i & (1 << j))
                {
                    dp2[i] = min(dp2[i], dp1[i][j]);
                    break;
                }
            }
        }

        for (int i = 0; i < top; i++)
        {
            if (check(i))
            {
                for (int t = (i - 1) & i; t != 0; t = (t - 1) & i)
                {
                    if (check(t))
                        dp2[i] = min(dp2[i], dp2[t] + dp2[i - t]);
                }
            }
        }

        if (dp2[top - 1] >= INF)
            printf("No solution\n");
        else
            printf("%d\n", dp2[top - 1]);
    }
    return 0;
}

你可能感兴趣的:(【动态规划】--斯坦纳树)