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 (1≤T≤300) .
Each case describes a networkG , and the first line contains two integers n (2≤n≤200) and m (0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n .
The second line contains two different integerss and t (1≤s,t≤n) corresponding to the source and sink.
Each of the nextm lines contains three integers u,v and w (1≤w≤255) describing a directed edge from node u to v with capacity w .
Each case describes a network
The second line contains two different integers
Each of the next
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
- HDU 6214 Smallest Minimum Cut 最小割(isap)
- HDU 6214 Smallest Minimum Cut 【最小割】
- HDU-6214 Smallest Minimum Cut(最小割)
- HDU 6214 Smallest Minimum Cut (最小割)
- Hdu 6214 Smallest Minimum Cut(最小割)
- HDU 6214 Smallest Minimum Cut (最小割最小割边)(两种算法的分析)
- HDU 6214 Smallest Minimum Cut(最小割的最少割边数)
- Hdu 6214 Smallest Minimum Cut【Dinic-最大流最小割】
- HDU 6214 Smallest Minimum Cut(网络流 最小割最少边数)
- HDU 6214 Smallest Minimum Cut(求最小割的最少边数)
- hdu-6214 Smallest Minimum Cut(最小割的最少边)
- HDU 6214 Smallest Minimum Cut 2017青岛网赛1009(最小割最小割边)
- hdu Smallest Minimum Cut 边数最少的最小割
- HDU 6214 Smallest Minimum Cut【最小割的最小边数】
- 2017 ACM/ICPC Asia Regional Qingdao Online HDU 6214 Smallest Minimum Cut(最小割的边数)
- HDU 6214 Smallest Minimum Cut
- HDU6214 Smallest Minimum Cut【最小割-最小边数】
- hdu6214-最小割边数&最大流最小割-Smallest Minimum Cut
- 大数据学习14:Hive中Join的原理和机制
- angular2向组件中传值的问题
- Codeforces Round #434 k-rounding
- iOS银行卡验证
- 测试入门之缺陷管理
- HDU 6214 Smallest Minimum Cut 最小割(isap)
- C语言
- 9月18日云栖精选夜读:「阿里巴巴编码规范(Java版)」认证考试出炉!你考过了吗?
- 数据结构Java实现01----算法概述
- 用邻接矩阵创建无向网
- JAVA IO流最详解(一)
- java中的URLEncoder.encode对应JS中用decodeURIComponent,js和java编码,解码
- 单例设计模式----概述
- 5、群组函数:诺贝尔奖的更多练习