题目:
http://poj.org/problem?id=3308
题意:
有一个n*m的格子,格子上有一些敌人,给出这些敌人在格子上的位置,可以在行或者列上安装一些武器,每次消灭一行或者一列,动用每一个武器都有一个花费,总的花费为每个武器花费的成绩,求消灭所有敌人的最小花费
思路:
二分图最小点权匹配,只不过之前碰到的题最大流是累加和,这个是累乘积,建图时对所有的花费取log,然后对求出来的最大流取exp,因为log(a) + log(b) = log(a*b),再用exp求出原值就好了。我之前的做法是用花费直接建图,不取log,然后求出所有的割,把容量相乘,不知道这样做什么不对。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;
struct edge
{
int to, next;
double cap;
}g[N*N*2];
int cnt, head[N];
int gap[N], que[N], level[N], pre[N], cur[N];
bool vis[N];
int ss, tt, nv;
void add_edge(int v, int u, double cap)
{
g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
void bfs(int t)
{
memset(level, -1, sizeof level);
memset(gap, 0, sizeof gap);
int st = 0, en = 0;
level[t] = 0;
que[en++] = t;
gap[level[t]]++;
while(st < en)
{
int v = que[st++];
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(level[u] < 0)
{
level[u] = level[v] + 1;
gap[level[u]]++;
que[en++] = u;
}
}
}
}
double sap(int s, int t)
{
bfs(t);
memcpy(cur, head, sizeof head);
int v = pre[s] = s;
double flow = 0, aug = INF;
while(level[s] < nv)
{
bool flag = false;
for(int &i = cur[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[v] == level[u] + 1)
{
flag = true;
pre[u] = v;
v = u;
aug = min(aug, g[i].cap);
if(v == t)
{
flow += aug;
while(v != s)
{
v = pre[v];
g[cur[v]].cap -= aug;
g[cur[v]^1].cap += aug;
}
aug = INF;
}
break;
}
}
if(flag) continue;
int minlevel = nv;
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[u] < minlevel)
minlevel = level[u], cur[v] = i;
}
if(--gap[level[v]] == 0) break;
level[v] = minlevel + 1;
gap[level[v]]++;
v = pre[v];
}
return flow;
}
int main()
{
int t, n, m, k, a, b;
double c;
scanf("%d", &t);
while(t--)
{
cnt = 0;
memset(head, -1, sizeof head);
scanf("%d%d%d", &n, &m, &k);
ss = 0, tt = n + m + 1;
for(int i = 1; i <= n; i++) scanf("%lf", &c), add_edge(ss, i, log(c));
for(int i = 1; i <= m; i++) scanf("%lf", &c), add_edge(n+i, tt, log(c));
for(int i = 1; i <= k; i++) scanf("%d%d", &a, &b), add_edge(a, n+b, INF);
nv = tt + 1;
printf("%.4f\n", exp(sap(ss, tt)));
}
return 0;
}