15 more schools whu Harry Potter and the Forbidden Forest (calculate the minimum number of edges for the minimum cut of the Network)

Source: Internet
Author: User

Harry Potter wants to stop the zombie from reaching n-1 at, so he wants to destroy several paths, each of which consumes a certain amount of magic. He wants to know that at least magic, at least damage.

(Incorrect thinking) I want to use the charge stream to solve the game. The cost value for each edge of the original side is 1, and the minimum cost after the flow is the minimum cut count, however, the result is not found after running because the cost is not the cost of edge Cutting for all expenses of the current stream...

Correct Solution:

(Theorem? (In this case, the problem solution report uses the least cut method.) in the network, any stream F is the largest stream, all the possible minimum cut edges of the network must be included in some full-stream edges of the stream F.

The proof is not strict (if you have a problem with your own YY, hope that the passing Daniel will give you some advice ):

Assume that an edge with the minimum cut is not in the full edge of the F instance. Set this edge to E1 and the capacity to C1, the traffic of stream F on this edge is F1 (C1> F1). Because F is the largest stream, this cut is the smallest cut. According to the famous theorem of the smallest cut of the largest stream, if the traffic of F is equal to the cut capacity, F will have at least one feasible Stream Containing the edge not where E1 is located, and the stream does not contain other edges of the cut, this feasible stream is independent of the Connection source sink point path of the original cut, which is in conflict with the definition of the minimum cut.

According to the above non-logical theorem, there is a solution to the problem report. First, find the maximum stream of the original network, and then assign a value to 1 to the edge of the full stream of the residual network, when the edge value of a non-full stream is INF, the maximum stream is obtained for the new network. The value of this stream is the minimum number of edges for the minimum cut.

 

Method 1:

 

#include <cstdio>#include <cstring>const int maxn=1050;const int inf=1<<25;const int s=0;int L , W , N , P;struct edge{       int v,next,w;}edge[500005];int head[maxn],cnt;//for sapvoid addedge(int u, int v, int w){     edge[cnt].v=v;     edge[cnt].w=w;     edge[cnt].next=head[u];     head[u]=cnt++;     edge[cnt].v=u;     edge[cnt].w=0;     edge[cnt].next=head[v];     head[v]=cnt++;}int sap(int t){    int pre[maxn],cur[maxn],dis[maxn],gap[maxn];    int flow=0 , aug=inf ,u;    bool flag;    for (int i=0 ; i<=t ; ++i)    {        cur[i]=head[i];        gap[i]=dis[i]=0;    }    gap[s]=t+1;    u=pre[s]=s;    while (dis[s]<=t)    {          flag=0 ;          for (int &j=cur[u] ; ~j ; j=edge[j].next)          {              int v=edge[j].v;              if (edge[j].w>0 && dis[u]==dis[v]+1)              {                   flag=1;                   if(edge[j].w<aug)aug=edge[j].w;                   pre[v]=u;                   u=v;                   if (u==t)                   {                       flow+=aug;                       while (u!=s)                       {                             u=pre[u];                             edge[cur[u]].w-=aug;                             edge[cur[u]^1].w+=aug;                       }                       aug=inf;                   }                   break;              }          }          if (flag)continue ;          int mindis=t+1;          for (int j=head[u]; ~j ; j=edge[j].next)          {              int v=edge[j].v;              if (edge[j].w>0 && dis[v]<mindis)              {                 mindis=dis[v];                 cur[u]=j;              }          }          if(--gap[dis[u]]==0)break;          gap[dis[u]=mindis+1]++;          u=pre[u];    }    return flow;}/*void build_graph(){}*/void init (){     memset (head , -1 , sizeof(head));     cnt=0;}int main (){    int cas;    scanf("%d",&cas);    int n,m;    int u,v,w,d;    for (int I=1 ; I<=cas ; ++I)    {        scanf("%d%d",&n,&m);        init ();        for (int i=0 ; i<m ; ++i)        {            scanf("%d%d%d%d",&u,&v,&w,&d);            if(d)addedge(v,u,w);            addedge(u,v,w);        }        sap(n-1);        for (int i=0 ; i<cnt ; i+=2)        {            if(edge[i].w==0)            {                edge[i].w=1;                edge[i^1].w=0;            }            else            {                edge[i].w=inf;                edge[i^1].w=0;            }        }        int ans=sap(n-1);        printf("Case %d: %d\n",I,ans);    }    return 0;}

On the Internet, I saw another very powerful method. I added a weight to each edge, and the original capacity C was changed to C * (m + 1) + 1.

The minimum cut is Cmin = C' min/(m + 1), and the minimum number of edges is C' min % (m + 1 );

 

You can find the maximum number of edges for the minimum cut with a slight modification.

Core code

For (INT I = 0; I <m; ++ I) {scanf ("% d % i64d % d", & U, & V, & W, & D); If (d) addedge (v, U, w * m + 1ll + W); addedge (U, V, W * m + 1ll + W );} int ans = (SAP (n-1) + m + 1) % (m + 1); // int ans = m + 1-(SAP (n-1) + m + 1) % (m + 1);/* calculate the maximum number of edges for the minimum cut, + 1 to-1 */printf ("case % d: % d \ n", I, ans );

 

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.