HDU 3311

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

题意:

n 个和尚所住地各有一口井,另有 m 处也有口井,这 n + m 口井的挖掘需要花费,另外有 p 条路线连接这些井,问如何修路和挖掘使每个和尚都有水且总花费最小。

分析:

n + m 个点选 n 个点的最小生成树,但是要考虑到有可能出现这样总花费最小的情况:n 个和尚就在自己的所在地挖井,不造路,此时是不存在树的。

不过我们仔细分析,可以建一个超级源点 0,分别连接着 n + m 口井,路径值就是挖掘当前井的花费。

接下来就很简单了。

#include 
#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, w;
    int next;
};

int n, m, p;
int cnt;
int top;
int head[1010];
Edge edge[20000];
int state[1010];
int dp[1010][1 << 6];
int vis[1010][1 << 6];
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++;
}

void spfa()
{
    while (!Q.empty())
    {
        int u = Q.front().first;
        int s = Q.front().second;
        Q.pop();
        vis[u][s] = 0;

        for (int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v;
            int w = edge[i].w;
            int ss = s | state[v];
            if (dp[v][ss] > dp[u][s] + w)
            {
                dp[v][ss] = dp[u][s] + w;

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

int main()
{
    while (~scanf("%d%d%d", &n, &m, &p))
    {
        memset(head, -1, sizeof(head));
        memset(vis, 0, sizeof(vis));
        memset(state, 0, sizeof(state));
        cnt = 0;
        top = 1 << (n + 1);

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

        for (int i = 0; i <= n; i++)
        {
            state[i] = 1 << i;
            dp[i][state[i]] = 0;
        }

        int w;
        for (int i = 1; i <= (n + m); i++)
        {
            scanf("%d", &w);
            add_edge(0, i, w); // 超级源点
        }

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

        for (int i = 0; i < top; i++)
        {
            for (int j = 0; j <= (n + m); j++)
            {
                for (int sub = i; sub != 0; sub = (sub - 1) & i)
                    dp[j][i] = min(dp[j][i], dp[j][sub | state[j]] + dp[j][(i - sub) | state[j]]);

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

            spfa();
        }

        int res = INF;
        for (int i = 0; i <= (n + m); i++)
            res = min(res, dp[i][top - 1]);

        printf("%d\n", res);
    }
    return 0;
}

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