AC日记——Destroying The Graph poj 2125

Destroying The Graph
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8356   Accepted: 2696   Special Judge

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.

Sample Input

3 6
1 2 3
4 2 1
1 2
1 1
3 2
1 2
3 1
2 3

Sample Output

5
3
1 +
2 -
2 +

Source

 

思路:

  最小点权覆盖;

 

来,上代码:

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

#define INF 0x7ffffff

using namespace std;

struct EdgeType {
    int v,e,f;
};
struct EdgeType edge[5000<<5];

int n,m,vout[105],vin[105],s,t;
int head[505],deep[505],cnt=1,ans;

bool if_[505];

char Cget;

inline void in(int &now)
{
    now=0,Cget=getchar();
    while(Cget>'9'||Cget<'0') Cget=getchar();
    while(Cget>='0'&&Cget<='9')
    {
        now=now*10+Cget-'0';
        Cget=getchar();
    }
}

inline void edge_add(int u,int v,int f)
{
    edge[++cnt].v=v,edge[cnt].f=f,edge[cnt].e=head[u],head[u]=cnt;
    edge[++cnt].v=u,edge[cnt].f=0,edge[cnt].e=head[v],head[v]=cnt;
}

bool BFS()
{
    for(int i=s;i<=t;i++) deep[i]=-1;
    queue<int>que;que.push(s);deep[s]=0;
    while(!que.empty())
    {
        int now=que.front();
        for(int i=head[now];i;i=edge[i].e)
        {
            if(deep[edge[i].v]<0&&edge[i].f>0)
            {
                deep[edge[i].v]=deep[now]+1;
                if(edge[i].v==t) return true;
                que.push(edge[i].v);
            }
        }
        que.pop();
    }
    return false;
}

int flowing(int now,int flow)
{
    if(now==t||flow==0) return flow;
    int oldflow=0;
    for(int i=head[now];i;i=edge[i].e)
    {
        if(deep[edge[i].v]!=deep[now]+1||edge[i].f==0) continue;
        int pos=flowing(edge[i].v,min(flow,edge[i].f));
        flow-=pos;
        oldflow+=pos;
        edge[i].f-=pos;
        edge[i^1].f+=pos;
        if(flow==0) return oldflow;
    }
    if(oldflow==0) deep[now]=-1;
    return oldflow;
}

void check(int now)
{
    if_[now]=true;
    for(int i=head[now];i;i=edge[i].e)
    {
        if(!edge[i].f||if_[edge[i].v]) continue;
        check(edge[i].v);
    }
}

int main()
{
    in(n),in(m);
    s=0,t=n+n+1;
    for(int i=1;i<=n;i++)
    {
        in(vout[i]);
        edge_add(i+n,t,vout[i]);
    }
    for(int i=1;i<=n;i++)
    {
        in(vin[i]);
        edge_add(s,i,vin[i]);
    }
    int u,v;
    for(int i=1;i<=m;i++)
    {
        in(u),in(v);
        edge_add(u,v+n,INF);
    }
    while(BFS()) ans+=flowing(s,INF);
    cout<<ans;ans=0;
    putchar('
');check(s);
    for(int i=1;i<=n;i++)
    {
        if(!if_[i]) ans++;
        if(if_[i+n]) ans++;
    }
    cout<<ans;putchar('
');
    for(int i=1;i<=n;i++)
    {
        if(!if_[i]) printf("%d -
",i);
        if(if_[i+n]) printf("%d +
",i);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6505342.html