bzoj1497 [NOI2006]最大获利

1497: [NOI2006]最大获利

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 6241  Solved: 3006
[Submit][Status][Discuss]

Description

新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战。THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需要完成前期市场研究、站址勘测、最优化等项目。在前期市场调查和站址勘测之后,公司得到了一共N个可以作为通讯信号中转站的地址,而由于这些地址的地理位置差异,在不同的地方建造通讯中转站需要投入的成本也是不一样的,所幸在前期调查之后这些都是已知数据:建立第i个通讯中转站需要的成本为Pi(1≤i≤N)。另外公司调查得出了所有期望中的用户群,一共M个。关于第i个用户群的信息概括为Ai, Bi和Ci:这些用户会使用中转站Ai和中转站Bi进行通讯,公司可以获益Ci。(1≤i≤M, 1≤Ai, Bi≤N) THU集团的CS&T公司可以有选择的建立一些中转站(投入成本),为一些用户提供服务并获得收益(获益之和)。那么如何选择最终建立的中转站才能让公司的净获利最大呢?(净获利 = 获益之和 - 投入成本之和)

Input

输入文件中第一行有两个正整数N和M 。第二行中有N个整数描述每一个通讯中转站的建立成本,依次为P1, P2, …, PN 。以下M行,第(i + 2)行的三个数Ai, Bi和Ci描述第i个用户群的信息。所有变量的含义可以参见题目描述。

Output

你的程序只要向输出文件输出一个整数,表示公司可以得到的最大净获利。

Sample Input

5 5
1 2 3 4 5
1 2 3
2 3 4
1 3 3
1 4 2
4 5 3

Sample Output

4

HINT

【样例说明】选择建立1、2、3号中转站,则需要投入成本6,获利为10,因此得到最大收益4。【评分方法】本题没有部分分,你的程序的输出只有和我们的答案完全一致才能获得满分,否则不得分。【数据规模和约定】 80%的数据中:N≤200,M≤1 000。 100%的数据中:N≤5 000,M≤50 000,0≤Ci≤100,0≤Pi≤100。

分析:最大权闭合子图模型.

   利用网络流求解. 对于每一个中转站,向汇点连边,边权为建中转站的费用.  对于每一个用户群,源点向其连边,费用为收益. 每一个用户群向其所依赖的两个中转站连边,费用为inf.

   考虑最小割的意义:用户群与中转站之间的边肯定是不能割的. 如果割掉源点与用户群的边,就表示这个用户群的方案不会被选择. 如果割掉中转站与汇点的连边,就表示这个中转站会被选择. 最小割实际上就是额外的花费. 

   答案就是所有用户群的收益-最小割.

   最大流问题可以转换成最小割问题求解,当最小割的意义明确的时候.  在最小割问题中,如果不允许割掉某条边,就将这条边的边权设为inf.这条边所连的两个点就会有一个关联. 挺巧妙的.

   注意一点:算边数要把反向边也给算进来,数组不要开小了!

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 60100,inf = 0x7fffffff;
int n,m,a[maxn],head[maxn],nextt[maxn * 6],w[maxn * 6],to[maxn * 6],tot = 2,S,T;
int d[maxn],ans;

void add(int x,int y,int z)
{
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;

    w[tot] = 0;
    to[tot] = x;
    nextt[tot] = head[y];
    head[y] = tot++;
}

bool bfs()
{
    memset(d,-1,sizeof(d));
    d[S] = 0;
    queue <int>q;
    q.push(S);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (u == T)
            return true;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (w[i] && d[v] == -1)
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
    return false;
}

int dfs(int u,int f)
{
    if (u == T)
        return f;
    int res = 0;
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (w[i] && d[v] == d[u] + 1)
        {
            int temp = dfs(v,min(f - res,w[i]));
            w[i] -= temp;
            w[i ^ 1] += temp;
            res += temp;
            if (res == f)
                return res;
        }
    }
    if (res == 0)
        d[u] = -1;
    return res;
}

void dinic()
{
    while (bfs())
        ans -= dfs(S,inf);
}

int main()
{
    scanf("%d%d",&n,&m);
    S = n + m + 1;
    T = n + m + 2;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
        add(i,T,a[i]);
    }
    for (int i = 1; i <= m; i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        ans += z;
        add(i + n,x,inf);
        add(i + n,y,inf);
        add(S,i + n,z);
    }
    dinic();
    printf("%d
",ans);

    return 0;
}

 update:加了当前弧优化,快了好多.

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 60100,inf = 0x7fffffff;
int n,m,a[maxn],head[maxn],nextt[maxn * 6],w[maxn * 6],to[maxn * 6],tot = 2,S,T;
int d[maxn],ans,cur[maxn];

void add(int x,int y,int z)
{
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;

    w[tot] = 0;
    to[tot] = x;
    nextt[tot] = head[y];
    head[y] = tot++;
}

bool bfs()
{
    memset(d,-1,sizeof(d));
    d[S] = 0;
    queue <int>q;
    q.push(S);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (u == T)
            return true;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (w[i] && d[v] == -1)
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
    return false;
}

int dfs(int u,int f)
{
    if (u == T)
        return f;
    int res = 0;
    for (int i = cur[u];i;i = nextt[i])
    {
        int v = to[i];
        if (w[i] && d[v] == d[u] + 1)
        {
            int temp = dfs(v,min(f - res,w[i]));
            w[i] -= temp;
            w[i ^ 1] += temp;
            res += temp;
            if (w[i] > 0)
                cur[u] = i;
            if (res == f)
                return res;
        }
    }
    if (res == 0)
        d[u] = -1;
    return res;
}

void dinic()
{
    while (bfs())
    {
        for (int i = 1; i <= T; i++)
            cur[i] = head[i];
        ans -= dfs(S,inf);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    S = n + m + 1;
    T = n + m + 2;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
        add(i,T,a[i]);
    }
    for (int i = 1; i <= m; i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        ans += z;
        add(i + n,x,inf);
        add(i + n,y,inf);
        add(S,i + n,z);
    }
    dinic();
    printf("%d
",ans);

    return 0;
}
原文地址:https://www.cnblogs.com/zbtrs/p/8587610.html