poj 3498 March of the Penguins(拆点+枚举汇点 最大流)
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 4873 | Accepted: 2220 |
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
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=1e9;
const int N=+;
int b[N];
int head[N];
int tot;
struct node{
int to,next,flow;
}edge[N<<];
struct Node{
double x,y;
int m,k;
}a[N];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int flow){
edge[tot].to=v;
edge[tot].flow=flow;
edge[tot].next=head[u];
head[u]=tot++; edge[tot].to=u;
edge[tot].flow=;
edge[tot].next=head[v];
head[v]=tot++;
}
int dis[N];
int BFS(int s,int t){
queue<int>q;
memset(dis,-,sizeof(dis));
q.push(s);
dis[s]=;
while(!q.empty()){
int x=q.front();
q.pop();
if(x==t)return ;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(dis[v]==-&&edge[i].flow){
dis[v]=dis[x]+;
q.push(v);
}
}
}
if(dis[t]==-)return ;
return ;
}
int DFS(int s,int flow,int t){
if(s==t)return flow;
int ans=;
for(int i=head[s];i!=-;i=edge[i].next){
//cout<<34<<endl;
int v=edge[i].to;
if(edge[i].flow&&dis[v]==dis[s]+){
int f=DFS(v,min(flow-ans,edge[i].flow),t);
edge[i].flow-=f;
edge[i^].flow+=f;
ans+=f;
if(ans==flow)return ans;
}
}
return ans;
}
int Dinc(int s,int t){
int flow=;
while(BFS(s,t)){
// cout<<4<<endl;
flow=flow+DFS(s,INF,t);
}
return flow;
}
int main(){
int tt;
scanf("%d",&tt);
while(tt--){
int n;
double d;
int s,t;
scanf("%d%lf",&n,&d);
int sum=;
for(int i=;i<n;i++){
scanf("%lf%lf%d%d",&a[i].x,&a[i].y,&a[i].m,&a[i].k);
sum=sum+a[i].m;
}
s=*n+;
t=*n+;
int ss=;
for(int kk=;kk<n;kk++){
init();
add(kk,t,INF);
for(int i=;i<n;i++){
add(i,i+n,a[i].k);
add(s,i,a[i].m);
for(int j=i+;j<n;j++){
double dd=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
if(dd<=d){
add(i+n,j,INF);
add(j+n,i,INF);
}
}
}
if(Dinc(s,t)==sum){
// cout<<kk<<endl;
b[ss++]=kk;
}
}
if(ss==)cout<<-<<endl;
else{
cout<<b[];
for(int i=;i<ss;i++)cout<<" "<<b[i];
cout<<endl;
}
}
}
poj 3498 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(最大流+拆点)
题目大意:在南极生活着一些企鹅,这些企鹅站在一些冰块上,现在要让这些企鹅都跳到同一个冰块上.但是企鹅有最大的跳跃距离,每只企鹅从冰块上跳走时会给冰块造成损害,因此企鹅跳离每个冰块都有次数限制.找出企鹅 ...
- 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(最大流+拆点)
题目链接:http://poj.org/problem?id=3498 思路:首先设一个超级源点,将源点与各地相连,边容量为各点目前的企鹅数量,然后就是对每个冰块i进行拆点了(i,i+n),边容量为能 ...
- March of the Penguins
poj3498:http://poj.org/problem?id=3498 题意:某个冰块上有a只企鹅,总共可以跳出去b只,问是否可能所有的企鹅都跳到某一块冰块上,输出所有的可能的冰块的编号. 由于 ...
- poj 3498 最大流
March of the Penguins Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 4809 Accepted: ...
- UVA 1658 海军上将(拆点法+最小费用限制流)
海军上将 紫书P375 这题我觉得有2个难点: 一是拆点,要有足够的想法才能把这题用网络流建模,并且知道如何拆点. 二是最小费用限制流,最小费用最大流我们都会,但如果限制流必须为一个值呢?比如这题限制 ...
- Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负
/** 题目:Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负 链接:https://vjudge.net/problem/UVA-1161 ...
- POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)
http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路 ...
随机推荐
- 【PostgreSQL-9.6.3】事件触发器
当预定的事件发生时,事件触发器就会被触发.由于事件触发器设计的权限比较大,所以只有超级用户才能创建和修改触发器. 1. 事件触发器支持的事件分三类:ddl_command_start, ddl_com ...
- Python语言之数据结构1(序列--列表,元组,字符串)
0.序列 列表,元组,字符串都是序列. 序列有两个特点:索引操作符和切片操作符.索引操作符让我们可以从序列中抓取一个特定项目.切片操作符让我们能够获取序列的一个切片,即一部分序列. 以字符串为例: 1 ...
- 【技术累积】【点】【java】【28】Map遍历
Map遍历 map的遍历一般有几种吧 遍历entrySet for(Map.Entry<String,String> entry : map.entrySet()){ } Iterator ...
- logstash windows下添加服务启动管理
nssm下载链接:http://nssm.cc/release/nssm-2.24.zip
- 谷歌浏览器中a:link设置字体颜色无效问题
<div id="box"> <a href="#">111111</a> <a href=""& ...
- [如何在mac下使用gulp] 2. gulp模块的常用方法
常用的gulp模块方法有: gulp.src() gulp.src('client/one.js'); //指定明确的要处理文件 gulp.src('client/*.js'); //处理client ...
- 爬虫文件存储-3:Redis
前提条件: 安装并运行redis服务端程序,安装RedisPy库 说明:Redis 是 StrictRedis 的子类,它的主要功能是用于向后兼容旧版本库里的几个方法,官方推荐使用 StrictRed ...
- jquery源码分析(三)——工具函数
jQuery.extend({ expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "&quo ...
- 【codeforces 527B】Error Correct System
[题目链接]:http://codeforces.com/contest/527/problem/B [题意] 给你两个字符串; 允许你交换一个字符串的两个字符一次: 问你这两个字符串最少会有多少个位 ...
- Excel 2010/2013/2016在鼠标右键新建xls或xlsx文件后,打开报错“无法打开文件”“文件格式或文件扩展名无效”
近段时间,陆续有两个同事先后出现同样的问题(在Excel多个版本都可能出现),问题描述: 当用鼠标右键在任意文件夹或电脑桌面“新建”→“ Microsoft Excel 工作表”,再用鼠标双击打开这个 ...