2013
11-12

# Paratroopers

It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the invaders of Mars want to land some paratroopers in the m × n grid yard of one their main weapon factories in order to destroy it. In addition, the spies informed them the row and column of the places in the yard in which each paratrooper will land. Since the paratroopers are very strong and well-organized, even one of them, if survived, can complete the mission and destroy the whole factory. As a result, the defense force of the Earth must kill all of them simultaneously after their landing.

In order to accomplish this task, the defense force wants to utilize some of their most hi-tech laser guns. They can install a gun on a row (resp. column) and by firing this gun all paratroopers landed in this row (resp. column) will die. The cost of installing a gun in the ith row (resp. column) of the grid yard is ri (resp. ci ) and the total cost of constructing a system firing all guns simultaneously is equal to the product of their costs. Now, your team as a high rank defense group must select the guns that can kill all paratroopers and yield minimum total cost of constructing the firing system.

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing three integers 1 ≤ m ≤ 50 , 1 ≤ n ≤ 50 and 1 ≤ l ≤ 500 showing the number of rows and columns of the yard and the number of paratroopers respectively. After that, a line with m positive real numbers greater or equal to 1.0 comes where the ith number is ri and then, a line with n positive real numbers greater or equal to 1.0 comes where the ith number is ci. Finally, l lines come each containing the row and column of a paratrooper.

For each test case, your program must output the minimum total cost of constructing the firing system rounded to four digits after the fraction point.

1
4 4 5
2.0 7.0 5.0 2.0
1.5 2.0 2.0 8.0
1 1
2 2
3 3
4 4
1 4

16.0000

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

public class Main{

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());
}
for(int i=1;i<=C;i++){
cap[i+R][t] = Math.log10(scan.nextDouble());
}

for(int i=0;i< L;i++){
int a = scan.nextInt();
int b = scan.nextInt();
cap[a][b+R] = INF;
}

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){

}
}

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];
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;
}
}
}

return visit[t];
}

public static double eps(double ds) {

return ds< 1e-8?0:ds;
}
}

1. 微博-默认微博-心情微博-浪小花微博-拜年微博-柏夫微博-小幺鸡微博-天气微博-休闲微博-搞怪微博-大熊微博-BOBO和TOTO微博-管不着微博-阿狸微博-懒猫猫微博-大耳兔微博-哈皮兔微博-星座微博-爱心微博-亚运会微博-张小盒微博-悠嘻猴微博-小新小