首页 > 代码库 > AC日记——Destroying The Graph poj 2125
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.
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 +
Source
思路:
最小点权覆盖;
来,上代码:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#define INF 0x7ffffffusing 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(‘\n‘);check(s); for(int i=1;i<=n;i++) { if(!if_[i]) ans++; if(if_[i+n]) ans++; } cout<<ans;putchar(‘\n‘); for(int i=1;i<=n;i++) { if(!if_[i]) printf("%d -\n",i); if(if_[i+n]) printf("%d +\n",i); } return 0;}
AC日记——Destroying The Graph poj 2125
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。