ZOJ 3792 Romantic Value minimum cut (minimum edge number at minimum cost)

Source: Internet
Author: User

Minimum Cut and minimum cost

Edge Weight c = c * 10000 + 1

Then run a minimum cut, flow/10000 is the cost flow % 10000 is the number of edges.

This is the case where the number of edges is the least ..

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; # define ll int # define N 500 # define M 205000 # define inf 107374182 # define inf64 1152921504606846976 struct Edge {ll from, to, cap, nex ;} edge [M * 2]; // note that this must be large enough. Otherwise, re will have reverse arc ll head [N], edgenum; void add (ll u, ll v, ll cap, ll rw = 0) {// if it is a directed edge, add (u, v, cap); if it is a undirected edge, add (u, v, cap, cap); Edge E = {u, v, cap, head [u]}; edge [edgenum] = E; head [u] = edgenum ++; edge E2 = {v, u, rw, head [v]}; edge [edgenum] = E2; head [v] = edgenum ++;} ll sign [N]; bool BFS (ll from, ll to) {memset (sign,-1, sizeof (sign); sign [from] = 0; queue
       
         Q; q. push (from); while (! Q. empty () {int u = q. front (); q. pop (); for (ll I = head [u]; I! =-1; I = edge [I]. nex) {ll v = edge [I]. to; if (sign [v] =-1 & edge [I]. cap) {sign [v] = sign [u] + 1, q. push (v); if (sign [to]! =-1) return true ;}}return false;} ll Stack [N], top, cur [N]; ll Dinic (ll from, ll) {ll ans = 0; while (BFS (from, to) {memcpy (cur, head, sizeof (head); ll u = from; top = 0; while (1) {if (u = to) {ll flow = inf, loc; // loc indicates the minimum cap edge in the Stack for (ll I = 0; I <top; I ++) if (flow> edge [Stack [I]. cap) {flow = edge [Stack [I]. cap; loc = I ;}for (ll I = 0; I <top; I ++) {edge [Stack [I]. c Ap-= flow; edge [Stack [I] ^ 1]. cap + = flow;} ans + = flow; top = loc; u = edge [Stack [top]. from;} for (ll I = cur [u]; I! =-1; cur [u] = I = edge [I]. nex) // cur [u] indicates the subscript if (edge [I] of the edge where the u is located. cap & (sign [u] + 1 = sign [edge [I]. to]) break; if (cur [u]! =-1) {Stack [top ++] = cur [u]; u = edge [cur [u]. to;} else {if (top = 0) break; sign [u] =-1; u = edge [Stack [-- top]. from ;}}return ans;} void init () {memset (head,-1, sizeof head); edgenum = 0;} int n, m, from,; # define dou struct int main () {int T; scanf ("% d", & T); ll I, j, u, v, d; while (T --) {init (); cin> n> m> from> to; ll all = 0; for (I = 1; I <= m; I ++) {cin> u> v> d; all + = d; d * dou + 1; add (u, v, d, d );} ll cost = Dinic (from, to); ll bian = cost % dou; cost = all-cost/dou; if (bian = 0) {puts ("Inf "); continue;} double ans = (double) cost/(double) bian; printf ("%. 2lf \ n ", ans);} return 0;}/* 99 2 0 1 2 4 5 1 41 2 11 3 12 4 23 4 22 3 1 */
       
      
     
    
   
  
 


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.