spoj287 Classic Network flow problem, two points + network flow decision scheme

Source: Internet
Author: User

The main topic: There are n houses, M-bar side. The point of Number 1 is the network Center. Now give you K house, each house must be connected by a cable to the network center. There are two rules for wiring: 1. A network cable must be complete, with only one color. 2, the edge of all the network cable must be different colors, the edge is the edge of two houses, such as 3->2->1 on the two side of the request: the minimum number of colors. Look at the topic specifically.

Ideas:

Must be the maximum flow Yes, the key is to build the map.

A word on the Internet "the number of colors depends on the maximum amount of traffic in each edge of the network stream, so the capacity of the two edges and the network flow can be done." Feel a little far-fetched.

First of all, if you give each side the number of colors is infinite, then you will get a maximum of all the traffic, but this traffic is obviously not the answer we want, for example, the source point a total of seven streams, we can from s->1->2->t, or s->3->4- >t, then it is possible that the first pass through the seven stream and the second road one did not, in fact, the least number of colors is certainly smaller than 7, so the maximum flow algorithm is only said that there is a flow method to ensure that the maximum flow, and can not control the flow of the specific flow, the situation is many, of course, the minimum number of

But what we have to do is squeeze each side of the capacity, forcing the flow of diversion, think of the words should be let the flow more fully use capacity of the case better, then we two-point cap, so that the capacity of each side of the CAP, see in this capacity is not a solution to meet test instructions, if there is a description of this color is sufficient, Then we go down two points, find a smaller number of colors, if not exist that the maximum stream is less than k, then the color number is not enough, then upward, and finally get a minimum number of colors.

Before his own dinic bad use, tle to death, may be where did not write well? Had to go to Daniel Blog Isap a copy of the template, very useful, requisition O (∩_∩) o

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #define CLEAR (A  , x) memset (A, x, sizeof a) #define copy (A, b) memcpy (A, B, sizeof a) using namespace std;const int Maxe=1000000;const int maxn=100000;const int maxq=1000000;const int oo=0x3f3f3f3f;struct edge{int v;//arc tail int c;//capacity int n;//point to next arc from the same arc head} edge[maxe];//Edge Group int ADJ[MAXN], cnte;//forward star's header int Q[MAXQ], head, tail;//queue int D[MAXN], CUR[MAXN], PRE[MAXN], Num[maxn];int Sourse, sink, Nv;//sourse: Source point, Sink: Meeting Point, NV: number modified upper limit int n, m,k;int x[maxn],f[maxn],g[maxn];void addedge (int u, int v, int c)//    Add Edge {//forward edge edge[cnte].v = V;    EDGE[CNTE].C = c;//The capacity of the forward arc is c EDGE[CNTE].N = Adj[u];    Adj[u] = cnte++;//reverse Edge edge[cnte].v = u;    EDGE[CNTE].C = 0;//The capacity of the reverse arc is 0 EDGE[CNTE].N = adj[v]; ADJ[V] = cnte++;}    void Rev_bfs ()//reverse BFS designator {clear (num, 0);    Clear (D,-1),///1 D[sink] = 0;//Meeting point is the default number num[0] = 1;    Head = tail = 0;    q[tail++] = sink; while (head! = tail) {iNT U = q[head++];            for (int i = adj[u]; ~i; i = edge[i].n) {int v = EDGE[I].V;            if (~d[v]) continue;//already labeled d[v] = D[u] + 1;//label q[tail++] = v;        num[d[v]]++; }}}int Isap () {copy (cur, adj);//copy, current arc optimization rev_bfs ();//Only one label at a time is enough, the re-marking is done in the ISAP main function. int flow = 0, U = Pre[sour    SE] = sourse, I;    while (D[sink] < NV)//Longest is a chain, where the largest label will only be nv-1, if greater than or equal to the description of the middle of the fault.            {if (U = = sink)//If an augmented path has been found, modify the traffic along the augmentation path {int f = oo, neck;                    for (i = sourse; I! = sink; i = edge[cur[i]].v) {if (F > Edge[cur[i]].c) {            f = edge[cur[i]].c;//constantly updated traffic needs to be reduced neck = i;//record the fallback point in order to not go back to the starting point to re-find}  } for (i = sourse; I! = sink; i = edge[cur[i]].v)//Modify Traffic {edge[cur[i]].c-=                F            Edge[cur[i] ^ 1].c + = f;  } Flow + + f;//update          U = neck;//fallback} for (i = cur[u]; ~i; i = EDGE[I].N) if (d[edge[i].v] + 1 = = D[u] && edge[i        ].C) break;            if (~i)//If there is a viable augmented path, update {Cur[u] = i;//Modify Current arc pre[edge[i].v] = u;        u = edge[i].v;            } else//otherwise fallback, re-find the augmented path {if (0 = = (--num[d[u]))) Break;//gap clearance optimization, if there is a fault, you can know there will be no more augmentation road            int mind = NV; for (i = adj[u]; ~i, i = EDGE[I].N) {if (edge[i].c && mind > D[EDGE[I].V])//search can be augmented                The minimum label {Cur[u] = i;//modifies the current arc mind = D[EDGE[I].V];            }} D[u] = mind + 1;            num[d[u]]++; U = pre[u];//Fallback}} return flow;} void init ()//initial {clear (adj,-1); cnte = 0;}    int main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n,&m,&k);        sourse=0,sink=1;        nv=n+1; for (int i=1; i<=k;i++) {scanf ("%d", &x[i]);        } for (int i=1; i<=m; i++) {scanf ("%d%d", &f[i],&g[i]);        } int l=0,r=n;        int answer=0;            while (l<=r) {init (); for (int i=1; i<=k; i++) {Addedge (0,x[i],1);//starting from 0 to 2*k-1} int mid= (l+r            ) >>1;                for (int i=1; i<=m; i++) {Addedge (f[i],g[i],mid);            Addedge (G[i],f[i],mid);                } if (ISAP () ==k) {//printf ("--------%d\n", mid-1);                Answer=mid;            R=mid-1;        } else l=mid+1;    } printf ("%d\n", answer); } return 0;}


Copyright NOTICE: This article is the original blogger article, reproduced please indicate the source Http://blog.csdn.net/hitwhacmer1

spoj287 Classic Network flow problem, two points + network flow decision scheme

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.