图论(网络流,二分图最小点权覆盖):POJ 2125 Destroying The Graph
2016-07-07 21:53
621 查看
Destroying The Graph
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
Sample Output
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 +
#include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; const int maxn=1010; const int maxm=100010; const int INF=1000000000; int cnt,tot,fir[maxn],fron[maxn],dis[maxn]; int to[maxm],nxt[maxm],gap[maxn],path[maxn]; int cap[maxm];queue<int>q; struct Max_Flow{ void Init(int tot_=0){ tot=tot_;cnt=1; memset(fir,0,sizeof(fir)); memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); } void add(int a,int b,int c){ nxt[++cnt]=fir[a]; fir[a]=cnt; cap[cnt]=c; to[cnt]=b; } void addedge(int a,int b,int c){ add(a,b,c); add(b,a,0); } bool BFS(int s,int t){ dis[t]=1;q.push(t); while(!q.empty()){ int x=q.front();q.pop(); for(int i=fir[x];i;i=nxt[i]) if(!dis[to[i]]){ dis[to[i]]=dis[x]+1; q.push(to[i]); } } return dis[s]; } int Aug(int s,int t,int &p){ int f=INF; while(p!=s){ f=min(f,cap[path[p]]); p=to[path[p]^1]; }p=t; while(p!=s){ cap[path[p]]-=f; cap[path[p]^1]+=f; p=to[path[p]^1]; } return f; } int ISAP(int s,int t){ if(!BFS(s,t))return 0; for(int i=s;i<=t;i++)fron[i]=fir[i]; for(int i=s;i<=t;i++)gap[dis[i]]+=1; int p=s,ret=0; while(dis[s]<=tot){ if(p==t)ret+=Aug(s,t,p); for(int &i=fron[p];i;i=nxt[i]) if(cap[i]&&dis[p]==dis[to[i]]+1){ path[p=to[i]]=i; break; } if(!fron[p]){ if(--gap[dis[p]]==0) break; int Min=tot; for(int i=fir[p];i;i=nxt[i]) if(cap[i])Min=min(Min,dis[to[i]]); gap[dis[p]=Min+1]+=1;fron[p]=fir[p]; if(p!=s)p=to[path[p]^1]; } } return ret; } }isap; int n,m,top; int tag[maxn],st[maxn]; void DFS(int x){ tag[x]=1; for(int i=fir[x];i;i=nxt[i]) if(cap[i]&&!tag[to[i]])DFS(to[i]); } int main(){ scanf("%d%d",&n,&m); int s=0,t=2*n+1; isap.Init(t+1); for(int i=1,v;i<=n;i++){ scanf("%d",&v); isap.addedge(s,i,v); } for(int i=1,v;i<=n;i++){ scanf("%d",&v); isap.addedge(i+n,t,v); } for(int i=1,a,b;i<=m;i++){ scanf("%d%d",&a,&b); isap.addedge(b,a+n,INF); } printf("%d\n",isap.ISAP(s,t)); DFS(0); for(int i=1;i<=n;i++){ if(!tag[i]) st[++top]=i; if(tag[i+n]) st[++top]=i+n; } printf("%d\n",top); for(int i=1;i<=top;i++){ if(st[i]<=n) printf("%d +\n",st[i]); else printf("%d -\n",st[i]-n); } return 0; }
相关文章推荐
- java http 请求
- Servlet的Cookie与Seesion的介绍
- 在LoadRunner中设置HTTP请求time-out的时间
- http、TCP/IP协议与socket之间的区别
- 支持流媒体传输的网络协议
- HTTP——10 Status Code Definitions
- 使用caffe-future完成FCN网络的训练
- java基础学习总结——网络编程
- UNIX网络编程——基本SCTP套接字编程
- 利用http缓存数据
- 【Netty4.X】TCP粘包/拆包问题的解决办法(二)
- Linux网络编程
- http://www.ibm.com/developerworks/cn/java/j-lo-junit-src/
- http://my.oschina.net/pangyangyang/blog/144495
- Apache 部署https证书
- http://codeforces.com/gym/100803/attachments A题 Bit String Reordering
- 雨水泛洪和网络泛洪那个更可怕
- 雨水泛洪和网络泛洪那个更可怕
- okhttp3的简单使用
- 网络流(最大密集度子图,分数规划):UvaLive 3709 Hard Life