"BZOJ-2400" spoj839optimal Marks min cut + DFS

Source: Internet
Author: User

2400:SPOJ 839 Optimal Marks time limit:10 Sec Memory limit:128 MB
submit:567 solved:202
[Submit] [Status] [Discuss] Description defines the value of an edge in an image without a direction: An XOR value for the value of two points connected by this edge. Defines the value of an none-graph: The and of the values of all sides of this non-graph. Give you an no-show graph with n nodes m edges. The value of some of these points is given, and the value of the remaining points is determined by you (but the requirements are non-negative), so that the value of this graph is minimized. Under the premise of the least value of the non-direction graph, the value and the smallest of the values in the graph are made. Input first line, two number n,m, indicates the number of points and sides of a graph. Next n rows, one number per line, and the value of each point by number (if negative, the value of this point is determined by you, the value of the absolute size does not exceed 10^9). The next M-line, two numbers per line, a, a, represents an edge between two points numbered A and B. (Guaranteed no heavy side with self-loop.)    Output the first line, a number that represents the value of the non-graph. The second row, a number, represents the and of the values that are somewhat in the non-graph. Sample Input3 2
2
-1
0
1 2
2 3
Sample Output2
2
HINT

Data conventions
n<=500,m<=2000

Sample explanation
The value of 2 nodes is set to 0.

SourceSolution

At first see may be helpless, but see and XOR, you can consider decomposition into a binary each, then the practice has

Disassembled into binary to look at each bit, to establish a minimum cut model, s-->0;1-->t, is obviously the INF, then even the additional edge is set to 1; minimum cut

The second question, find the point of S set can, then search directly, add into the answer

Code
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<queue>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 20000#defineMAXM 2000100intN,M,VAL[MAXN],VAL[MAXN];structedgenode{intNext,to,cap;} edge[maxm<<1];intHead[maxn],cnt=1;voidAddintUintVintW) {cnt++;edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt;edge[cnt].cap=W;}voidInsertintUintVintW) {Add (u,v,w); Add (V,u,0);}intdis[maxn],que[maxn<<1],cur[maxn],s,t;BOOLBFs () { for(intI=s; i<=t; i++) dis[i]=-1; que[0]=s; dis[s]=0;intHe=0, Ta=1;  while(he<ta) {            intnow=que[he++];  for(intI=head[now]; I I=edge[i].next)if(Edge[i].cap && dis[edge[i].to]==-1) dis[edge[i].to]=dis[now]+1, que[ta++]=edge[i].to; }    returndis[t]!=-1;}intDfsintLocintLow ) {    if(loc==t)returnLow ; intW,used=0;  for(intI=cur[loc]; I I=edge[i].next)if(Edge[i].cap && dis[edge[i].to]==dis[loc]+1) {W=dfs (Edge[i].to,min (low-used,edge[i].cap)); Edge[i].cap-=w; edge[i^1].cap+=W; Used+=w;if(EDGE[I].CAP) cur[loc]=i; if(Used==low)returnLow ; }    if(!used) dis[loc]=-1; returnused;}#defineINF 0x7fffffffintDinic () {inttmp=0;  while(BFS ()) { for(intI=s; i<=t; i++) cur[i]=Head[i]; TMP+=DFS (S,inf); }    returntmp;}intU[MAXN],V[MAXN];voidBuild (intx) {CNT=1; Memset (Head,0,sizeof(head));  for(intI=1; i<=n; i++)        if(val[i]>=0)            if(val[i]&x) Insert (I,t,inf); ElseInsert (S,i,inf);  for(intI=1; i<=m; i++) Insert (U[i],v[i],1), insert (V[i],u[i],1);}BOOLVISIT[MAXN];voidDFS (intx) {Visit[x]=1;  for(intI=HEAD[X]; I I=edge[i].next)if(edge[i^1].cap &&!Visit[edge[i].to]) DFS (edge[i].to);}Long LongAns,ans;intMain () {//freopen ("graph.in", "R", stdin);//freopen ("Graph.out", "w", stdout);N=read (); M=read (); s=0, t=n+1;  for(intI=1; i<=n; i++) val[i]=read ();  for(intI=1; i<=m; i++) U[i]=read (), v[i]=read ();  for(intI=0; i<= -; i++) {Build (1<<i); Ans+=(Long Long)(1<<i) *Dinic (); memset (Visit,0,sizeof(visit));            DFS (T);  for(intj=1; j<=n; J + +)if(Visit[j]) val[j]+= (1<<i); }     for(intI=1; i<=n; i++) ans+=val[i]>0?Val[i]:val[i]; printf ("%lld\n%lld\n", Ans,ans); return 0;}

"BZOJ-2400" spoj839optimal Marks min cut + DFS

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.