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

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

代码:

#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. 关于<ul><ol><li>的用法

    <ul>:无序列表 <ol>:有序列表 <li>:行. 想要去掉前面的序号和点可以在<ol>或<ul>style中用list-style: ...

  2. WPF - XAML如何引入名字空间

    WPF 的XAML引入名字空间的概念,经常容易让人混淆.如何引入名字空间,并且在XAML中调用其中的类,下面给一个简单的介绍. 比如我们有一个Hepler类. namespace Wheat.PIMS ...

  3. iOS 部分问题总结2 - 苹果审核篇

    iOS 部分问题总结(二) - 苹果审核篇 1. 记录下5.1新规后上传被拒的问题排查和解决过程. 几天前,最新一次的更新被拒了,提示Invaild Binary.好在苹果同时发来了说明邮件做了详细说 ...

  4. Linux RAR 安装和使用详细说明

    描述:Linux默认自带ZIP压缩,最大支持4GB压缩,RAR的压缩比大于4GB.  流程:下载 >安装 > 使用  ----------------------------------- ...

  5. LoadRuner性能测试之内存分析方法及步骤(Windows)

    1.首先观察Available  Mbytes(可用内存),至少要>=1/2的内存空间 2.然后观察Pages/sec值是不是很大 3.再观察Page  Faules/sec是不是很大,其值表示 ...

  6. 理解JavaScript 的原型属性

    1.原型继承 面向对象编程可以通过很多途径实现.其他的语言,比如 Java,使用基于类的模型实现: 类及对象实例区别对待.但在 JavaScript 中没有类的概念,取而代之的是一切皆对象.JavaS ...

  7. Oracle 用户、对象权限、系统权限

    --================================ --Oracle 用户.对象权限.系统权限 --================================  一.用户与模式 ...

  8. 解决linux不能使用chmod更改权限的问题

    本人安装的是win10和ubuntu的双系统,发现在ubuntu下挂载windows硬盘不用命令chmod更改文件的权限,解决方法记录如下: 对于使用命令$ chmod 777 dirname更改不了 ...

  9. <经验杂谈>C#/.Net中xml的Serialization序列化与DeSerializetion反序列化

    1.先讲概念:.Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象.Serializ ...

  10. Javascript进阶篇——(DOM—节点---属性、访问节点)—笔记整理

    节点属性在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType : ...