POJ 3380 最大流
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
System Crawler (2014-10-14)
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 npositive
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
题意:给出火星人降落的坐标(行x,列y)和使用每一行每一列武器的代价,你能够在一行或一列使用一把武器,能够消灭掉该行或该列的全部外星人。使用多种武器的代价是他们的乘积,你须要求出消灭全部火星人的最小代价。
思路:在把乘机转化成对数相加的形式,能够看到要消灭一个外星人,要么在他所在的行上放一把武器,要么在他所在列上放一把武器,因此能够在源点s和行x上连一条log(w)的边,在列和汇点t上连一条log(w)的边,在外星人的坐标x和y+n之间连一条INF的边。由于增广的时候满足流量限制所以会选择最小的代价。
只是还是wa了非常久。。原因就是用个g++提交,输出的时候使用了%lf,后来改成%f了还是wa,后来发现关闭了c++和c的输入输出同步,但任然在混用输入。。取消关闭之后就ac了。。。所以建议没事还是不要混用输入输出了
代码例如以下:
/*************************************************************************
> File Name: c.cpp
> Author: acvcla
> QQ:
> Mail: acvcla@gmail.com
> Created Time: 2014年10月13日 星期一 22时26分17秒
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 5e2 + 30;
const int INF =1e7;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int n,m,s,t,l;
int d[maxn],cur[maxn];
struct Edge
{
int from,to;
double cap,flow;
};
std::vector<int>G[maxn];
std::vector<Edge>edges;
void init(int n){
for(int i=0;i<=n;i++)G[i].clear();
edges.clear();
}
void addEdge(int u,int v,double w)
{
edges.pb((Edge){u,v,w,0});
edges.pb((Edge){v,u,0.0,0});
int sz=edges.size();
G[u].pb(sz-2);
G[v].pb(sz-1);
}
int bfs(){
memset(d,0,sizeof d);
queue<int>q;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<G[u].size();i++){
Edge &e=edges[G[u][i]];
if(e.to==s)continue;
if(!d[e.to]&&e.cap>e.flow){
q.push(e.to);
d[e.to]=d[u]+1;
}
}
}
return d[t];
}
double dfs(int u,double a)
{
if(u==t||a==0.0)return a;
double flow=0.0,f=0.0;
for(int &i=cur[u];i<G[u].size();++i){
Edge &e=edges[G[u][i]];
if(d[e.to]==d[u]+1&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0.0){
e.flow+=f;
edges[G[u][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0.0)break;
}
}
return flow;
}
double Dinic(){
double flow=0;
while(bfs()){
memset(cur,0,sizeof cur);
flow+=dfs(s,INF);
}
return flow;
}
int main(){
//开启关闭同步且混用输入输出wa,凝视掉AC
//ios_base::sync_with_stdio(false);
//cin.tie(0);
int T;scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&l);
int u,v;
s=0,t=n+1+m;
init(t);
double w;
for(int i=1;i<=n;i++){
cin>>w;
addEdge(s,i,log(w));
}for(int i=1;i<=m;i++){
cin>>w;
addEdge(n+i,t,log(w));
}
for(int i=1;i<=l;i++){
cin>>u>>v;
addEdge(u,v+n,INF);
}
double ans=Dinic();
printf("%.4f\n",exp(ans));
}
return 0;
}
POJ 3380 最大流的更多相关文章
- poj 3281 最大流+建图
很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很 ...
- UVA 820 --- POJ 1273 最大流
找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...
- poj 1273 最大流
题目链接:http://poj.org/problem?id=1273 a.EK算法:(Edmond-Karp): 用BFS不断找增广路径,当找不到增广路径时当前流量即为最大流. b.dinic算法: ...
- poj 1149 最大流
题目链接:http://poj.org/problem?id=1149 #include <cstdio> #include <cmath> #include <algo ...
- poj 3281 最大流建图
题目链接:http://poj.org/problem?id=3281 #include <cstdio> #include <cmath> #include <algo ...
- POJ 1698 最大流
Alice's Chance Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7327 Accepted: 2992 De ...
- [转载 ]POJ 1273 最大流模板
转载 百度文库花了5分下的 不过确实是自己需要的东西经典的最大流题POJ1273 ——其他练习题 POJ3436 . 题意描述: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条水渠,给 ...
- poj 2516 (费用流)
题意:有N个供应商,M个店主,K种物品.每个供应商对每种物品的的供应量已知,每个店主对每种物品的需求量的已知,从不同的供应商运送不同的货物到不同的店主手上需要不同的花费,又已知从供应商m送第k种货物的 ...
- poj 1459 (最大流)
最大流简单题,,这题重要的是知道了scanf("%s",str);sscanf(str,"(%d,%d)%d",&x,&y,&w);读入 ...
随机推荐
- hdu 4465 概率称号
http://acm.hdu.edu.cn/showproblem.php?pid=4465 第一直觉概率DP但很快被否定,发现只有一个简单的二项分布,但感情的表达,没有对生命和死亡的例子.然后找到准 ...
- MVC简单的认识
学习一个新知识,首先要了解的就是以下几个问题,它是什么?它能干什么?使用它有什么优点?这篇文章就环绕这几个问题来展开讨论. mvc不是一种编程语言,严格来说.它都不算是一门技术.它是开发软件时使用的一 ...
- K60 启动过程分析
很高兴老师借给我一K60的开发板,趁着暑假好好鼓捣鼓捣! 有了上图的过程分析我想心里大概有个低了吧! 以下看代码: /* CodeWarrior ARM Runtime Support Library ...
- Mapxtreme C#鹰眼地图
Demo演示程序下载地址: http://pan.baidu.com/s/1jG9gKMM#dir/path=%2F%E4%BA%A7%E5%93%81%2FDemos 找:EagelEyeMap.r ...
- 基于docker构建jenkins和svn服务(转)
码农们很定都知道svn的重要性,机器坏掉丢代码的惨痛教训想必很多人都有. jenkins可能很多人都不了解.这是一个持续集成的工具,在敏捷开发领域很流行:跟svn结合可以实现定期build.check ...
- Linux在device is busy处理
在Linux管理umount设备时,时常会遇到"device is busy", 假设umount一个文件系统碰到这样的情况.而且你并没有在所需卸载的文件夹下.那么非常可能实用户或 ...
- hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!
题意:维护一个文本编辑,并且查询最大前缀和. 写了splay,wa了13次 过了之后觉着特傻逼.发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和. 哎,傻逼,太弱了,还 ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
- 为什么数据线easy糟糕
一个好的设计可以帮助解决问题似乎无关紧要豪. 两天前M3数据线被破坏.在弯附近的电话插口可以充电.一松就充不了电了. 今天突然想到每次充电的时候用手机发信息.玩游戏都特别不方便,才想到为什么数据线ea ...
- c++的string容器
c++相比c的一个好处就是实现了很多的容器和泛型算法,使得程序员的工作得到了很大的化简.其中一个很有用的泛型容器就是string.string是一个类,是一个在STL里边实现好了的类,由于他的很多功能 ...