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

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

代码:

#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. 编程:C#调用Python模块

    当下,C#与Python都是比较热门的计算机编程语言,他们各有优缺点,如果能让他们互相配合工作,那是多么美好的事情,今天我来讲解一下如何利用C#来调用Python. 如果让C#支持调用Python模块 ...

  2. hdu5045:带权二分图匹配

    题目大意 : n个人 做m道题,其中 每连续的n道必须由不同的人做 已知第i人做出第j题的概率为pij,求最大期望 思路:考虑每连续的n道题 都要n个人来做,显然想到了带权的二分图匹配 然后就是套模板 ...

  3. javaweb 登录注册

    1:用户登录界面 login.jsp <%@ page language="java" import="java.util.*" pageEncoding ...

  4. cumber + selenium +java自动化测试

    1.新建一个maven项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo ...

  5. MyBatis 通过包含的jdbcType类型和java中对应的数据类型

    MyBatis 通过包含的jdbcType类型 BIT         FLOAT      CHAR           TIMESTAMP       OTHER       UNDEFINED ...

  6. 初学者学Java设计模式(一)------单例设计模式

    单例设计模式 单例设计模式是指一个类只会生成一个对象,优点是他可以确保所有对象都访问唯一实例. 具体实现代码如下: public class A { public static void main(S ...

  7. POJ 1322 Chocolate

    Chocolate Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8245   Accepted: 2186   Speci ...

  8. the process cannot access the file because it is being used by another process

    当在IIS中改动绑定的port号后启动时遇到例如以下错误,表明你的port号已经被占用了 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdmljMDIyOA ...

  9. SQL Server常用脚本

    一.迁移登录用户脚本: select 'create login [' + p.name + '] ' + case when p.type in('U','G') then 'from window ...

  10. ASP.net获取当前页面的文件名,参数,域名等方法

    ASP.net后台获取当前页面的文件名 System.IO.Path.GetFileName(Request.Path).ToString(); 获取当前页面文件名,参数,域名等方法 假设当前页完整地 ...