趁热打铁,再来几题,加深理解。
一样是求最小点权覆盖,同样求一个最小割,但是要求出割去了那些边,只要用最终的剩余网络进行一次遍历就可以了,比较简单。
建图:同样是一个二分图,左边的点代表去掉出边,右边的点代表去掉入边(小心别弄混),左边去掉出边的点与源点相连,容量为wi- 。右边的点与汇点相连,容量为wi+。然后更据给出的弧进行连线,权值为INF
Destroying The Graph
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 5485 | Accepted: 1740 | 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 10
6 . 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<cstdio> #include<algorithm> using namespace std; #define mm(a) memset((a),0,sizeof((a))) #define INF 0xFFFFFF #define MAXN 255 int n,m,c[MAXN][MAXN],dis[MAXN],gap[MAXN]; int st,ed; bool visited[MAXN]; int stack[MAXN],top; int sap(int u,int flow) { if(u==ed) return flow; int ans=0,i,t; for(i=0;i<=n+1;++i) if(c[u][i]&&dis[u]==dis[i]+1) { t=sap(i,min(flow-ans,c[u][i])); c[u][i]-=t,c[i][u]+=t,ans+=t; if(ans==flow) return ans; } if(dis[st]>=n+2) return ans; if(!--gap[dis[u]]) dis[st]=n+2; ++gap[++dis[u]]; return ans; } void dfs(int p) { if(visited[p]) return ; visited[p]=true; for(int i=0;i<n+1;i++) if(!visited[i] && c[p][i]) dfs(i); } void solve() { int x,y,ans=0,temp; mm(dis),mm(gap),mm(c),mm(visited); for(int i=1;i<=n;i++) scanf("%d",&c[n+i][n*2+1]); for(int i=1;i<=n;i++) scanf("%d",&c[0][i]); for(int i=1;i<=m;i++) { scanf("%d%d",&x,&y); c[x][y+n]=INF; } n=n<<1; st=0,ed=n+1; for(gap[0]=n+2;dis[st]<n+2;) ans+=sap(st,INF); dfs(0); cout<<ans<<endl; top=0; n=n>>1; for(int i=1;i<=n;i++) { if(!visited[i]) stack[top++]=i; if(visited[i+n]) stack[top++]=i+n; } cout<<top<<endl; while(top>0) { temp=stack[--top]; if(temp<=n) cout<<temp<<" -"<<endl; else cout<<temp-n<<" +"<<endl; } } int main() { while(scanf("%d%d",&n,&m)!=EOF) solve(); return 0; }