OPEN-PIT MINING

题目链接点击打开链接

Description

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.

Input

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

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

Sample Input

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

Sample Output

2

Hint

Source
rmc17

思路

最大权闭合子图问题,设置一个超级源点,超级汇点。从源点到每个矿建一条边,边的权值为矿的价值,从矿到汇点建立一条边,边的权值是矿的cost。问题就转化为所有的矿总值减去最小割的值。对最大权闭合子图不懂的可以参考胡伯涛的最小割模型在信息学竞赛中的应用,里面讲的很详细。可以做一下2018 中国大学生程序设计竞赛宁夏网络赛CLEVER KING

代码

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define pa pair<int,int>
#define maxn 300
#define maxm 42000
#define inf 0x3fffffff
#define ll long long

using namespace std;
struct edge_type
{
    int next,to,v;
}e[maxm];
int head[maxn],cur[maxn],dis[maxn];
int n,s,t,cnt=1,x,y,z;
ll ans=0;
inline int read()  //读入一个整数
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}  //f记录正负号
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

inline void add_edge(int x,int y,int v)
{
    e[++cnt]=(edge_type){head[x],y,v};head[x]=cnt;
    e[++cnt]=(edge_type){head[y],x,0};head[y]=cnt;
}

inline bool bfs()  //返回的是是否存在从源点到汇点的流
{
    queue<int>q;
    memset(dis,-1,sizeof(dis));
    dis[s]=0;q.push(s);
    while(!q.empty())
    {
        int tmp=q.front();q.pop();
        if (tmp==t) return true;
        for(int i=head[tmp];i;i=e[i].next)
            if (e[i].v&&dis[e[i].to]==-1)  //下一个点没访问过
            {
                dis[e[i].to]=dis[tmp]+1;  //距源点的距离
                q.push(e[i].to);
            }
    }
    return false;
}
inline int dfs(int x,int f)
{
    int tmp,sum=0;
    if (x==t) return f;  //已经到终点了
    for(int &i=cur[x];i;i=e[i].next)
    {
        int y=e[i].to;
        if (e[i].v&&dis[y]==dis[x]+1)
        {
            tmp=dfs(y,min(f-sum,e[i].v));
            e[i].v-=tmp;e[i^1].v+=tmp;sum+=tmp;  //这里开始的时候弄了正反边,cnt从2开始
            if (sum==f) return sum;
        }
    }
    if (!sum) dis[x]=-1;  //这条路不用走了,走不通了
    return sum;
}
inline void dinic()
{
    while (bfs())  //存在从源到汇的流
    {
        F(i,1,t) cur[i]=head[i];
        ans-=dfs(s,inf);
    }
}
void init(){
    memset(e,0,sizeof(e));
    memset(head,0,sizeof(head));
    memset(cur,0,sizeof(cur));
    memset(dis,0,sizeof(dis));
    ans =0;
    cnt=1;
}
int main()
{
    freopen("b.txt", "r", stdin);
    int num;
    init();
    n=read(); //输入n个产品,m个矿
    s=n+1;t=s+1;  //超级源点,超级汇点
    int numa,numb;
    F(i,1,n)
    {
        z=read();
        ans+=z;
        add_edge(s,i,z);  //对于每个盈利的,与源点连一条边,权值为价值
        z=read();
        add_edge(i,t,z);  //对于每个花钱的,与汇点连一条边
        numa=read();  //开采该矿能开哪些其他的矿
        F(j,1,numa){
            y=read();
            add_edge(y,i,inf); //开采i矿才能开采y矿
        }
    }
    dinic();
    printf("%lld\n",ans);

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值