1. 程式人生 > >網路流專題 Open-Pit Mining (最大權閉合子圖)

網路流專題 Open-Pit Mining (最大權閉合子圖)

 Open-Pit Mining

時間限制: 1 Sec  記憶體限制: 128 MB
提交: 41  解決: 23
[提交] [狀態] [討論版] [命題人:admin]

題目描述

Open-pit mining is a surface mining technique of extracting rock or minerals from the earth by their removal from an open pit or borrow. Open-pit mines are used when deposits of commercially useful minerals or rocks are found near the surface. Automatic Computer Mining (ACM) is a company that would like to maximize
its profits by open-pit mining. ACM has hired you to write a program that will determine the maximum profit it can achieve given the description of a piece of land.
Each piece of land is modelled as a set of blocks of material. Block i has an associated value (vi), as well as a cost (ci), to dig that block from the land. Some blocks obstruct or bury other blocks. So for example if block i is obstructed by blocks j and k, then one must first dig up blocks j and k before block i can be dug up. A block can be dug up when it has no other blocks obstructing it.

輸入

The first line of input is an integer N (1≤N≤200) which is the number of blocks. These blocks are numbered 1 through N.
Then follow N lines describing these blocks. The ith such line describes block i and starts with two integers vi, ci denoting the value and cost of the ith block (0≤vi,ci≤200).
Then a third integer 0≤mi≤N-1 on this line describes the number of blocks that block i obstructs.
Following that are mi distinct space separated integers between 1 and N (but excluding i) denoting the label(s) of the blocks that block i obstructs.
You may assume that it is possible to dig up every block for some digging order. The sum of values mi over all blocks i will be at most 500.

輸出

Output a single integer giving the maximum profit that ACM can achieve from the given piece of land.

樣例輸入

5
0 3 2 2 3
1 3 2 4 5
4 8 1 4
5 3 0
9 2 0

樣例輸出

2

來源/分類

題目大意:給出一些礦井,有一個收益和花費,要開發當前礦井,必須要先開發擋住當前礦井的礦井。

由於開發每一個礦井有一個前提就是開發另外一些礦,如果把當前礦與其他礦連邊,就會發現符合閉合子圖的定義(如果選當前的點,他的所有出邊也要選。)

所以根據求最大權閉合子圖的方法,我們將所有權值為正的點與原點連邊,所有權值為負的點與匯點連線。

一個結論:權閉合子圖的最大權值為所有正權值的邊之和減去最小割。

選擇的方案就是割集S原點所在的集合。

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define sca(x) scanf("%d",&x)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x3f3f3f3f
#define LL long long
#define N 1600
#define inf 0x3f3f3f3f
 
int n;
struct edg
{
    int to,w,nt;
}g[N];
 
int dep[205],head[205];
bool bfs(int s,int t)
{
    memset(dep,0,sizeof(dep));
    dep[s]=1;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=g[i].nt)
        {
            int to=g[i].to;
            if(!dep[to]&&g[i].w>0)
            {
                dep[to]=dep[u]+1;
                q.push(to);
            }
        }
    }
    return dep[t]>0;
}
 
int dfs(int s,int flow)
{
    int now=0;
    if(s==n+1)return flow;
    for(int i=head[s];i!=-1;i=g[i].nt)
    {
        int to=g[i].to;
        if(dep[to]==dep[s]+1&&g[i].w>0
           &&(now=dfs(to,min(flow,g[i].w))))
        {
            g[i].w-=now;
            g[i^1].w+=now;
            return now;
        }
    }
    return 0;
}
 
int dinic(int s,int t)
{
    int ans=0;
    int d;
    while(bfs(s,t)){
        while(d=dfs(s,inf))
            ans+=d;
    }
    return ans;
}
 
int tot;
void addedg(int u,int v,int w)
{
    g[tot].to=v;
    g[tot].w=w;
    g[tot].nt=head[u];
    head[u]=tot++;
 
    g[tot].to=u;
    g[tot].w=0;
    g[tot].nt=head[v];
    head[v]=tot++;
}
 
int main()
{
    sca(n);
    int sum=0;
    tot=0;
    memset(head,-1,sizeof(head));
    rep(i,1,n)
    {
        int c,v,k;
        scanf("%d%d%d",&v,&c,&k);
        if(v-c>=0)addedg(0,i,v-c),sum+=v-c;
        else addedg(i,n+1,c-v);
        rep(j,1,k)
        {
            int tmp;
            sca(tmp);
            addedg(tmp,i,inf);
        }
    }
    printf("%d\n",sum-dinic(0,n+1));
}