Poj2125 destroying the graph bipartite graph + minimum point weight overwrite + minimum cut

Source: Internet
Author: User
Let's take a few more questions to deepen our understanding. The same is to obtain the least vertex weight overwrite, and also to obtain a minimum cut, but to cut out those sides, as long as the final remaining network can be traversed once, relatively simple. Creating a graph: it is also a bipartite graph. The vertices on the left represent removing the outbound edge, and the vertices on the right represent removing the inbound edge (Be careful not to mix them). The vertices on the left are connected to the source point, the capacity is wi -. The point on the right is connected to the sink, and the capacity is wi +. Then link the data to the given arc. The weight is infdestroying the graph.
Time limit:2000 ms   Memory limit:65536 K
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
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 in 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 +
#include<iostream>#include<cstdio>#include<algorithm> using namespace std;#define mm(a) memset((a),0,sizeof((a)))#define INF 0xFFFFFF#define MAXN 255int 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.