poj 3308 (最大流)
题意:n*m的地图,给出L个火星人登陆的坐标,要在火星人登陆地球的瞬间全部消灭他们,有一种激光枪,一次可以消灭一行(或一列),消灭一行(或一列)有不同的代价,总代价是所有激光枪的代价之积。
思路:之前做过类似的题是求最少多少次能消灭,而最少的次数不一定是代价最小的,行跟列建立二分图,每个火星人就是一条边,就是选一些点覆盖所有的边,这些点的权值之积最小,如果是求和的话就是二分图的最小点权覆盖集了,所以要把求积转化成求和,a*b=log(a)+log(b);求出最小割就可以了。
#include<stdio.h>
#include<math.h>
#include<string.h>
const int N=3000;
#define inf 100.0
int head[N],num,ans,start,end,gap[N],dis[N];
struct edge
{
int st,ed,next;
double flow;
}e[N*100];
void addedge(int x,int y,double w)
{
e[num].st=x;e[num].ed=y;e[num].flow=w;e[num].next=head[x];head[x]=num++;
e[num].st=y;e[num].ed=x;e[num].flow=0;e[num].next=head[y];head[y]=num++;
}
double dfs(int u,double minflow)
{
if(u==end)return minflow;
int i,v;
double f,flow=0.0;
for(i=head[u];i!=-1;i=e[i].next)
{
v=e[i].ed;
if(e[i].flow>0)
{
if(dis[v]+1==dis[u])
{
f=dfs(v,e[i].flow>minflow-flow?minflow-flow:e[i].flow);
flow+=f;
e[i].flow-=f;
e[i^1].flow+=f;
if(minflow-flow<=1e-8)return flow;
if(dis[start]>=ans)return flow;
}
}
}
if(--gap[dis[u]]==0)
dis[start]=ans;
dis[u]++;
gap[dis[u]]++;
return flow;
}
double isap()
{
double maxflow=0.0;
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
gap[0]=ans;
while(dis[start]<ans)
maxflow+=dfs(start,inf);
return maxflow;
}
int main()
{
int i,n,m,k,x,y,t;
double w;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
memset(head,-1,sizeof(head));
num=0;start=0;end=n+m+1;ans=end+1;
for(i=1;i<=n;i++)
{
scanf("%lf",&w);
addedge(start,i,log(w));
}
for(i=1;i<=m;i++)
{
scanf("%lf",&w);
addedge(i+n,end,log(w));
}
while(k--)
{
scanf("%d%d",&x,&y);
addedge(x,y+n,inf);
}
w=isap();
printf("%.4lf\n",exp(w));
}
return 0;
}
poj 3308 (最大流)的更多相关文章
- POJ - 3308 Paratroopers(最大流)
1.这道题学了个单词,product 还有 乘积 的意思.. 题意就是在一个 m*n的矩阵中,放入L个敌军的伞兵,而我军要在伞兵落地的瞬间将其消灭.现在我军用一种激光枪组建一个防御系统,这种枪可以安装 ...
- poj 3281 最大流+建图
很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很 ...
- POJ 3380 最大流
Paratroopers Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3308 Paratroopers 最大流,乘积化和 难度:2
Paratroopers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7267 Accepted: 2194 Desc ...
- poj 3308(最小点权覆盖、最小割)
题目链接:http://poj.org/problem?id=3308 思路:裸的最小点权覆盖,建立超级源点和超级汇点,将源点与行相连,容量为这行消灭敌人的代价,将列与汇点相连,容量为这列消灭敌人的代 ...
- 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 3308 Paratroopers(最小割EK(邻接表&矩阵))
Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the ...
- poj 1149 最大流
题目链接:http://poj.org/problem?id=1149 #include <cstdio> #include <cmath> #include <algo ...
随机推荐
- sqlserver练习
1.基本表的练习: create table Test( name ), age int, sex ) ) alter table Test ) alter table Test ) alter ta ...
- 批量设置AssetBundleName
using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class Change ...
- Xcode5和6上新建工程如何本地化启动页面
建议阅读本篇文章前先具备iOS本地化的基本知识,Google中搜索“iOS本地化”,有成片的教程~~ 最近有个app需要支持英语.简体中文.繁体中文,由于启动页面上有文字,所以也不得不做下本地化处理. ...
- ORA-00119: invalid specification for system parameter LOCAL_LISTENER
重启oracle是提示错误ORA-00119: invalid specification for system parameter LOCAL_LISTENER. 解决方法: 命令查看错误信息:oe ...
- 21. DNS 配置和端口检测
一.将本机的 DNS 配置为 8.8.8.8 ,用 nslookup (还可以使用 host.dig)验证 # 修改配置文件 # vim /etc/resolv.conf # 在文件的最后加入 ...
- javascript的本地存储 cookies、localStorage
一.cookies 本地存储 首先是常用的cookies方法,网上有很多相关的代码以及w3cSchool cookies. // 存储cookies function setCookie(name,v ...
- javascript,css3加载动画
html代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...
- htm跨域锚点定位
a.html <div class="title" id="aaaa">保险服务</div> b.html <div class= ...
- flex 调用WebService2(基于.net)
flex 访问WebService的方法有很多种,使用FLEX4中的"数据/服务"功能可以自动生成访问WebService的代理类,这样可以避免把所有的数据访问都写到MXML页面上 ...
- 对list代理扩展功能
package 动态代理扩展List; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; imp ...