Paratroopers
Description 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 theith 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 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. Output 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. Sample Input 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 Sample Output 16.0000 Source |
分析:最小割。总费用用乘法算,故用对数函数log()转化成加法。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define mem(x,i) memset(x,i,sizeof(x))
#define sfi(a) scanf("%d", &a)
#define sfii(a,b) scanf("%d %d", &a, &b)
#define sfiii(a,b,c)scanf("%d %d %d", &a, &b, &c)
const double EPS = 1e-10;
const double pai = acos(-1.0);
const int INF = 0xfffffff;
const int MOD = 1000000007;
typedef long long LL;
const int maxn = +5;
--------------------最大流Dinic开始------------------------
const int maxnode = 105;
const int maxedge = 10005*4;
struct Edge
{
int v, nxt;
double f;
};
struct Dinic{
int src, sink;
int g[maxnode + 10];
int nume;
Edge e[maxedge * 2 + 10];
void init()
{
memset(g, 0, sizeof(g));
nume = 1;
}
void addedge(int u, int v, double c)
{
e[++nume].v = v;
e[nume].f = c;
e[nume].nxt = g[u];
g[u] = nume;
e[++nume].v = u;
e[nume].f = 0;
e[nume].nxt = g[v];
g[v] = nume;
}
queue<int> que;
bool vis[maxnode + 10];
int dist[maxnode + 10];
void bfs()
{
memset(dist, 0, sizeof(dist));
while (!que.empty()) que.pop();
vis[src] = true;
que.push(src);
while (!que.empty()){
int u = que.front();
que.pop();
for (int i = g[u]; i; i = e[i].nxt)
if (e[i].f>EPS&&!vis[e[i].v]){
que.push(e[i].v);
dist[e[i].v] = dist[u] + 1;
vis[e[i].v] = true;
}
}
}
double dfs(int u, double delta)
{
if (u == sink){
return delta;
}
else{
double ret = 0;
for (int i = g[u]; delta&&i; i = e[i].nxt)
if (e[i].f&&dist[e[i].v] == dist[u] + 1){
double dd = dfs(e[i].v, min(e[i].f, delta));
e[i].f -= dd;
e[i ^ 1].f += dd;
delta -= dd;
ret += dd;
}
return ret;
}
}
double maxflow(int s, int t)
{
src = s; sink = t;
double ret = 0;
while (true){
memset(vis, 0, sizeof(vis));
bfs();
if (!vis[sink]) return ret;
ret += dfs(src, INF);
}
}
};
--------------------Dinic结束------------------------
Dinic D;
int N, M, L;
int main()
{
//freopen("f:\\input.txt", "r", stdin);
int T;
sfi(T);
while (T--){
sfiii(N, M, L);
D.init();
int s = N + M, t = s + 1;
for (int i = 0; i < N; i++){
double c;
scanf("%lf", &c);
c = log(c);
D.addedge(s, i, c);
}
for (int i = 0; i < M; i++){
double c;
scanf("%lf", &c);
c = log(c);
D.addedge(N + i, t, c);
}
for (int i = 0; i < L; i++){
int x, y;
sfii(x, y);
D.addedge(x - 1, y - 1 + N, INF);
}
double ret = D.maxflow(s, t);
printf("%.4f\n", exp(ret));
}
return 0;
}