coj 1433 Defend the Bases
来源:互联网 发布:矩阵的秩的所有性质 编辑:程序博客网 时间:2024/06/05 11:59
如果把问题看成一个二分图,左边节点是军队,右边节点是基地。那么二分时间mid,对每一个时间,计算每一个点对(i,j)(i ∈军队,j∈基地)计算需要的移动时间t,如果t < mid,那么就加上这条边。然后对生成的这个图求最大匹配。因为每个节点最多能连接一条匹配边,所以对于基地节点,最多数目的匹配边能连接最多数目的基地节点。当求完最大匹配后,判断是否对基地的每一个点都存在一条匹配边。
#include <iostream>#include<stdio.h>#include<cstring>#include<algorithm>#include<cmath>#define maxn 110using namespace std;int n,m;struct Troop{ double x,y,v;};Troop a[maxn];struct Base{ double x,y;};Base b[maxn];int head[maxn];struct Edge{ int to,next;};Edge e[10010];int cnt_edge;int vis[maxn];int mat[maxn];int match;void init(){ memset(head,-1,sizeof head); memset(mat,0,sizeof mat); cnt_edge = 0; match = 0;}void add_edge(int a,int b){ e[cnt_edge].to = b; e[cnt_edge].next = head[a]; head[a] = cnt_edge; cnt_edge++;}bool crosspath(int k){ for(int i = head[k]; i !=-1; i = e[i].next) { int j = e[i].to; if(!vis[j]) { vis[j] = 1; if(!mat[j] || crosspath(mat[j])) { mat[j] = k; return true; } } } return false;}void hungary(){ for(int i = 1; i <= n; i++) { memset(vis,0,sizeof vis); if(crosspath(i)) { match++; } }}double dis(int i,int j){ double temp = sqrt((a[i].x - b[j].x)*(a[i].x - b[j].x) + (a[i].y - b[j].y)*(a[i].y - b[j].y)); return temp/a[i].v;}int main(){ while(scanf("%d %d",&n,&m)!=EOF) { for(int i = 1; i <= n; i++) { scanf("%lf %lf %lf",&a[i].x,&a[i].y,&a[i].v); } for(int i = 1; i <= m; i++) { scanf("%lf %lf",&b[i].x,&b[i].y); } double low = 0; double high = 15000; double mid; while(high - low > 0.000000001) { mid = (low + high)/2; init(); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(dis(i,j) < mid) { add_edge(i,j); } } hungary(); int flag = 1; for(int i = 1; i <= m; i++) { if(!mat[i]) { flag = 0; break; } } if(flag) high = mid; else low = mid; } printf("%.10lf\n",mid); } return 0;}
0 0
- coj 1433 Defend the Bases
- UVa355 - The Bases Are Loaded
- sgu290:Defend the Milky Way(三维凸包)
- UVa 355 - The Bases Are Loaded
- coj 1509 Build the given towers
- COJ 1326 The contest(分组背包)
- HDU 3371和COJ 1191 Connect the Cities(kruscal)
- COJ 1068: Count the Number of Cycles(向量点积)
- Defend 射击小游戏教程
- Numerical Bases
- Numerical Bases
- Prime Bases
- Prime Bases
- Two Bases
- Two Bases
- Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for
- hdu4269 Defend Jian Ge(模拟)
- coj 1061
- WPF技术触屏上的应用系列(六): 视觉冲击、超炫系统主界面、系统入口效果实现
- C语言中文件指针和文件位置指针,位置指针相关库函数
- hadoop2.4 ha(双namenode自动故障转移安装)
- Matlab回归说明
- 深入Java集合学习系列:ArrayList的实现原理
- coj 1433 Defend the Bases
- 链表应用——按字母顺序存储并输出某目录下的子文件或子目录名
- java 删除重复文件
- 【原创】Tiny6410驱动模块的构建、加载和卸载
- SystemTimeToFileTime、FileTimeToLocalFileTime、LocalFileTimeToFileTime三函数的跨平台实现
- 整形与字符型之间的相互转换
- Hive权限之审计
- Linux下安装C++开发环境(G++/Eclipse)
- Modelsim仿真查看内部信号