poj2125 Destroying The Graph

125 篇文章 0 订阅

Description Alice and Bob play the following game. First, Alice draws
some directed graph with N vertices and M arcs. After that Bob tries
to destroy it. In a move he may take any vertex of the graph and
remove either all arcs incoming into this vertex, or all arcs outgoing
from this vertex. Alice assigns two costs to each vertex: Wi+ and Wi-.
If Bob removes all arcs incoming into the i-th vertex he pays Wi+
dollars to Alice, and if he removes outgoing arcs he pays Wi- dollars.
Find out what minimal sum Bob needs to remove all arcs from the graph.

Input Input file describes the graph Alice has drawn. The first line
of the input file contains N and M (1 <= N <= 100, 1 <= M <= 5000).
The second line contains N integer numbers specifying Wi+. The third
line defines Wi- in a similar way. All costs are positive and do not
exceed 106 . Each of the following M lines contains two integers
describing the corresponding arc of the graph. Graph may contain loops
and parallel arcs.

Output On the first line of the output file print W — the minimal
sum Bob must have to remove all arcs from the graph. On the second
line print K — the number of moves Bob needs to do it. After that
print K lines that describe Bob’s moves. Each line must first contain
the number of the vertex and then ‘+’ or ‘-’ character, separated by
one space. Character ‘+’ means that Bob removes all arcs incoming into
the specified vertex and ‘-’ that Bob removes all arcs outgoing from
the specified vertex.

把每个点的入点和出点分成两部中间连的边就是原图中的边,求最小割。
输出方案可以从原点出发dfs一遍得到割成的两个集合,集合之间的边就是要割掉的边。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int s=205,t=206,oo=0x3f3f3f3f;
int fir[210],que[210],vis[210],dep[210],ne[50010],to[50010],w[50010],f[210],
n,m,num;
void add(int u,int v,int x)
{
    num++;
    ne[num*2]=fir[u];
    fir[u]=num*2;
    to[num*2]=v;
    w[num*2]=x;
    ne[num*2+1]=fir[v];
    fir[v]=num*2+1;
    to[num*2+1]=u;
    w[num*2+1]=0;
}
bool bfs()
{
    int hd,tl,u,v;
    memset(dep,0,sizeof(dep));
    dep[s]=1;
    que[hd=tl=1]=s;
    while (hd<=tl)
    {
        u=que[hd++];
        for (int i=fir[u];i;i=ne[i])
            if (w[i]&&!dep[v=to[i]])
            {
                dep[v]=dep[u]+1;
                que[++tl]=v;
            }
    }
    return dep[t];
}
int dfs(int u,int lim)
{
    if (u==t) return lim;
    int ret=0,v,x;
    for (int i=fir[u];i&&ret<lim;i=ne[i])
        if (w[i]&&dep[v=to[i]]==dep[u]+1)
        {
            x=dfs(v,min(lim-ret,w[i]));
            w[i]-=x;
            w[i^1]+=x;
            ret+=x;
        }
    if (!ret) dep[u]=0;
    return ret;
}
void dfs(int u)
{
    int v;
    vis[u]=1;
    for (int i=fir[u];i;i=ne[i])
        if (w[i]&&!vis[v=to[i]])
            dfs(v);
}
int main()
{
    int u,v,x,ans=0,tot=0;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        add(i*2+1,t,x);
    }
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        add(s,i*2,x);
    }
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        add(u*2,v*2+1,oo);
    }
    while (bfs()) ans+=dfs(s,oo);
    printf("%d\n",ans);
    dfs(s);
    for (int i=fir[s];i;i=ne[i])
        if (!w[i]&&!vis[v=to[i]])
            f[++tot]=v;
    for (int i=fir[t];i;i=ne[i])
        if (!w[i^1]&&vis[v=to[i]])
            f[++tot]=v;
    printf("%d\n",tot);
    for (int i=1;i<=tot;i++)
        printf("%d %c\n",f[i]/2,(f[i]&1)?'+':'-');
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值