题目大意:在南极生活着一些企鹅,这些企鹅站在一些冰块上,现在要让这些企鹅都跳到同一个冰块上。但是企鹅有最大的跳跃距离,每只企鹅从冰块上跳走时会给冰块造成损害,因此企鹅跳离每个冰块都有次数限制。找出企鹅可以在哪些冰块上聚齐。

解题思路:(最大流 + 拆点)把每个冰块看做一个点,然后每个点拆分成两个相连的点,容量为最大的跳走次数。添加一个源点,源点到每个冰块代表的点连边,容量为开始冰块上的企鹅数目。枚举判断每个冰块是否满足条件。

代码:

#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(最大流+拆点)的更多相关文章

  1. poj 3498 March of the Penguins(拆点+枚举汇点 最大流)

    March of the Penguins Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 4873   Accepted: ...

  2. [POJ 3498] March of the Penguins

    March of the Penguins Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 4378   Accepted:  ...

  3. POJ 3498 March of the Penguins(网络最大流)

    Description Somewhere near the south pole, a number of penguins are standing on a number of ice floe ...

  4. poj 3498 最大流

    March of the Penguins Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 4809   Accepted:  ...

  5. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

  6. poj 3498(最大流+拆点)

    题目链接:http://poj.org/problem?id=3498 思路:首先设一个超级源点,将源点与各地相连,边容量为各点目前的企鹅数量,然后就是对每个冰块i进行拆点了(i,i+n),边容量为能 ...

  7. UVALive-3972 March of the Penguins (最大流:节点容量)

    题目大意:有n个带有裂缝的冰块.已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数.所有的企鹅都 ...

  8. 【POJ3498】March of the Penguins(最大流,裂点)

    题意:在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃,但是它们的跳跃距离,有一个上限.  ...

  9. March of the Penguins

    poj3498:http://poj.org/problem?id=3498 题意:某个冰块上有a只企鹅,总共可以跳出去b只,问是否可能所有的企鹅都跳到某一块冰块上,输出所有的可能的冰块的编号. 由于 ...

随机推荐

  1. 写在学习linux内核协议栈之前

    一直很喜欢内核,但是新手,非常的痛苦啊.现在看一本linux内核协议栈源码解析一书,将自己学习的经历以及 理解记录下来,以备将来回头查漏补缺,同时校正自己的理解错误,自勉

  2. poj 2046 Gap(bfs+hash)

    Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...

  3. install-file -Dfile=J:\project01\workspace\service\lib\javapns-jdk16-163.jar -DgroupId=org.json -Dar

    今天在开发项目的时候发现了一个问题,所以通过博客来记录起来! 为了以后在问题的解决方面能得到借鉴! 问题的现象是这种: 这样会报错的.pom.xml文件他在编译.检查他的文件语法的时候是须要參考库中的 ...

  4. js jsp 时间 日期 控件 插件 简单 实用

    js时间控件一般都是找网上的用,这东西平常很少涉及到,一用到找起来却烦死人,不是没用就是太复杂,今天向大家推荐一个简单实用的控件,该控件在不断更新,而且有专门的网站对它进行维护,所以值得一看. 先说它 ...

  5. C#控制台吹泡泡算法

    代码如下: static void Main(string[] args) { Bubbling(100, 100, "O", 1000); Console.ReadLine(); ...

  6. SuperSocket学习笔记(一)

    这是根据我自己学习的经历整理出来的,如有不对之处,还请多多指教! SuperSocket源码下载 SuperSocket文档 安装并启动Telnet 学习方法: QuickStrart + 文档 参考 ...

  7. WebApi2官网学习记录---异常处理

    HttpResponseException 当WebAPI的控制器抛出一个未捕获的异常时,默认情况下,大多数异常被转为status code为500的http response即服务端错误. Http ...

  8. GDI+(Graphics Device Interface)例子

    使用SolidBrush 单色画笔 Bitmap bitmap = new Bitmap(800, 600);            Graphics graphics = Graphics.From ...

  9. Eclipse汉化后怎么改回英文版 (中文 改 英文)

    Eclipse汉化后怎么改回英文版(可切换中英文) 很多朋友将MyEclipse汉化后还想改回英文的,其实只要修改MyEclipse的配置文件就可以了,这里我以MyEclipse7.0为例演示一下如何 ...

  10. Key lock 的秘密

    研究死锁,或者观察sp_lock,有时候最恼人的莫过于你看到下面研究成果的key lock,但是却不知道究竟是哪个page 哪个row被lock住了: Exec sp_lock:   就说上面的key ...