二分图最小点权覆盖(输出覆盖的点)poj2025

来源:互联网 发布:吉利知豆好不好 编辑:程序博客网 时间:2024/06/05 20:45

Language:
Destroying The Graph
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7319 Accepted: 2321 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 61 2 34 2 11 21 13 21 23 12 3

Sample Output

531 +2 -2 +

转化成最小点权覆盖见图的方法,参见论文《最小割模型在信息学竞赛中的应用》

这里想说一下如何输出这些点,前几天在uva上遇到过这种题,没仔细看,现在总结一下

首先从X的未匹配点出发dfs直到不能再走,这样X集合未标记的点和Y集合标记了的点就是可以覆盖所有边的点

uva11419

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=210;const int INF=1000000000;int A[maxn],B[maxn];int N,M,s,t,nn;int vis[maxn],pre[maxn],cnt;int dis[maxn],gap[maxn],cur[maxn],head[maxn],biao[maxn];//vector<int> ans;struct node{    int u,v,f,next;}edge[15000];void add_edge(int x,int y,int f){    edge[cnt].u=x;    edge[cnt].v=y;    edge[cnt].f=f;    edge[cnt].next=head[x];    head[x]=cnt++;    edge[cnt].u=y;    edge[cnt].v=x;    edge[cnt].f=0;    edge[cnt].next=head[y];    head[y]=cnt++;}int SAP(int st,int en){    for(int i=0;i<=nn;i++)    {        cur[i]=head[i];        dis[i]=gap[i]=0;    }    int u=0;    int flow=0,aug=INF;    gap[st]=nn;    u=pre[st]=st;    bool flag;    while(dis[st]<nn)    {        flag=0;        for(int &j=cur[u];j!=-1;j=edge[j].next)        {            int v=edge[j].v;            if(edge[j].f>0&&dis[u]==dis[v]+1)            {                flag=1;                if(edge[j].f<aug)aug=edge[j].f;                pre[v]=u;                u=v;                if(u==en)                {                    flow+=aug;                    while(u!=st)                    {                        u=pre[u];                        edge[cur[u]].f-=aug;                        edge[cur[u]^1].f+=aug;                    }                    aug=INF;                }                break;            }        }        if(flag)continue;        int mindis=nn;        for(int j=head[u];j!=-1;j=edge[j].next)        {            int v=edge[j].v;            if(dis[v]<mindis&&edge[j].f>0)            {                mindis=dis[v];                cur[u]=j;            }        }        if((--gap[dis[u]])==0)break;        gap[dis[u]=mindis+1]++;        u=pre[u];    }    return flow;}void DFS(int u){    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(biao[v])continue;        if(edge[i].f>0)        {            biao[v]=1;            DFS(v);        }    }}int main(){    while(scanf("%d%d",&N,&M)!=EOF)    {        for(int i=1;i<=N;i++)scanf("%d",&A[i]);        for(int i=1;i<=N;i++)scanf("%d",&B[i]);        s=0,t=N*2+1,nn=t+1;        cnt=0;        memset(head,-1,sizeof(head));        for(int i=1;i<=N;i++)        {            add_edge(s,i,B[i]);            add_edge(i+N,t,A[i]);        }        while(M--)        {            int u,v;            scanf("%d%d",&u,&v);            add_edge(u,v+N,INF);        }        memset(biao,0,sizeof(biao));        int ans=SAP(s,t);        DFS(s);        printf("%d\n",ans);        ans=0;        for(int i=1;i<=2*N;i++)        {            if(!biao[i]&&i<=N)ans++;            else if(biao[i]&&i>N)ans++;        }        printf("%d\n",ans);        for(int i=1;i<=N;i++)        {            if(!biao[i])printf("%d -\n",i);            if(biao[i+N])printf("%d +\n",i);        }    }    return 0;}




0 0
原创粉丝点击