poj 3498 March of the Penguins(最大流+拆点)
题目大意:在南极生活着一些企鹅,这些企鹅站在一些冰块上,现在要让这些企鹅都跳到同一个冰块上。但是企鹅有最大的跳跃距离,每只企鹅从冰块上跳走时会给冰块造成损害,因此企鹅跳离每个冰块都有次数限制。找出企鹅可以在哪些冰块上聚齐。
解题思路:(最大流 + 拆点)把每个冰块看做一个点,然后每个点拆分成两个相连的点,容量为最大的跳走次数。添加一个源点,源点到每个冰块代表的点连边,容量为开始冰块上的企鹅数目。枚举判断每个冰块是否满足条件。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#define INF 0x7fffffff
using namespace std;
struct edge{
int from;
int to;
int cap;
int flow;
};
queue<int> Q;
vector<edge> e;
vector<int> g[210];
int n,T,sum,pig[110],cnt[110];
double d,x[110],y[110];
void init(){
int i;
e.clear();
for(i=0;i<=n*2+2;i++)
g[i].clear();
}
void addedge(int from,int to,int cap,int flow){
e.push_back((edge){from,to,cap,0});
e.push_back((edge){to,from,0,0});
int t = e.size();
g[from].push_back(t-2);
g[to].push_back(t-1);
}
bool solve(int s,int t){
int i,j,w,a[210],p[210],f = 0;
while(true){
memset(a,0,sizeof(a));
a[s] = INF;
while(!Q.empty())
Q.pop();
Q.push(s);
while(!Q.empty()){
int u = Q.front();
if(u == t) break;
Q.pop();
w = g[u].size();
for(i=0;i<w;i++){
int t = g[u][i];
int v = e[t].to;
if(!a[v] && e[t].cap > e[t].flow){
a[v] = a[u] < e[t].cap - e[t].flow ? a[u] : e[t].cap - e[t].flow;
p[v] = t;
Q.push(v);
}
}
}
if(a[t] == 0)
break;
int u = t;
for( ;u!=s;){
e[p[u]].flow += a[t];
e[p[u]^1].flow -= a[t];
u = e[p[u]].from;
}
f += a[t];
}
if(f == sum)
return true;
else
return false;
}
int main(){
int i,j;
cin >> T;
while(T--){
cin >> n >> d;
init();
sum = 0;
memset(pig,0,sizeof(pig));
for(i=1;i<=n;i++){
scanf("%lf%lf%d%d",&x[i],&y[i],&pig[i],&cnt[i]);
sum += pig[i];
}
for(i=1;i<=n;i++)
addedge(i,i+n,cnt[i],0);
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i] - y[j])*(y[i]-y[j])) <= d){
addedge(i+n,j,INF,0);
addedge(j+n,i,INF,0);
}
for(i=1;i<=n;i++)
if(pig[i])
addedge(0,i,pig[i],0);
int ans = 1;
int k = e.size();
for(i=1;i<=n;i++){
for(j=0;j<k;j++)
e[j].flow = 0;
if(solve(0,i))
if(ans != 1)
printf(" %d",i-1);
else{
printf("%d",i-1);
ans ++;
}
}
if(ans == 1)
printf("-1");
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: 4873 Accepted: ...
- [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(网络最大流)
Description Somewhere near the south pole, a number of penguins are standing on a number of ice floe ...
- poj 3498 最大流
March of the Penguins Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 4809 Accepted: ...
- poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap
poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...
- poj 3498(最大流+拆点)
题目链接:http://poj.org/problem?id=3498 思路:首先设一个超级源点,将源点与各地相连,边容量为各点目前的企鹅数量,然后就是对每个冰块i进行拆点了(i,i+n),边容量为能 ...
- UVALive-3972 March of the Penguins (最大流:节点容量)
题目大意:有n个带有裂缝的冰块.已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数.所有的企鹅都 ...
- 【POJ3498】March of the Penguins(最大流,裂点)
题意:在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃,但是它们的跳跃距离,有一个上限. ...
- March of the Penguins
poj3498:http://poj.org/problem?id=3498 题意:某个冰块上有a只企鹅,总共可以跳出去b只,问是否可能所有的企鹅都跳到某一块冰块上,输出所有的可能的冰块的编号. 由于 ...
随机推荐
- dp优化简单总结
1.二分优化 (使用二分查找优化查找效率) 典型例题:LIS dp[i]保存长度为 i 的上升子序列中最小的结尾,可以用二分查找优化到nlogn 2.数学优化 (通过数学结论减少状态数) 例题1:hd ...
- CSS多级数字序号的目录列表(类似3.3.1.这样的列表序号)
编写文档手册的时候,我们经常需要列表项前面的序号将上级各层的序号也附加在前面,如下图: (图一) 但默认的<ol>列表,任何层次都是单个序号开始.如下图: (图二) 要实现图一效果,方法 ...
- sphinx全文检索之PHP使用教程
以上一篇的email数据表为例: 数据结构: 01.CREATE TABLE email ( 02.emailid mediumint(8) unsigned NOT NULL auto_increm ...
- cocos2d-x ios 设置横屏/竖屏(全)
Cocos2d-x项目\iOS\RootViewController.mm文件中. 以下方法任选其一即可… 本人机子函数二ok! 函数一: (BOOL)shouldAutorotateToI ...
- JAVA并发实现三(线程的挂起和恢复)
package com.subject01; /** * 通过标识位,实现线程的挂起和回复 * com.subject01.AlternateSuspendResume.java * @author ...
- Javascript:一个优雅的时钟
实现效果: 准备工作: 1# 定时器 相关知识了解 2#javascript Date(日期)对象 3# 准备效果所用图片 实现原理: 1# 获取当前时间: var time=new Date(); ...
- 大话分页(补充)——Threadlocal封装offSet和pageSize简化分页工具类
经过前两篇文章(大话分页一.大话分页二)的介绍,我认为我想介绍的东西已经介绍完了,不过想精益求精的童鞋可以继续看本篇文章. 在第一篇文章中介绍了一个分页的工具类(具体请看大话分页一),从实现功能上来说 ...
- Lrc歌词批量下载助手 MP3歌词批量下载助手
Lrc歌词批量下载助手 MP3歌词批量下载助手 易歌词的服务器已经挂掉,各个主流播放器已不提供明确的下载Lrc服务,当上G的MP3文件遇上苦逼的播放器,二缺就诞生了!本软件就是在这种背景下诞生的 ...
- Android播放音频的两种方式
一种使用MediaPlayer,使用这种方式通常是播放比较长的音频,如游戏中的背景音乐. 代码如下: private MediaPlayer mPlayer = null; mPlayer = Med ...
- the process cannot access the file because it is being used by another process
当在IIS中改动绑定的port号后启动时遇到例如以下错误,表明你的port号已经被占用了 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdmljMDIyOA ...