HDU 6214 Smallest Minimum Cut 最小割(isap)

来源:互联网 发布:分级基金 知乎 编辑:程序博客网 时间:2024/05/17 13:41

Smallest Minimum Cut

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)


Problem Description
Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition of nodes set V into two parts such that s and t belong to different parts. The cut set is the subset of E with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.
 

Input
The input contains several test cases and the first line is the total number of cases T (1T300).
Each case describes a network G, and the first line contains two integers n (2n200) and m (0m1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n.
The second line contains two different integers s and t (1s,tn) corresponding to the source and sink.
Each of the next m lines contains three integers u,v and w (1w255) describing a directed edge from node u to v with capacity w.
 

Output
For each test case, output the smallest size of all minimum cuts in a line.
 

Sample Input
24 51 41 2 31 3 12 3 12 4 13 4 24 51 41 2 31 3 12 3 12 4 13 4 3
 

Sample Output
23
 

Source
2017 ACM/ICPC Asia Regional Qingdao Online
求边最少的最小割。   add(u , v , w * 3000 + 1)  ,

权重放大3000倍,+ 1  , 跑出的最小割一定删掉的边最小


import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.Arrays;import java.util.StringTokenizer;    public class Main {      public static void main(String[] args) {          new Task().solve();      }  }    class Task {      InputReader in = new InputReader(System.in) ;      PrintWriter out = new PrintWriter(System.out) ;        final int inf=0x3f3f3f3f;    final int maxn = 200 + 8 ;    final int maxm = 1000 * 2 + 8 ;    int[] head = new int[maxn] ;    int[] num = new int[maxn] ;    int[] d = new int[maxn] ;    int[] cur = new int[maxn] ;    int cnt,s,t ;    int[] q = new int[maxm*2] ;    int nv ;     int[] pre  = new int[maxn] ;     class Edge{          int v,cap;          int next;    }    Edge[] edge = new Edge[maxm];    void add(int u,int v,int cap){          edge[cnt] = new Edge() ;          edge[cnt].v=v;          edge[cnt].cap=cap;          edge[cnt].next=head[u];          head[u]=cnt++;                    edge[cnt] = new Edge() ;          edge[cnt].v=u;          edge[cnt].cap=0;          edge[cnt].next=head[v];          head[v]=cnt++;    }        void bfs(){          Arrays.fill(num, 0) ;          Arrays.fill(d, -1) ;          int f1=0,f2=0;          q[f1++] = t ;           d[t]=0;          num[0]=1;          while(f2 <= f1){              int u=q[f2++];              for(int i=head[u];i!=-1;i=edge[i].next){                  int v=edge[i].v;                  if(d[v]!=-1){                      continue;                  }                  d[v]=d[u]+1 ;                   num[d[v]]++ ;                   q[f1++]=v;              }          }      }          int isap(){          System.arraycopy(head, 0, cur , 0 , maxn) ;          bfs();          int flow = 0 ,u = pre[s] = s , i ;          while(d[s] < nv){               if(u == t){                  int f = inf , pos = -1 ;                  for(i = s ; i != t ; i = edge[cur[i]].v){                       if(f>edge[cur[i]].cap){                          f=edge[cur[i]].cap;                          pos=i;                      }                  }                  for(i = s ;i != t ; i=edge[cur[i]].v){                       edge[cur[i]].cap-=f;                      edge[cur[i]^1].cap+=f;                  }                  flow += f;                   u = pos;              }              for(i=cur[u] ; i != -1 ; i = edge[i].next){                  if(d[edge[i].v]+1==d[u]&&edge[i].cap>0){                      break;                  }              }              if(i != -1){                   cur[u]=i;                   pre[edge[i].v]=u;                   u=edge[i].v;               }              else{                  if(--num[d[u]]==0){                      break ;                   }                  int mind=nv;                  for(i=head[u];i!=-1;i=edge[i].next){                       if(edge[i].cap>0&&mind>d[edge[i].v]){                          cur[u]=i;                          mind=d[edge[i].v];                      }                  }                  d[u]=mind+1;                  num[d[u]]++;                  u=pre[u];              }          }          return  flow ;    }        void solve(){           int T = in.nextInt() ;        while(T-- > 0){            Arrays.fill(head, -1) ;             cnt=0;            int n = in.nextInt() ;            int m = in.nextInt() ;            s = in.nextInt() ;            t = in.nextInt() ;            nv = n+1;            while(m-- > 0){                add(in.nextInt(), in.nextInt(), in.nextInt()*3000 + 1) ;            }            out.println(isap() % 3000) ;        }        out.flush() ;      }  }    class InputReader {          public BufferedReader reader;          public StringTokenizer tokenizer;                public InputReader(InputStream stream) {              reader = new BufferedReader(new InputStreamReader(stream), 32768);              tokenizer = new StringTokenizer("");          }                private void eat(String s) {              tokenizer = new StringTokenizer(s);          }                public String nextLine() {               try {                  return reader.readLine();              } catch (Exception e) {                  return null;              }          }                public boolean hasNext() {              while (!tokenizer.hasMoreTokens()) {                  String s = nextLine();                  if (s == null)                      return false;                  eat(s);              }              return true;          }                public String next() {              hasNext();              return tokenizer.nextToken();          }                public int nextInt() {              return Integer.parseInt(next());          }                public int[] nextInts(int n) {              int[] nums = new int[n];              for (int i = 0; i < n; i++) {                  nums[i] = nextInt();              }              return nums;          }                public long nextLong() {              return Long.parseLong(next());          }                public double nextDouble() {              return Double.parseDouble(next());          }                public BigInteger nextBigInteger() {              return new BigInteger(next());          }            }      




阅读全文
0 0