poj 1273 网络流

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

临界表的不写了,有点蛋疼。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值