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 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
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
题目:给出一个矩阵,矩阵上会有伞兵溅落,每次可杀死某一行的所有兵或者杀死某一列的所有兵。每个行、每个列都有一个花费。
求:杀掉所有兵的最少投资花费(投资花费为被选上的花费(可以是行,可以是列)的乘积)。
分析:杀死一个兵,可通过选行或者选列,最小点权覆盖模型,每个兵拆成两个点(u,v),s->u为选择行的花费的对数,v->t为选择列的花费的对数,u->v为无穷。最大流后结果用exp(maxflow)就是最小花费。
代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#include <cmath>
#include <algorithm>
#include <iostream>
#define Min(a, b) (a) < (b) ? a : b
#define Max(a, b) (a) > (b) ? a : b
using namespace std;
const int MAXN = 2005;
const int MAXM = 700000;
const double INF = 1100000000;//double
struct Edge
{
int st, ed, next;
double flow, cap;//double
}edge[MAXM];
int head[MAXN], level[MAXN], que[MAXN], stk[MAXN], pre[MAXN], del[MAXN], E;
void add(int u, int v, double w)
{
edge[E].flow = 0, edge[E].cap = w, edge[E].st = u, edge[E].ed = v;
edge[E].next = head[u], head[u] = E++;
edge[E].flow = 0, edge[E].cap = 0, edge[E].st = v, edge[E].ed = u;
edge[E].next = head[v], head[v] = E++;
}
int dinic_bfs(int src, int dest, int ver)
{
int i, j;
for (i = 0; i <= ver; i++)
{
level[i] = -1;
}
int rear = 1;
que[0] = src, level[src] = 0;
for (i = 0; i < rear; i++)
{
for (j = head[que[i]]; j != -1; j = edge[j].next)
{
if (level[edge[j].ed] == -1 && edge[j].cap > edge[j].flow)
{
level[edge[j].ed] = level[que[i]] + 1;
que[rear++] = edge[j].ed;
}
}
}
return level[dest] >= 0;
}
double dinic_dfs(int src, int dest, int ver)//double返回值
{
int top = 0, cur, ptr, i;
double ret = 0, minf;//double
for (i = 0; i <= ver; i++)
{
del[i] = 0;
}
stk[top++] = src;
pre[src] = src;
cur = src;
while (top)
{
while (cur != dest && top)
{
for (i = head[cur]; i != -1; i = edge[i].next)
{
if (level[edge[i].ed] == level[cur] + 1 && edge[i].cap > edge[i].flow && !del[edge[i].ed])
{
stk[top++] = edge[i].ed;
cur = edge[i].ed;
pre[edge[i].ed] = i;
break;
}
}
if (i == -1)
{
del[cur] = 1;
top--;
if (top) cur = stk[top-1];
}
}
if (cur == dest)
{
minf = INF;
while (cur != src)
{
cur = pre[cur];
minf = Min(minf, edge[cur].cap - edge[cur].flow);
cur = edge[cur].st;
}
cur = dest;
while (cur != src)
{
cur = pre[cur];
edge[cur].flow += minf;
edge[cur^1].flow -= minf;
if (edge[cur].cap - edge[cur].flow == 0)
{
ptr = edge[cur].st;
}
cur = edge[cur].st;
}
while (top > 0 && stk[top-1] != ptr) top--;
if (top) cur = stk[top-1];
ret += minf;
}
}
return ret;
}
double Dinic(int src, int dest, int ver)//double返回值
{
double ret = 0, t;//double
while (dinic_bfs(src, dest, ver))//bfs
{
t = dinic_dfs(src, dest, ver);//dfs
if (t) ret += t;
else break;
}
return ret;
}
int main()
{
int ca, n, m, k, i, j;
double w;
scanf("%d", &ca);
while (ca--)
{
scanf("%d%d%d", &n, &m, &k);
int s = 0, t = n + m + 1, ver = t + 1;
E = 0;
for (i = 0; i <= ver; i++)
{
head[i] = -1;
}
for (i = 1; i <= n; i++)
{
scanf("%lf", &w);
add(s, i, log(w));
}
for (i = 1; i <= m; i++)
{
scanf("%lf", &w);
add(i + n, t, log(w));
}
while (k--)
{
scanf("%d%d", &i, &j);
add(i, j + n, INF);
}
double ans = Dinic(s, t, ver);
printf("%.4f\n", exp(ans));
}
return 0;
}
/*
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
*/