1575. [Neerc 2003][poj2125] Map destruction
★★★ 输入文件:destroyingthegraph.in 输出文件:destroyingthegraph.out 简单对比时间限制:1 s 内存限制:256 MB
"Title description"
Alice and Bob are playing the following games. First, Alice draws a forward graph with n vertices and m edges. Then Bob tries to destroy it. In one operation, he can find a point in the diagram and delete all of its incoming or all out edges.
Alice defines two values for each of the points: wi+ and wi-. If Bob deletes all incoming edges of point I, he's going to pay Alice for wi+, and if he removes all the edges, he needs to pay Alice the WI dollar.
Find the minimum cost that Bob needs to delete all the edges in the diagram.
"Input Format"
The input data describes the graph that Alice Drew.
The first line of the input file has a number of two n,m (1<=n<=100,1<=m<=5000). The second line has n integers that describe the wi+ of n points, and the same third row is the wi-of the N points. All fees are positive and do not exceed 10^6. The next m row has two numbers for each row, representing the corresponding edge in the graph.
"Output Format"
The output is an integer, which is the minimum cost of Bob.
"Sample Input"
3 61 2 34 2 11 21 13 21 23 12 3
"Sample Output"
5
Prompted
样例的一个方案是:删除点1,2所有的入边,删除点2所有的出边。输出格式和原题有所不同。原题要求输出方案。
Source
Northeastern Europe 2003,Northern Subregion (NEERC 2003)POJ 2125 Destroying the Graph
Exercises
Through the topic can be found that we are required to use the smallest point in the case of the selection of all sides, each edge can be selected by his two endpoints, and access points may be different, so you can find that this is a minimum point weight coverage.
Build Edge:
1> first set up the virtual source s and sinks T, each point is split into two, Ia,ib.
2> from S to ia with a flow of wi-edge, from IB to t even a flow of wi+ edge.
3> The edge of the original image from Ua to VB with an infinitely large flow.
Then the maximum flow can be run.
Code:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#define N#define M 5100#define INF 0X3FFFFFFFusing namespace STD;structedge{intV,next,cap;} edge[Ten*M];intn,m,num=-1, S,t,ans;intw1[n],w2[n],head[2*n],pre[2*n],gap[2*n],dis[2*n],cur[2*n];intIn () {intx=0;CharCh=getchar (); while(ch<' 0 '|| Ch>' 9 ') Ch=getchar (); while(ch>=' 0 '&& ch<=' 9 ') x=x*Ten+ch-' 0 ', Ch=getchar ();returnx;}voidAddintUintVintCAP) {edge[++num].v=v; edge[num].cap=cap; Edge[num].next=head[u]; Head[u]=num;}voidBuild () {s=0,t=n<<1|1; for(intI=1; i<=n; i++) W1[i]=in (), add (I+n,t,w1[i]), add (T,i+n,0); for(intI=1; i<=n; i++) W2[i]=in (), add (S,i,w2[i]), add (I,s,0); for(intI=1; i<=m; i++) {intU=in (), V=in (); Add (U,v+n,inf), add (V+n,u,0); }}voidISAP () {memset(Dis,0,sizeof(dis));memset(Gap,0,sizeof(GAP)); for(intI=0; i<=t; i++) Cur[i]=head[i];intu=s,maxn=0, K=inf,v; gap[0]=t+1; Pre[s]=s; while(dis[s]<=t) {BOOLf=0; while(!f) {f=1; for(intI=cur[u]; i!=-1; I=edge[i].next) {v=edge[i].v;if(edge[i].cap>0&& dis[u]==dis[v]+1) {k=min (K,EDGE[I].CAP); Pre[v]=u; Cur[u]=i; U=v;if(u==t) { for(U=pre[u]; v!=s; V=u,u=pre[u]) {edge[cur[u]].cap-=k; edge[cur[u]^1].cap+=k; } maxn+=k; K=inf; } f=0; Break; } } }intminn=t+1; for(intI=head[u]; i!=-1; I=edge[i].next) {v=edge[i].v;if(edge[i].cap>0&& Dis[v]<minn) Cur[u]=i,minn=dis[v]; } gap[dis[u]]--;if(!gap[dis[u]]) Break; dis[u]=minn+1; gap[dis[u]]++; U=pre[u]; } ANS=MAXN;}intMain () {memset(head,-1,sizeof(head)); N=in (); M=in (); Build (); ISAP ();printf("%d\n", ans);return 0;}
"Network Flow", "Minimum point weight Override" "Neerc 2003" "POJ2125" "cogs 1575" graph destruction