void QPBO<REAL>::Solve()
{
	Node* i;

	maxflow();

	if (stage == 0)
	{
		if (all_edges_submodular)
		{
			for (i=nodes[0]; i<node_last[0]; i++)
			{
				i->label = what_segment(i);
			}
			return;
		}

		TransformToSecondStage(true);
		maxflow(true);
	}

	for (i=nodes[0]; i<node_last[0]; i++)
	{
		i->label = what_segment(i);
		if (i->label == what_segment(GetMate0(i))) i->label = -1;
	}
}
Beispiel #2
0
int main () {
	 int cases = 0;
    int a,b,c,i,j;
	 scanf("%d",&V);
	 while( V > 0){
       scanf("%d %d %d",&s,&t,&E);
       s -=1;
		 t -=1;
	    memset(FLOW, 0, sizeof(FLOW));
	    memset(queue, 0, sizeof(queue));
	    memset(F, 0, sizeof(F));

	    int v1, v2, val;
	    int i;
	    for(i=0; i<E; i++) {
         scanf("%d %d %d",&v1,&v2,&val);
         FLOW[v2-1][v1-1] += val;
         FLOW[v1-1][v2-1] += val;
	   }

       maxflow();
       printf("Network %d\nThe bandwidth is %d.\n\n",++cases,fTotal);

       scanf("%d",&V);
	 }
	 return 0;
}
Beispiel #3
0
void solve() {
	int i,j;
	scanf("%d %d %d %d",&N,&s,&t,&L);
	if(N>MAX) printf("increase MAX to %d and recompile\n",n),exit(1);
	for(i=0;i<N;i++) for(j=0;j<N;j++) scanf("%d",&c[i][j]);
	if(L==1) {
		/* we can only connect source to sink */
		printf("%.3f\n",c[s][t]/8.);
	} else if(L==2) {
		/* we can connect source to sink, as well as having one intermediate node */
		for(i=c[s][t],j=0;j<N;j++) if(j!=s && j!=t) i+=c[s][j]<c[j][t]?c[s][j]:c[j][t];
		printf("%.3f\n",i/8.);
	} else {
		/* transform graph: split each node u, call them u' and u'' */
		n=N*2;
		for(i=0;i<n;i++) for(j=0;j<n;j++) f[i][j]=0;
		/* for each edge source->v, add edge source->v' */
		for(i=0;i<N;i++) if(c[s][i]) f[s*2][i*2]=c[s][i];
		/* for each edge v->sink, add edge v''->sink */
		for(i=0;i<N;i++) if(c[i][t]) f[i*2+1][t*2]=c[i][t];
		/* for each edge u->v where none are source or sink, add edge u'->v'' */
		for(i=0;i<N;i++) if(i!=s && i!=t) for(j=0;j<N;j++) if(j!=s && j!=t && c[i][j])
			f[i*2][j*2+1]=c[i][j];
		/* for each edge u that isn't source of sink, add edge u'->u'' with INF cap */
		for(i=0;i<N;i++) if(i!=s && i!=t) f[i*2][i*2+1]=INF;
		/* flow now returns the correct answer */
		printf("%.3f\n",maxflow(s*2,t*2)/8.);
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	FlowNetwork g(6);
	int E;
	cin >> E;
	for (int i = 0; i < E; i++)
	{
		int v, w;
		double we;
		cin >> v >> w >> we;
		FlowEdge eIn(v, w, we);
		g.addEdge(eIn);
	}
	g.print();
	FordFulkerson maxflow(g, 0, 5);
	cout << endl;
	for (FlowEdge e : g.allEdge())
		cout << e.from() << "-->" << e.to() << " capacity:" << e.capacityGet() << " flow:" << e.flowGet()<<endl;
	cout << endl << "Max flow value:" << maxflow.valueGet()<<endl;



	system("pause");
	return 0;
}
/*
void output()
{
  int ts , te ;
  for( int i = 1 ; i <= m ; ++ i )
    {
      if( node[ i ].f - node[ i ].c < 0 )
        { tx = }
      else
        }
}
*/
int main()
{
    init();

    printf( "%d\n" , maxflow( n , 1 , n ) );

    //output();

    return 0;
}
 bool build(int n,int m,double mid)
 {
     init(n);
     double flow = 0.0;
     for(int i = 1 ; i <= m; ++i)
     {
         edge & e = a[i];
         if(e.cap + EPS < mid) mcut[sz++] = e.num,flow += e.cap - m;
         else addedge(e.from, e.to,e.cap - m,e.num);
     }
     return dcmp(flow + maxflow(1,n)) > 0;
 }
Beispiel #7
0
int main(void)
{
    int edges[MAXEDGES*2], ne, nv;
    int capacity[MAXEDGES], flow[MAXEDGES], cut[MAXVERTICES];
    int source, sink, p, q, i, j, ret;

    /*
     * Use this algorithm to find a maximal complete matching in a
     * bipartite graph.
     */
    ne = 0;
    nv = 0;
    source = nv++;
    p = nv;
    nv += 5;
    q = nv;
    nv += 5;
    sink = nv++;
    for (i = 0; i < 5; i++) {
	capacity[ne] = 1;
	ADDEDGE(source, p+i);
    }
    for (i = 0; i < 5; i++) {
	capacity[ne] = 1;
	ADDEDGE(q+i, sink);
    }
    j = ne;
    capacity[ne] = 1; ADDEDGE(p+0,q+0);
    capacity[ne] = 1; ADDEDGE(p+1,q+0);
    capacity[ne] = 1; ADDEDGE(p+1,q+1);
    capacity[ne] = 1; ADDEDGE(p+2,q+1);
    capacity[ne] = 1; ADDEDGE(p+2,q+2);
    capacity[ne] = 1; ADDEDGE(p+3,q+2);
    capacity[ne] = 1; ADDEDGE(p+3,q+3);
    capacity[ne] = 1; ADDEDGE(p+4,q+3);
    /* capacity[ne] = 1; ADDEDGE(p+2,q+4); */
    qsort(edges, ne, 2*sizeof(int), compare_edge);

    ret = maxflow(nv, source, sink, ne, edges, capacity, flow, cut);

    printf("ret = %d\n", ret);

    for (i = 0; i < ne; i++)
	printf("flow %d: %d -> %d\n", flow[i], edges[2*i], edges[2*i+1]);

    for (i = 0; i < nv; i++)
	if (cut[i] == 0)
	    printf("difficult set includes %d\n", i);

    return 0;
}
int main()
{
	int n, m;
	while (EOF != scanf("%d%d", &n, &m)) {
		initNetwork(n+2);
		for (int i = 1, a, b; i <= n; ++i) {
			scanf("%d%d", &a, &b);
			addEdge(0, i, a, 0);
			addEdge(i, n+1, b, 0);
		}
		for (int i = 0, u, v, w; i < m; ++i) {
			scanf("%d%d%d", &u, &v, &w);
			addEdge(u, v, w, w);
		}
		printf("%d\n", maxflow(n+2, 0, n+1));
	}
	return 0;
}
Beispiel #9
0
int main() {
	int n, m, s, d;
	while (EOF != scanf("%d%d%d%d", &n, &m, &s, &d)) {
		--s, --d;
		initNetwork(n+n);
		for (int i = 0, w; i < n; ++i) {
			scanf("%d", &w);
			addEdge(i, i+n, w);
		}
		for (int i = 0, a, b; i < m; ++i) {
			scanf("%d%d", &a, &b);
			--a, --b;
			addEdge(a+n, b, INF);
			addEdge(b+n, a, INF);
		}
		printf("%d\n", maxflow(n+n, s, d+n));
	}
	return 0;
}
int main()
{
	int i,j,x,y,z;
	while(scanf("%d %d",&n,&m) == 2){
		for(i = 1; i <= n; i++)
			for(j = 1; j <= n; j++){
				net[i][j].c = 0;
				net[i][j].f = 0;
			}
			for(i = 1; i <= n; i++){
				scanf("%d %d %d",&x,&y,&z);
				net[x][y].c += z;      //注意建图时有无重复边。
			}
			s = 1; t = m;
			int out = maxflow();
			printf("%d\n",out);
	}
	return 0;
}
Beispiel #11
0
void fun(){
    int i,j,t;
    row = col = 0;
    
    memset(matrix,0,sizeof(char)*NodeNum*NodeNum);
    /* scan from col*/
    for(i=0;i<N;i++){		
		j = 0;
		while(j< N){
			while(bitmap[j][i] == -1 && j < N){
				j ++;
			}
			if(j==N)
				break;			
			while(bitmap[j][i] != -1 && j < N){
				bitmap[j][i] = col;
				j ++;
			}
			col++;
		}		
	}
	/* scan from row */
	for(i=0;i<N;i++){
		j = 0;
		while(j<N){
			while(bitmap[i][j] == -1 && j < N){
				j ++;
			}
			if(j==N)
				break;			
			while(bitmap[i][j] != -1 && j < N){
				t = bitmap[i][j];
				matrix[row][t] = 1;
				
				j ++;
			}
			row++;
		}
	}
    t = maxflow();
    printf("%d\n",t);
}
Beispiel #12
0
int main()
{
    freopen("test.in","r",stdin);
    freopen("sap.txt","w",stdout);
    int n,m,x,y,z,i,T,cas=0;
    scanf("%d",&T);
    while (scanf("%d%d", &n, &m) == 2)
    {
    N=n;
    for (i=1;i<=n;i++)
        head[i]=mark[i]=-1;
    for (i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        add_edge(x,y,z);
    }
    printf("%d\n",cas,maxflow(1,n));
    }
    return 0;
}
Beispiel #13
0
Datei: p.c Projekt: DavidToca/acm
int main()
{
	int i, j, n, m, x, y;

	while (scanf("%d %d", &n, &m) == 2) {
		nvert = 2 * n;
		memset(adj, 0, sizeof(adj));

		for (i = 0; i < n; i++)
			adj[V_IN(i)][V_OUT(i)] = 1;

		for (j = 0; m-- > 0 && scanf(" ( %d , %d )", &x, &y) == 2;) {
			if (x != y && adj[V_OUT(x)][V_IN(y)] == 0) {
				adj[V_OUT(x)][V_IN(y)] = 1;
				adj[V_OUT(y)][V_IN(x)] = 1;
				j++;
			}
		}

		if (n == 0 || j >= (n * (n - 1) / 2)) {
			printf("%d\n", n);
			continue;
		}

		for (m = -1, x = 0; x < n; x++) {
			for (y = x + 1; y < n; y++) {
				i = maxflow(V_OUT(x), V_IN(y));
				if (m < 0 || i < m)
					m = i;
			}
		}

		printf("%d\n", (m < 0) ? 0 : m);
	}

	return 0;
}
void solve()
{
    int ans = INF;
    int left = 1, right = 500 * n;
    int mid;
    while(left <= right)
    {
        mid = (left + right) >> 1;
        make(mid);
        if(maxflow(0, n+1) == c)
        {
            right = mid - 1;
            if(mid < ans)
            {
                ans = mid;
            }
        }
        else
        {
            left = mid + 1;
        }
    }
    printf("%d\n",ans);
}
/* lmaxflow
 	A discrete maximum flow algorithm
*/
int lmaxflow(
    DBL_TYPE * g,				/* The isotropic but non-homogeneous metric */
    int * dim_buf,				/* The image dimensions */
    int dim_length,				/* The number of image dimesions */
    DBL_TYPE * dbl_type,		/* Label image of node types */
    DBL_TYPE * out				/* The output */
)
{
    int i, num_pixels;
    BVECT * dim;
    int return_val;
    BIMAGE * g_bimage, * out_bimage;
    char * type;

    /* Set up a BVECT to describe the image dimensions, and the source vector */
    dim = BVECT_constructor(dim_length);
    memcpy(dim->buf, dim_buf, dim_length*sizeof(int));

    /* Compute the number of pixels for reuse later */
    num_pixels = BVECT_prod(dim);

    /* Allocate a node type array and convert from input type */
    type = (char *)malloc(num_pixels * sizeof(char));
    for (i = 0; i < num_pixels; i++) {
        type[i] = (char)dbl_type[i];
    }

    /* Construct the necessary BIMAGEs */
    g_bimage = BIMAGE_constructor_double(g, dim);
    out_bimage = BIMAGE_constructor(dim);

    /* Compute a maximum flow */
    return_val = maxflow(
                     g_bimage,
                     type,
                     out_bimage,
                     ((BIMAGE * *)NULL)
                 );
    /*  thisa function does not return the mincut, and is only for timings */
    /*
    	return_val = maxflowBOOST(
    		g_bimage,
    		type,
    		out_bimage,
    		((BIMAGE * *)NULL)
    	);
    */

    /* Copy output buffer across */
    for (i = 0; i < num_pixels; i++) {
        out[i] = (DBL_TYPE)out_bimage->buf[i];
    }

    /* Display any LSTB_error or LSTB_debug messages */
    lreadLSTBmsgs();

    /* Free everything */
    BVECT_destructor(dim);
    BIMAGE_destructor(g_bimage);
    BIMAGE_destructor(out_bimage);
    free((void *)type);

    return return_val;
}
Beispiel #16
0
SCIP_Bool ghc_tree (GRAPH *gr, SCIP_Bool** cuts, int* ncuts, double minviol)
{
   /* Determines Gomory/Hu cut tree for input graph with
      capacitated edges, the tree structures is represented
      by parent pointers which are part of the node structure,
      the capacity of a tree edge is stored at the child node,
      the root of the cut tree is the first node in the list
      of graph nodes (&gr->nodes[0]). The implementation is
      described in [1].
     
      References:
      ----------
      1) D. Gusfield: "Very Simple Algorithms and Programs for
      All Pairs Network Flow Analysis", Computer Science Di-
      vision, University of California, Davis, 1987.
     
      2) R.E. Gomory and T.C. Hu: "Multi-Terminal Network Flows",
      SIAM J. Applied Math. 9 (1961), 551-570.

   */

   GRAPHNODE *nptr, *nptr1, *nptrn, *sptr, *tptr;
   long n;
   double maxfl;

   n = gr->nnodes;
   
   if ( ! init_maxflow (n) )
      return FALSE;

   nptr1 = gr->nodes;
   nptrn = &(gr->nodes[n-1L]);
   for ( nptr = nptrn; nptr >= nptr1; nptr-- )
      nptr->parent = nptr1;

   for ( sptr = &(gr->nodes[1L]); sptr <= nptrn; sptr++ )
   { 
      tptr = sptr->parent;
      maxfl = maxflow(gr, sptr, tptr);

      //maxfl < 0 <=> graph is not connected => generate cut
      if ( maxfl < 0L )
      {
         constructSingleCut(gr,cuts);
         *ncuts = 1;
	 fini_maxflow ();
         return TRUE;
      }
           
      sptr->mincap = maxfl;
      for ( nptr = &(gr->nodes[1L]); nptr <= nptrn; nptr++ )
         if ( nptr != sptr && ! nptr->alive && nptr->parent == tptr )
            nptr->parent = sptr;
      if ( ! tptr->parent->alive )
      { 
         sptr->parent = tptr->parent;
         tptr->parent = sptr;
         sptr->mincap = tptr->mincap;
         tptr->mincap = maxfl;
      }
   }
   fini_maxflow ();
   constructCutList(gr,cuts,ncuts,minviol);
   
   return TRUE;
} /* ghc_tree */