POJ 3308 Paratroopers 最小割、最大流
来源:互联网 发布:阿里云数据库备份 编辑:程序博客网 时间:2024/05/21 17:13
这道题目的建图是:从源点到行,在从行到列,最后从列到汇点。然后注意权值,因为这里让求的是乘积的最大值,所以要是用EK求最大时,用到的是加法,所以需要用对数转化一下。log(a)+log(b) = log(a*b),然后再转化回来就行了啊。
这道题目数组开小了,但是poj一直返回wa,然后就提交了好多遍才找到原因啊,以后的注意一下。
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
14 4 52.0 7.0 5.0 2.01.5 2.0 2.0 8.01 12 23 34 41 4
Sample Output
16.0000
EK算法:
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7//#define M 1000100//#define LL __int64#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 110;using namespace std;double c[110][110];int pre[maxn];double f[maxn];int n, m;double sum;double spfa(int s, int t){ memset(pre, -1 , sizeof(pre)); memset(f , 0 , sizeof(f)); queue<int>q; while(!q.empty()) q.pop(); q.push(s); f[s] = INF; pre[s] = 0; while(!q.empty()) { int temp = q.front(); q.pop(); if(temp == t) break; for(int i = 0; i <= n+m+1; i++) { if(c[temp][i] > 0 && pre[i] == -1) { pre[i] = temp; f[i] = min(c[temp][i], f[temp]); q.push(i); } } } return f[t];}void EK(int s, int t){ while(1) { double temp = spfa(s, t); if(temp == 0) break; for(int i = t; i != s; i = pre[i]) { c[pre[i]][i] -= temp; c[i][pre[i]] += temp; } sum += temp; }}int main(){ int t, l; cin >>t; while(t--) { cin >>m>>n>>l; memset(c, 0 , sizeof(c)); for(int i = 1; i <= m; i++) { double a; cin >>a; c[0][i] = log(a); } for(int i = 1; i <= n; i++) { double a; cin >>a; c[i+m][n+m+1] = log(a); } for(int i = 1; i <= l; i++) { int x, y; cin >>x>>y; c[x][y+m] = INF; } sum = 0; EK(0, m+n+1); cout<<fixed<<setprecision(4)<<exp(sum)<<endl; } return 0;}
dinic算法:
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7//#define M 1000100//#define LL __int64#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 1010;using namespace std;int pre[maxn];int head[maxn];int cnt;int n, m;int deep[maxn];struct node{ int v,next; double w;} edge[maxn*5];void init(){ cnt = 0; memset(head , -1 , sizeof(head));}void add(int u, int v, double w){ edge[cnt].v = v; edge[cnt].w = w; edge[cnt].next = head[u]; head[u] = cnt++; edge[cnt].v = u; edge[cnt].w = 0; edge[cnt].next = head[v]; head[v] = cnt++;}bool bfs()//bfs一次找到所有的增广路{ memset(deep, -1 , sizeof(deep)); queue<int>q; while(!q.empty()) q.pop(); q.push(0); deep[0] = 0; while(!q.empty()) { int t = q.front(); q.pop(); int p = head[t]; while(p != -1) { int v = edge[p].v; if(deep[v] == -1 && edge[p].w > 0) { deep[v] = deep[t]+1; q.push(v); } p = edge[p].next; } } if(deep[n+m+1] == -1) return false; return true;}void fid(int u, int v, double w)//更新反向边{ int p = head[u]; while(edge[p].v != v) { p = edge[p].next; } edge[p].w += w;}double dfs(int s, double f){ if(s == n+m+1) return f; double sum = 0; int p = head[s]; while(p != -1) { int v = edge[p].v; if(deep[v] == deep[s]+1 && edge[p].w > 0) { double temp = dfs(v, min(f-sum, edge[p].w));//这里要等于最小的流量,保证所有的可以通过 sum += temp; edge[p].w -= temp;//正向边流量减少 fid(v, s, temp);//反向边流量增加 } p = edge[p].next; } return sum;}double dinic(){ double ans=0; while(bfs()) { ans += dfs(0,INF); } return ans;}int main(){ int t, l; cin >>t; while(t--) { cin >>m>>n>>l; init(); for(int i = 1; i <= m; i++) { double a; cin >>a; add(0, i, log(a)); } for(int i = 1; i <= n; i++) { double a; cin >>a; add(i+m, n+m+1, log(a)); } for(int i = 1; i <= l; i++) { int x, y; cin >>x>>y; add(x, y+m, INF); } double sum; sum = dinic(); cout<<fixed<<setprecision(4)<<exp(sum)<<endl; } return 0;}
- POJ 3308 Paratroopers 最小割、最大流
- poj 3308 Paratroopers 最小割—最大流
- POJ 3308 Paratroopers //最小割
- 【最小割】POJ-3308 Paratroopers
- (最小割)poj 3308 Paratroopers
- POJ 3308 Paratroopers (二分图最小点权覆盖 -> 最小割 -> 最大流)
- POJ3308 Paratroopers 最大流最小割
- poj 3308 Paratroopers(网络流 最小割 dinic模板)
- poj 3308 Paratroopers(最小割)
- POJ 3308 Paratroopers(最小割+Dinic)
- POJ 3308 Paratroopers (最小割)
- POJ 3308--Paratroopers【 最小点权覆盖 && 最小割】
- [POJ 3308]Paratroopers[最大流]
- (POJ3308)Paratroopers 最大流最小割,建图,模板题
- zoj 2874 & poj 3308 Paratroopers (最小割)
- poj 3308 zoj 2874 Paratroopers(最小割)
- POJ 3308 Paratroopers(最小割EK(邻接表&矩阵))
- POJ 3308 Paratroopers(最小割+对数降级)
- CareerCup Find the ceiling value present in the BST of a given ke
- java 使用命令行查找注册表
- php几个函数
- SQL数据库中的临时表详解
- xml是被设计为存储、传输以及交换数据
- POJ 3308 Paratroopers 最小割、最大流
- 管理centos6.5应用程序中的图标
- 微软欲让Windows兼容安卓程序
- 东软睿道实训日记—第一天
- 内存管理的思考
- 浅析EJB
- mac 系统的触摸板使用技巧
- CDatabase类
- MySQL纪实