UVA 12125 March of the Penguins
题意:
给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1
分析:
很显然的最大流问题。把每个冰块x拆成x和x',连x->x'流量为跳出的次数限制。枚举落脚冰块建图跑最大流即可
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
const int maxn = 202 + 10;
const int INF = 1000000000;
struct Edge
{
int from,to,cap,flow;
};
struct Dinic
{
int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void clearall(int n)
{
for(int i=0;i<n;i++)
G[i].clear();
edges.clear();
}
void clearflow()
{
for(int i=0;i<edges.size();i++)
edges[i].flow=0;
}
void addedge(int from,int to,int cap)
{
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int>Q;
Q.push(s);
vis[s]=1;
d[s]=0;
while(!Q.empty())
{
int x=Q.front();
Q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==0)
{
return a;
}
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow -= f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
return flow;
}
int maxflow(int s,int t)
{
this->s=s;
this->t=t;
int flow=0;
//cout<<2<<endl;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
//cout<<3<<endl;
return flow;
}
};
struct Node
{
int x,y,num;
}p[maxn];
Dinic solver;
double dis(Node a,Node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
vector<int>ans;
int main()
{
int t,n,total;
double D;
scanf("%d",&t);
while(t--)
{
scanf("%d%lf",&n,&D);
solver.clearall(2*n+1);
int s1=0;
total=0;
D=D*D;
for(int i=1;i<=n;i++)
{
int x,y,ni,cap;
scanf("%d%d%d%d",&x,&y,&ni,&cap);
p[i].x=x;p[i].y=y;p[i].num=ni;
total+=ni;
solver.addedge(s1,i,ni);
solver.addedge(i,i+n,cap);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i != j && D-dis(p[i],p[j])> 1e-6)
{
solver.addedge(i+n,j,INF);
}
int sum=0;
ans.clear();
//cout<<1<<endl;
for(int i=1;i<=n;i++)
{
solver.clearflow();
if(solver.maxflow(s1,i)==total)
{
ans.push_back(i);
sum++;
}
}
//cout<<sum<<endl;
if(sum==0)
printf("-1\n");
else
{
for(int i=0;i<ans.size()-1;i++)
printf("%d ",ans[i]-1);
printf("%d\n",ans[ans.size()-1]-1);
}
}
}
输入:
2
5 3.5
1 1 1 1
2 3 0 1
3 5 1 1
5 1 1 1
5 4 0 1
3 1.1
-1 0 5 10
0 0 3 9
2 0 1 1
UVA 12125 March of the Penguins的更多相关文章
- [POJ 3498] March of the Penguins
March of the Penguins Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 4378 Accepted: ...
- poj 3498 March of the Penguins(拆点+枚举汇点 最大流)
March of the Penguins Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 4873 Accepted: ...
- POJ 3498 March of the Penguins(网络最大流)
Description Somewhere near the south pole, a number of penguins are standing on a number of ice floe ...
- hdu 2334 March of the Penguins
题意大意 在X,Y坐标系中有N(N<=100)个冰块,有些冰块上有1若干只企鹅,每只企鹅一次最多跳M距离,一个冰块在有Mi个企鹅离开,就会消失,问有哪些冰块可以作为集合点,就是所有企鹅都能成 ...
- March of the Penguins
poj3498:http://poj.org/problem?id=3498 题意:某个冰块上有a只企鹅,总共可以跳出去b只,问是否可能所有的企鹅都跳到某一块冰块上,输出所有的可能的冰块的编号. 由于 ...
- poj 3498 March of the Penguins(最大流+拆点)
题目大意:在南极生活着一些企鹅,这些企鹅站在一些冰块上,现在要让这些企鹅都跳到同一个冰块上.但是企鹅有最大的跳跃距离,每只企鹅从冰块上跳走时会给冰块造成损害,因此企鹅跳离每个冰块都有次数限制.找出企鹅 ...
- UVALive-3972 March of the Penguins (最大流:节点容量)
题目大意:有n个带有裂缝的冰块.已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数.所有的企鹅都 ...
- POJ3498:March of the Penguins——题解
最近的题解的故事背景割. 题目: 描述 在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃, ...
- UVALIVE 3972 March of the Penguins
最大流建图比较容易第一次Dicnc抄了下别人的版 存一下以后方便查 #include <map> #include <set> #include <list> #i ...
随机推荐
- solr group分组查询
如:http://localhost:8080/solr/test_core/select?q=*:*&wt=json&indent=true&group=true&g ...
- Javascript的性能瓶颈
Javascript是单线程的,它的性能瓶颈在于频繁的DOM操作, 因为每次操作都会使浏览器重新绘制一次. 其实纯JS的执行的速度是很快的,可以把元素都攒到一块,一次性放到页面中. 或者,定义一个延时 ...
- Android 平滑图片加载和缓存库 Glide 使用详解
在图片加载库烂大街的今天,选择一个适合自己使用的图片加载库已经成为了每一个Android开发者的必经之路.现在市面上知名的图片加载库有UIL,Picasso,Volley ImageLoader,Fr ...
- PHP学习笔记三十五【Try】
<?php function AddUser($name) { if($name=="张三") { echo "add success"; return ...
- windows xp通过VNC viewer远程连接RHEL5桌面
环境: [root@localhost ~]# cat /etc/issue Red Hat Enterprise Linux Server release 5.2 (Tikanga) Kernel ...
- 改变navigationbar的底部线条颜色
[[UINavigationBar appearance] setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault]; CG ...
- hdu120118岁生日
Problem Description Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所 ...
- 跨平台渲染框架尝试 - Texture管理
纹理是渲染器重要的资源,也是比较简单的资源.本文将详细讨论纹理资源的管理. 在资源管理概述中提到,资源就是一堆内存和CPU与GPU的访问权限.纹理管理在资源管理之上,要负责如何使用者一堆内存构造纹理对 ...
- MYSQL存储过程中-流程控制语句
存储过程中常用的流程控制 复习下存储过程内部的语法 定义存储过程体的局部变量: 定义方法:DECLARE a INT DEFAULT 100或者DECLARE a INT ; SET a=100; ...
- Shell使用
http://www.cnblogs.com/hbt19860104/archive/2012/08/14/2638952.html http://blog.csdn.net/tttyd/articl ...