本題題意就是,公元XXXX年,地球跟外星人打仗,然後有一個n*m的網格,會有外星人降落到某些位置上,因為外星人比較猛,所以必須一下來就消滅他們,現在可以在某些行或者某些列的首部放一些激光槍。這些槍的特性就是你放在行的首部你就消滅這一行的敵人,放在列的首部就消滅一列的敵人。但是放置這些槍也需要一定的費用,這些費用已經給出來了,最後總費用是這些槍的費用之積,現在要求最小的這個費用。
看到積之後,我們可以轉換為加法,就是取log,但是不知道數據是什麼情況,會不會超過double,就試一下。
然後就能發現是一個二分圖最小點權覆蓋的模型了
然後就是建圖,源點跟所有的行節點連邊,值呢就是相應花費的log,然後列節點與匯點連邊,值也為相應的花費的log,行與列的連邊就代表著相應的外星人了,值為INF。
注意到INF不能太大,因為double的精度問題,INF如果位數太多,算最大流的時候由於有小數,小數點後如果有8位,小數點之前如果再有太多的位數,就會損失精度
最後的結果用exp函數求回來即可
[cpp]
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 111
#define MAXM 55555
#define INF 1000007
using namespace std;
struct node
{
int v;
double c, f;
int next, r;
}edge[MAXM];
int dist[MAXN], nm[MAXN], src, des, n;
int head[MAXN], e;
void add(int x, int y, double c)
{
edge[e].v = y;
edge[e].c = c;
edge[e].f = 0;
edge[e].r = e + 1;
edge[e].next = head[x];
head[x] = e++;
edge[e].v = x;
edge[e].c = 0;
edge[e].f = 0;
edge[e].r = e - 1;
edge[e].next = head[y];
head[y] = e++;
}
void rev_BFS()
{
int Q[MAXN], h = 0, t = 0;
for(int i = 1; i <= n; ++i)
{
dist[i] = MAXN;
nm[i] = 0;
}
Q[t++] = des;
dist[des] = 0;
nm[0] = 1;
while(h != t)
{
int v = Q[h++];
for(int i = head[v]; i != -1; i = edge[i].next)
{
if(edge[edge[i].r].c == 0 || dist[edge[i].v] < MAXN)continue;
dist[edge[i].v] = dist[v] + 1;
++nm[dist[edge[i].v]];
Q[t++] = edge[i].v;
}
}
}
void init()
{
e = 0;
memset(head, -1, sizeof(head));
}
double maxflow()
{
rev_BFS();
int u;
double total = 0;
int cur[MAXN], rpath[MAXN];
for(int i = 1; i <= n; ++i)cur[i] = head[i];
u = src;
while(dist[src] < n)
{
if(u == des) // find an augmenting path
{
double tf = INF;
for(int i = src; i != des; i = edge[cur[i]].v)
tf = min(tf, edge[cur[i]].c);
for(int i = src; i != des; i = edge[cur[i]].v)
{
edge[cur[i]].c -= tf;
edge[edge[cur[i]].r].c += tf;
edge[cur[i]].f += tf;
edge[edge[cur[i]].r].f -= tf;
}
total += tf;
u = src;
}
int i;
for(i = cur[u]; i != -1; i = edge[i].next)
if(edge[i].c > 0 && dist[u] == dist[edge[i].v] + 1)break;
if(i != -1) // find an admissible arc, then Advance
{
cur[u] = i;
rpath[edge[i].v] = edge[i].r;
u = edge[i].v;
}
else // no admissible arc, then relabel this vtex
{
if(0 == (--nm[dist[u]]))break; // GAP cut, Important!
cur[u] = head[u];
int mindist = n;
for(int j = head[u]; j != -1; j = edge[j].next)
if(edge[j].c > 0)mindist = min(mindist, dist[edge[j].v]);
dist[u] = mindist + 1;
++nm[dist[u]];
if(u != src)
u = edge[rpath[u]].v; // Backtrack
}
}
return total;
}
int nt, m, l;
int main()
{
int T, u, v;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &nt, &m, &l);
src = nt + m + 1;
des = nt + m + 2;
n = des;
init();
double tmp;
for(int i = 1; i <= nt; i++)
{
scanf("%lf", &tmp);
add(src, i, log(tmp));
}
for(int i = nt + 1; i <= nt + m; i++)
{
scanf("%lf", &tmp);
add(i, des, log(tmp));
} www.2cto.com
while(l--)
{
scanf("%d%d", &u, &v);
add(u, nt + v, INF);
}
printf("%.4f\n", exp(maxflow()));
}
return 0;
}