[POJ 3498] March of the Penguins
| Time Limit: 8000MS | Memory Limit: 65536K | |
| Total Submissions: 4378 | Accepted: 1988 |
Description
Somewhere near the south pole, a number of penguins are standing on a number of ice floes. Being social animals, the penguins would like to get together, all on the same floe. The penguins do not want to get wet, so they have use their limited jump distance to get together by jumping from piece to piece. However, temperatures have been high lately, and the floes are showing cracks, and they get damaged further by the force needed to jump to another floe. Fortunately the penguins are real experts on cracking ice floes, and know exactly how many times a penguin can jump off each floe before it disintegrates and disappears. Landing on an ice floe does not damage it. You have to help the penguins find all floes where they can meet.

A sample layout of ice floes with 3 penguins on them.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
One line with the integer N (1 ≤ N ≤ 100) and a floating-point number D (0 ≤ D ≤ 100 000), denoting the number of ice pieces and the maximum distance a penguin can jump.
N lines, each line containing xi, yi, ni and mi, denoting for each ice piece its X and Y coordinate, the number of penguins on it and the maximum number of times a penguin can jump off this piece before it disappears (−10 000 ≤ xi, yi ≤ 10 000, 0 ≤ ni ≤ 10, 1 ≤ mi ≤ 200).
Output
Per testcase:
- One line containing a space-separated list of 0-based indices of the pieces on which all penguins can meet. If no such piece exists, output a line with the single number −1.
Sample Input
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
Sample Output
1 2 4
-1
经典最大流、- -
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
#define N 1010
#define M 100010 struct Edge
{
int to,next,val;
}edge[M];
int tot;
int head[N]; int pre[N];
int cur[N];
int gap[N];
int dep[N]; void init()
{
tot=;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w,int rw=)
{
edge[tot].to=v;
edge[tot].val=w;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].to=u;
edge[tot].val=rw;
edge[tot].next=head[v];
head[v]=tot++;
}
int SAP(int src,int des,int n)
{
memset(dep,,sizeof(dep));
memset(gap,,sizeof(gap));
memcpy(cur,head,sizeof(head)); int u=pre[src]=src;
int maxflow=,aug=-;
gap[]=n; while(dep[src]<n){
loop:
for(int &i=cur[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].val && dep[u]==dep[v]+){
aug==-?(aug=edge[i].val):(aug=min(aug,edge[i].val));
pre[v]=u;
u=v;
if(v==des){
maxflow+=aug;
for(u=pre[v];v!=src;v=u,u=pre[u]){
edge[cur[u]].val-=aug;
edge[cur[u]^].val+=aug;
}
aug=-;
}
goto loop;
}
}
int Min=n;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].val && Min>dep[v]){
cur[u]=i;
Min=dep[v];
}
}
if(--gap[dep[u]]==) break;
dep[u]=Min+;
gap[dep[u]]++;
u=pre[u];
}
return maxflow;
} int n,sum,k1[N],k2[N];
double d,x[N],y[N]; double dis(int i,int j)
{
return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
sum=;
scanf("%d%lf",&n,&d);
for(int i=;i<=n;i++){
scanf("%lf%lf%d%d",&x[i],&y[i],&k1[i],&k2[i]);
sum+=k1[i];
} for(int i=;i<=n;i++) add(,i,k1[i]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i!=j && dis(i,j)<=d){
add(i+n,j,INF);
}
}
}
for(int i=;i<=n;i++) add(i,i+n,k2[i]); int ans[N],ansd=;
for(int i=;i<=n;i++){
int t=SAP(,i,*n+);
if(t==sum) ans[++ansd]=i;
for(int j=;j<tot;j+=){
edge[j].val+=edge[j^].val,edge[j^].val=;
}
}
for(int i=;i<=ansd;i++){
if(i==ansd) printf("%d\n",ans[i]-);
else printf("%d ",ans[i]-);
}
if(!ansd) printf("-1\n");
}
return ;
}
[POJ 3498] March of the Penguins的更多相关文章
- 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 ...
- poj 3498 March of the Penguins(最大流+拆点)
题目大意:在南极生活着一些企鹅,这些企鹅站在一些冰块上,现在要让这些企鹅都跳到同一个冰块上.但是企鹅有最大的跳跃距离,每只企鹅从冰块上跳走时会给冰块造成损害,因此企鹅跳离每个冰块都有次数限制.找出企鹅 ...
- poj 3498 最大流
March of the Penguins Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 4809 Accepted: ...
- March of the Penguins
poj3498:http://poj.org/problem?id=3498 题意:某个冰块上有a只企鹅,总共可以跳出去b只,问是否可能所有的企鹅都跳到某一块冰块上,输出所有的可能的冰块的编号. 由于 ...
- poj 3498(最大流+拆点)
题目链接:http://poj.org/problem?id=3498 思路:首先设一个超级源点,将源点与各地相连,边容量为各点目前的企鹅数量,然后就是对每个冰块i进行拆点了(i,i+n),边容量为能 ...
- hdu 2334 March of the Penguins
题意大意 在X,Y坐标系中有N(N<=100)个冰块,有些冰块上有1若干只企鹅,每只企鹅一次最多跳M距离,一个冰块在有Mi个企鹅离开,就会消失,问有哪些冰块可以作为集合点,就是所有企鹅都能成 ...
- UVA 12125 March of the Penguins
题意: 给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1 ...
- UVALive-3972 March of the Penguins (最大流:节点容量)
题目大意:有n个带有裂缝的冰块.已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数.所有的企鹅都 ...
随机推荐
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- Python编程规范
打破一条既定规则的两个理由: 应用这个规则将导致代码可读性下降. 为了和周围的代码保持一致. 编码: 所有的 Python 脚本文件都应在文件头标上如下标识或其兼容格式的标识: # -*- codin ...
- hibernate Session
转: http://kayo.iteye.com/blog/204143 Session 接口 Session 接口对于Hibernate 开发人员来说是一个最重要的接口.然而在Hibernate 中 ...
- mysql中文乱码解决
有时服务端显示中文正常,但在客户端却显示?乱码, 首先,系统字符集, echo $LANG vi .bash_profile export $LANG=en_us.utf8 另一个是, mysql的, ...
- sql over()---转载
1.使用over子句与rows_number()以及聚合函数进行使用,可以进行编号以及各种操作.而且利用over子句的分组效率比group by子句的效率更高. 2.在订单表(order)中统计中,生 ...
- 【转】前端图片该保存为什么格式?png or jpg?
疑虑: 图片存储为web格式,该用什么格式保存呢?png?jpg?压缩比例该为多大?css spript的优劣?有时候我们可能会因为一张格式不正确的图片而导致设计品质的下降以及页面性能的降低.了解图片 ...
- 获取SqlDataReader的列名
SqlConnection thisConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionSt ...
- mysql小记--基础知识
一.事务 事务是由一组SQL语句组成的逻辑处理单元. 事务的特征ACID,即原子性.一致性.隔离性和持久性. 原子性(Atomicity)事务作为整体执行,操作要么全部执行.要么全部不执行. 一致性( ...
- ThreadLocal类的理解
首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的.各 ...
- Sina App Engine(SAE)入门教程(10)- Cron(定时任务)使用
参考资料 SAE Cron说明文档 Cron能干什么? cron 可以定时的触发一个脚本,在sae上最大的频率是一分钟一次.你可以用其来完成自己需要的业务逻辑,例如定期的抓取某些网页完菜信息的采集,定 ...