HDU 3879 Base Station(最大权闭合图)

Base Station

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65768/32768 K (Java/Others)
Total Submission(s): 2827    Accepted Submission(s): 1219


Problem Description
A famous mobile communication company is planning to build a new set of base stations. According to the previous investigation, n places are chosen as the possible new locations to build those new stations. However, the condition of each position varies much, so the costs to built a station at different places are different. The cost to build a new station at the ith place is P i (1<=i<=n).

When complete building, two places which both have stations can communicate with each other.

Besides, according to the marketing department, the company has received m requirements. The ith requirement is represented by three integers A i, B i and C i, which means if place A and B i can communicate with each other, the company will get C i profit.

Now, the company wants to maximize the profits, so maybe just part of the possible locations will be chosen to build new stations. The boss wants to know the maximum profits.
 

Input
Multiple test cases (no more than 20), for each test case:
The first line has two integers n (0<n<=5000) and m (0<m<=50000).
The second line has n integers, P1 through Pn, describes the cost of each location.
Next m line, each line contains three integers, A i, B i and C i, describes the ith requirement.
 

Output
One integer each case, the maximum profit of the company.
 

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

题解:
将所有点与en连一条边,权值为其花费,对于每一个收益的点对(x,y),
建立一个点k,st与k连一条权值为其收益的边,然后k->x,k->y连一条
权值为无穷大的边。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
const int maxn=60005;
const int st=0;
const int en=60004;
const int inf=1e9;
struct node
{
    int to,cap,rev;
};
vector<node>G[maxn];
int level[maxn],iter[maxn];
void add(int from,int to,int cap)
{
    G[from].push_back((node){to,cap,G[to].size()});
    G[to].push_back((node){from,0,G[from].size()-1});
}
void bfs(int s)
{
    queue<int>P;
    memset(level,-1,sizeof(level));
    level[s]=0;P.push(s);
    while(!P.empty())
    {
        int v=P.front();P.pop();
        for(int i=0;i<G[v].size();i++)
        {
            node e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                P.push(e.to);
            }
        }
    }
}
int dfs(int v,int t,int f)
{
    if(v==t)return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        node &e=G[v][i];
        if(e.cap>0&&level[e.to]>level[v])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    while(1)
    {
        bfs(s);
        if(level[t]<0)return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,inf))>0)flow+=f;
    }
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        ll sum=0;
        for(int i=0;i<maxn;i++)G[i].clear();
        for(int i=1;i<=n;i++)
        {
            int x;scanf("%d",&x);
            add(i,en,x);
        }
        for(int i=1;i<=m;i++)
        {
            int x,y,z;scanf("%d%d%d",&x,&y,&z);
            sum+=z;
            add(st,n+i,z);
            add(n+i,x,z);
            add(n+i,y,z);
        }
        printf("%lld\n",sum-max_flow(st,en));
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值