hdu-1532-Drainage Ditches-Dinic算法-java

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18745    Accepted Submission(s): 8908


Problem Description
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 
 

Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
 

Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond. 
 

Sample Input
  
  
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
 

Sample Output
  
  
50
解题思路:
先不说题先让我哭一会 (哇啊啊啊啊啊啊啊啊啊啊啊啊)(哇啊啊啊啊啊啊)
弄懂这网络流太心酸了
Dinic算法是一种求最大流的优化
1.在搜索之前先对图划分层次
2.搜索时严格按照层次搜索
刚开始不觉有多快 第一遍没用这个写稳稳超时 用上这个java300多ms就过了
发明这算法的人太nb了 就是神器啊 哈哈哈哈哈
如果下面的代码不理解 先去看看ek算法 我菜的自己都受不了 就不写了
ac代码:
import java.util.LinkedList; import java.util.Scanner; public class Main { private static int n; private static int m; private static int[][] graph; private static int sum; private static int[] flag_level; public static void main(String[] args) { // TODO Auto-generated method stub         Scanner scanner = new Scanner(System.in);         while (scanner.hasNext()) { SCN(scanner);//输入 Dinic(); System.out.println(sum); } }     //Dinic算法     private static void Dinic() {     while (bfs()) { int value; do { value = dfs(1,Integer.MAX_VALUE); sum += value; } while (value!=0); } } //深搜寻找此层次下的所有可增广路径     private static int dfs(int i, int min) {        if (i==n) {return min;}        for (int j = 1; j <=n; j++) { if (flag_level[j]==flag_level[i]+1&&graph[i][j]>0) { int k = dfs(j,Math.min(min,graph[i][j])); if (k>0) { graph[i][j] -= k; graph[j][i] += k; return k; } } }     return 0; } //广搜建立了一棵层次树 private static boolean bfs() {         flag_level = new int [n+1];//层次标记数组 LinkedList<Integer> queue = new LinkedList<Integer>(); //辅助进行松弛操作的队列 queue.push(1); flag_level[1] = 1; while (!queue.isEmpty()) { int point = queue.pop(); for (int j = 1; j <=n; j++) { if (flag_level[j]==0&&graph[point][j]>0) { flag_level[j] = flag_level[point]+1; queue.push(j); } } } return flag_level[n]>0?true:false; } //构图 private static void SCN(Scanner scanner) { sum = 0; m = scanner.nextInt(); n = scanner.nextInt();    graph = new int [n+1][n+1];    for (int i = 0; i < m; i++) { int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); graph[a][b] += c; } } }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值