Algorithm review--Euler loop hybrid diagram (bzoj2095 binary + network flow)

Source: Internet
Author: User

Title: Description

Yyd in order to lose weight, he came to the thin sea, which is a huge sea, there are N islands in the sea, the island has a M-bridge connection between the two small islands will not have two bridges, and from an island can go to any other small island. Now Yyd want to ride a bike from the Island 1, ride every bridge, reach every small island, and then back to the island 1. PA in order to let yyd lose weight success, summon a strong wind, because it is the sea, the wind becomes very big, after each bridge has unavoidable wind obstruction yyd,yyd very DDT, so with bubble to bribe you, hope you can help him find a bear the biggest wind the smallest route.

Input

Input: The first behavior two spaces separated by the integer n (2<=n<=1000), M (1<=m<=2000), next read into m lines separated by a space of 4 integers, A, B (1<=a,b<=n,a<>b), C,d ( 1<=c,d<=1000), which indicates that the block I Bridge of line i+1 connects islets A and B, the wind from A to B is C, and the Wind from B to a is d.

Output

Output: If the weight loss plan cannot be completed, the output NIE, otherwise the first line of output to withstand the maximum wind force (to make it the smallest)

Sample Input4 4
1 2 2 4
2 3 3 4
3 4 4 4
4 1 5 4
Sample Output4
HINT

Note: Through the bridge to the Euler circuit

Exercises

After the two-point answer to the figure must be a non-directional edge + one-way edge of the mixed graph, the mixed graph of the Euler loop to find the following specific method:

The non-directed edges of the graph are arbitrarily oriented to calculate the degrees and degrees of each point. If there is an odd difference in the degree of access to a certain point, then there is definitely no Euler circuit. Because the Euler circuit requires each point into the degree = out, that is, the total number of degrees is even, there must be odd points can not have the Euler circuit.

Well, now the difference between the degrees and the degrees of each point is even. Then divide the even number by 2, which is X. That is, for each point, as long as the X-side change direction (in > Out is the change in, out > into the change), you can guarantee the = into. If each point is out = in, then it is clear that the graph has a Euler loop.

Now the question becomes: Which sides should I change, and what can I do to get each point out = in? Constructs a network flow model.

First of all, there is no way to change the direction of the side, to be useless, delete. Didn't you orient the non-directed side at first? What to do, the network is built into what, the edge of a long capacity limit of 1. Another new S and T. For Point u in > Out, connect Edge (U, t), Volume X, for Point v out > into, connecting Edge (S, v), Volume X (note different for different point x).
After that, look at whether all the edges from S are full stream. There is can have the Euler circuit, no is no. Which is the Euler circuit? Look at the flow value allocation, all traffic is not 0 (the upper limit is 1, the stream value is not 0 is 1) the side of the reverse, you can get each point in the degree = out of the Eulerian graph.
Because it is full flow, so each into > out of the point, there are x edges come in, will these come in the opposite side, OK, into = out. The same is the point for out > entry. So, what about the points that are not connected to s and T? and the condition of the S-connection is out of >, and the condition of the T-connection is into > out, then this neither and s also not connected to the point of the T, naturally early in the beginning already satisfied into = out. Then in the network flow process, these points belong to "intermediate point". We know that the middle point flow is not allowed to accumulate, so that how much to go out, after the reverse, nature remains balanced.
So, in this case, the Euler circuit problem of the mixed graph is solved.

Code:

MD Because the number of edges is initialized to 0, it's never been checked out. Pay attention to the details next time.

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<ctime>#include<cctype>#include<cstring>#include<string>#include<algorithm>using namespacestd;Const intn=1005;Const intm=4005;structnode{int  from, go; intVal1,val2;} EDGE[M];intn,m,tot,father[n],size[n],chu[n],ru[n],src,des,maxx,temp;intfirst[n],go[m*2],next[m*2],rest[m*2],lev[n],cur[n],totl;inlineintGETFA (inta) {  if(father[a]==a)returnA; Father[a]=GETFA (Father[a]); returnfather[a];} InlinevoidCOMBFA (intAintb) {  intHasGETFA (a); intfb=GETFA (b); if(fa!=FB) FATHER[FA]=fb,size[fb]+=SIZE[FA];} InlinevoidComb (intAintBintc) {next[++totl]=first[a],first[a]=totl,go[totl]=b,rest[totl]=C; next[++totl]=first[b],first[b]=totl,go[totl]=a,rest[totl]=0;} InlineBOOLBFs () { for(inti=src;i<=des;i++) cur[i]=first[i],lev[i]=-1; Static intque[n],tail,u,v; Que[tail=1]=src; LEV[SRC]=0;  for(intHead=1; head<=tail;head++) {u=Que[head];  for(intE=first[u];e;e=Next[e]) {      if(lev[v=go[e]]==-1&&Rest[e]) {Lev[v]=lev[u]+1; que[++tail]=v; if(V==des)return true; }    }  }  return false;} InlineintDinic (intUintflow) {  if(u==des)returnflow; intres=0, Delta,v;  for(int&e=cur[u];e;e=Next[e]) {    if(lev[v=go[e]]>lev[u]&&Rest[e]) {Delta=dinic (V,min (flow-res,rest[e])); if(Delta) {Rest[e]-=Delta; Rest[e^1]+=Delta; Res+=Delta; if(Res==flow) Break; }    }  }  if(flow!=res) lev[u]=-1; returnRes;} InlinevoidMaxflow () { while(BFS ()) Temp+=dinic (SRC,100000000);} InlineBOOLCheckintlimit) {memset (Chu,0,sizeof(CHU)); memset (Ru,0,sizeof(RU)); memset (First,0,sizeof(first)); SRC=0, des=n+1, maxx=0, temp=0, totl=1;  for(intI=1; i<=n;i++) Father[i]=i,size[i]=1;  for(intI=1; i<=m;i++)  {    if(Edge[i].val1<=limit&&edge[i].val2<=limit)//non-directed edge orientation{comb (edge[i]. from, Edge[i].go,1); Chu[edge[i]. from]++; Ru[edge[i].go]++; COMBFA (Edge[i].go,edge[i]. from); }    Else      if(edge[i].val1<=limit) {Chu[edge[i]. from]++; Ru[edge[i].go]++; COMBFA (Edge[i]. from, Edge[i].go); }      Else        if(edge[i].val2<=limit) {Chu[edge[i].go]++; Ru[edge[i]. from]++; COMBFA (Edge[i]. from, Edge[i].go); }        Else return false; }  if(SIZE[GETFA (1)]!=n)return false;  for(intI=1; i<=n;i++)  {     if(Chu[i]==ru[i])Continue; if(ru[i]<Chu[i]) {      if((Chu[i]-ru[i])%2==1)return false; ElseComb (Src,i, (Chu[i]-ru[i])/2), maxx+= ((Chu[i]-ru[i])/2); }    Else    {      if((Ru[i]-chu[i])%2==1)return false; ElseComb (I,des, (Ru[i]-chu[i])/2);  }} maxflow (); if(Temp!=maxx)return false; Else return true;} InlineBOOLCMP (node A,node b) {returna.val1<B.val1;}intMain () {//freopen ("a.in", "R", stdin);scanf"%d%d",&n,&m); inta,b,c,d,ans=0;  for(intI=1; i<=m;i++) {scanf ("%d%d%d%d",&a,&b,&c,&d); Edge[i]. from=A; Edge[i].go=b; Edge[i].val1=C; Edge[i].val2=D; }  intleft=1, right= +;  while(left<=Right ) {    intMid= (left+right)/2; if(Check (mid)) right=mid-1, ans=mid; ElseLeft=mid+1; }  if(!ans) cout<<"NIE"<<Endl; Elsecout<<ans<<Endl; return 0;}

Algorithm review--Euler loop hybrid diagram (bzoj2095 binary + network flow)

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.