poj 3308 java

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class POJ_3308 {

	static List<Integer> map[];
	static double cap[][];
	static double flow[][];
	static int s,t;
	static int R,C;
	public static final double INF = 1e8;
	static double minCost;
	static int dist[];
	
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		int N = scan.nextInt();
		
		for(int c=0;c<N;c++){
			R = scan.nextInt();
			C = scan.nextInt();
			int L = scan.nextInt();
			
			minCost = 0;
			cap = new double[R+C+2][R+C+2];
			flow = new double[R+C+2][R+C+2];
			map = new ArrayList[R+C+2];
			for(int i=0;i<R+C+2;i++)
				map[i] = new ArrayList<Integer>();
			
			s = 0;
			t = R+C+1;
			for(int i=1;i<=R;i++){
				cap[0][i] = Math.log10(scan.nextDouble());
				map[0].add(i);
				map[i].add(0);
			}
			for(int i=1;i<=C;i++){
				cap[i+R][t] = Math.log10(scan.nextDouble());
				map[i+R].add(t);
				map[t].add(i+R);
			}
			
			for(int i=0;i<L;i++){
				int a = scan.nextInt();
				int b = scan.nextInt();
				cap[a][b+R] = INF;
				map[a].add(b+R);
				map[b+R].add(a);
			}
			
			while(BFS())
				minCost += DFS(s,INF);
			
			System.out.printf("%.4f",Math.pow(10, minCost));
			System.out.println();
			
		}

	}

	public static double DFS(int s, double flow) {
		
		if(s == t)
			return flow;
		double subFlow = 0;
		
		for(int i=0;i<map[s].size();i++){
			int j = map[s].get(i);
			if(dist[s]+1==dist[j]&&eps(cap[s][j])>0){
				double adjustFlow = DFS(j,Math.min(flow-subFlow, cap[s][j]));
				subFlow += adjustFlow;
				
				cap[s][j] -= adjustFlow;
				cap[j][s] += adjustFlow;
			}
		}
		
		return subFlow;
	}

	public static boolean BFS() {
		Queue<Integer> q = new LinkedList<Integer>();
		dist = new int[R+C+2];
		boolean visit[] = new boolean[R+C+2];
		q.add(s);
		visit[s] = true;
		dist[s] = 1;
		while(!q.isEmpty()){
			int v = q.poll();
			for(int i=0;i<map[v].size();i++){
				int j = map[v].get(i);
				if(!visit[j]&&eps(cap[v][j])>0){
					visit[j] = true;
					dist[j] = dist[v] + 1;
					q.add(j);
				}
			}
		}
		
		return visit[t];
	}

	public static double eps(double ds) {
		
		return ds<1e-8?0:ds;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值