poj 1273 网络流
2013-04-07 20:27
363 查看
import java.io.*; import java.util.*; public class Main { static int[][] map = new int[202][202]; static int[] level = new int[202]; public static boolean bfs(int sink){ Arrays.fill(level, 0); boolean[] mark = new boolean[202]; Queue<Integer> q = new LinkedList<Integer>(); q.add(1); mark[1] = true; level[1] = 1; while(!q.isEmpty()){ int tmp = q.poll(); for(int i=1;i<=sink;i++){ if(!mark[i]&&map[tmp][i]>0){ mark[i] = true; level[i] = level[tmp]+1; q.add(i); } } } return mark[sink]; } public static int dfs(int d, int cap, int sink){ if(d==sink){ return cap; } int tmp = cap; for(int i=1;i<=sink;i++){ if(level[i] ==level[d]+1 &&tmp>0&&map[d][i]>0){ int flow = dfs(i,Math.min(tmp,map[d][i]),sink); map[d][i]-=flow; map[i][d]+=flow; tmp-=flow;//residual flow to be relaxed } } return cap - tmp; } public static void dinic(int sink){ int result = 0; while(bfs(sink)){ result+=dfs(1,999999999,sink); } System.out.println(result); } public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String ss = ""; while((ss=reader.readLine())!=null){ map = new int[202][202]; String[] tmp = ss.split(" "); int path = Integer.parseInt(tmp[0]); int node = Integer.parseInt(tmp[1]); for(int i=0;i<path;i++){ ss = reader.readLine(); tmp = ss.split(" "); int s = Integer.parseInt(tmp[0]); int d = Integer.parseInt(tmp[1]); int l = Integer.parseInt(tmp[2]); map[s][d] += l; } dinic(node); } } };
DINIC 过的,没啥问题,等下写一下EK 和 临界表实现
再补一个EK算法的:
import java.io.*; import java.util.*; public class Main { static int[][] map = new int[202][202]; static int[] prev = new int[202]; public static boolean bfs(int source, int sink){ Queue<Integer> q = new LinkedList<Integer>(); q.add(source); Arrays.fill(prev, 0); prev[source] = source; while(!q.isEmpty()){ int tmp = q.poll(); for(int i=source;i<=sink;i++){ if(prev[i]==0&&map[tmp][i]>0){ prev[i] =tmp; q.add(i); if(i==sink) return true; } } } return false; } public static void MaxFlow(int source, int sink){ int flow = 0; while(bfs(source, sink)){ int tmp = sink; int min = 99999999; while(tmp!=source){ min = Math.min(min,map[prev[tmp]][tmp]); tmp = prev[tmp]; } tmp = sink; while(tmp!=source){ map[prev[tmp]][tmp] -= min; map[tmp][prev[tmp]] += min; tmp = prev[tmp]; } flow+=min; } System.out.println(flow); } public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String ss = ""; while((ss=reader.readLine())!=null){ map = new int[202][202]; String[] tmp = ss.split(" "); int path = Integer.parseInt(tmp[0]); int node = Integer.parseInt(tmp[1]); for(int i=0;i<path;i++){ ss = reader.readLine(); tmp = ss.split(" "); int s = Integer.parseInt(tmp[0]); int d = Integer.parseInt(tmp[1]); int l = Integer.parseInt(tmp[2]); map[s][d] += l; } MaxFlow(1,node); } } };
感慨一下EK算法居然172MS就过了,比DINIC还快!!!
看来小数据完全看OJ状态。。。
总的来说编程复杂度和DINIC类似,但是貌似DINIC效率很高. EK有前途么,没人写吧
临界表的不写了,有点蛋疼。
相关文章推荐
- hdu 1532(poj 1273) Drainage Ditches (网络流·最大流)
- poj 1273 第1道网络流 Edmonds-Karp算法
- POJ 1273 Drainage Ditches 网络流基础题
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
- Drainage Ditches - poj 1273(网络流模板)
- POJ1273_Drainage Ditches(网络流)
- poj 1273 Drainage Ditches(网络流模版题)
- poj 1273 Drainage Ditches (第一道网络流~)
- POJ 1273 Drainage Ditches【图论,网络流】
- POJ 1273 网络流(最大流)模板
- poj 1273 裸 网络流 (dinic)
- 网络流poj(1273)Drainage Ditches
- poj 1273 Drainage Ditches(网络流基础)
- poj 1273 网络流
- POJ 1273 网络流(EK,Dinic,ISAP)
- poj 1273 hdu 1532 网络流最大流 Dinic算法
- poj1273 网络流 Dinic
- poj 1273 Drainage Ditches 网络流 Edmond Karp || Dinic
- hdu3549(网络流)买一送一poj1273
- 【POJ 1273】Drainage Ditches(网络流)