您的位置:首页 > 理论基础 > 计算机网络

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有前途么,没人写吧

临界表的不写了,有点蛋疼。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: