Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7847 | Accepted: 2365 |
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
题目描述:
敌人侵略r*c的地图。为了消灭敌人,可以在某一行或者某一列安置超级大炮。
每一个大炮可以瞬间消灭这一行(或者列)的敌人。
安装消灭第i行的大炮消费是ri。
安装消灭第j行的大炮消费是ci
现在有n个敌人,告诉你这n个敌人的坐标,让你 同时 消灭这些敌人,为你最小花费是多少。
花费的定义:每个大炮消费的乘积。
思路:
花费是乘积,那么转化成log之后就是求和了。所以转化为求最小的和是多少。
一个敌人在a行b列,那么他可以被大炮ra,或者cb消灭。
建立二分图:左边是r行,右边是c列。一个敌人在a行b列,就连接左边的点a和右边的点b。这样,一条边就表示了消灭一个敌人。
那么选取一些点,是这些点的权值之和最小,同时覆盖所有的边(敌人),就是我们的答案了。显然,问题转化为:最小点权覆盖。
建图如下:
二分图x和y两部分。
一个超级源点s,和x连接,容量是x的权值。
一个超级汇点t,y和t连接,容量是y的权值。
如果x中点a和y中点b有边,连接ab,权值无限大。
最大流(最小割)就是答案
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> #define maxn 200 #define maxm 1000000 #define INF 0x3f3f3f3f using namespace std; int m, n, L; int head[maxn],cur[maxn], cnt; int dist[maxn], vis[maxn]; struct node{ int u, v; double cap, flow; int next; }; node edge[maxm]; void init(){ cnt = 0; memset(head, -1, sizeof(head)); } void add(int u, int v, double w){ node E1 = {u, v, w, 0, head[u]}; edge[cnt] = E1; head[u] = cnt++; node E2 = {v, u, 0, 0, head[v]}; edge[cnt] = E2; head[v] = cnt++; } void getmap(){ double r, c; for(int j = 1; j <=n; ++j){ scanf("%lf", &c); add(0, j, log(c)); } for(int i = 1; i <= m; ++i){ scanf("%lf", &r); add(n + i, n + m + 1, log(r)); } for(int i = 1; i <= L; ++i){ int a, b; scanf("%d%d", &a, &b); add(a, b + n, INF); } } bool BFS(int st, int ed){ queue<int>q; memset(vis, 0 ,sizeof(vis)); memset(dist, -1, sizeof(dist)); vis[st] = 1; dist[st] = 0; q.push(st); while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(!vis[E.v] && E.cap > E.flow){ vis[E.v] = 1; dist[E.v] = dist[u] + 1; if(E.v == ed) return true; q.push(E.v); } } } return false; } double DFS(int x, int ed, double a){ if(a == 0 || x == ed) return a; double flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next){ node &E = edge[i]; if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){ E.flow += f; edge[i ^ 1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } double maxflow(int st, int ed){ double sumflow = 0; while(BFS(st,ed)){ memcpy(cur, head, sizeof(head)); sumflow += DFS(st, ed, INF); } return sumflow; } int main (){ int T; scanf("%d", &T); while(T--){ scanf("%d%d%d", &n,&m,&L); init(); getmap(); printf("%.4lf\n", exp(maxflow(0, n + m + 1))); } return 0; }