最近的题解的故事背景割。

题目:
描述

在靠近南极的某处,一些企鹅站在许多漂浮的冰块上。由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上。企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃,但是它们的跳跃距离有一个上限。
随着气温的升高,冰块开始融化,并出现了裂痕。而企鹅跳跃的压力,使得冰块的破裂加速。幸运的是,企鹅对冰块十分有研究,它们能知道每块冰块最多能承受多少次跳跃。对冰块的损害只在跳起的时候产生,而落地时并不对其产生伤害。
现在让你来帮助企鹅选择一个冰面使得它们可以聚集到一起。

输入

第一行一个正数:数据个数,最多100个。之后:

第一行整数N,和浮点数D,表示冰块的数目和企鹅的最大跳跃距离。
(1≤N ≤100) (0 ≤D ≤100 000),
接下来N行,xi, yi, ni and mi,分别表示冰块的X和Y坐标,该冰块上的企鹅数目,以及还能承受起跳的次数。

输出

每个数据:

输出所有可能的相聚冰块的编号,以0开始。如果不能相遇,输出-1。

样例输入

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
样例输出

1 2 4
-1

攒的有点多先写一道。
简单点说:网络流的题,自己建一个源点,源点连向每个冰块,容量为该冰块企鹅数。
冰块分为入点和出点,入点到出点容量为起跳次数。
一个冰块能跳到另一个冰块,则前者出点连后者入点,容量INF,后者出点连前者入点,容量INF。
枚举汇点。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
const int INF=;
struct{
int next;
int to;
int w;
int cun;
}edge[maxn*maxn];
int head[maxn],cnt=-;
void add(int u,int v,int w){//u起点v终点w容量
cnt++;
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
edge[cnt].cun=w;
head[u]=cnt;
}
int lev[maxn],cur[maxn];//lev层数,cur[i]为以i为起点的边的编号
bool bfs(int m,int zong){//m为汇点,有zong个点
int dui[zong+],r=;//队列和右指针
for(int i=;i<=zong;i++){//初始化
lev[i]=-;
cur[i]=head[i];
}
dui[]=,lev[]=;
int u,v;//u起点v终点
for(int l=;l<=r;l++){//左指针
u=dui[l];
for(int e=head[u];e!=-;e=edge[e].next){
v=edge[e].to;
if(edge[e].w>&&lev[v]==-){//1.能走 2.未分层
lev[v]=lev[u]+;
r++;
dui[r]=v;//v入队
if(v==m)return ;//分层完毕
}
}
}
return ;//无法分层
}
int dinic(int u,int flow,int m){//u当前点,flow为下面的点能够分配多大的流量,m终点
if(u==m)return flow;//终点直接全流入
int res=,delta;//res实际流量
for(int &e=cur[u];e!=-;e=edge[e].next){//'&'相当于cur[u]=e;即流满的边不会再被扫一次
int v=edge[e].to;
if(edge[e].w>&&lev[u]<lev[v]){//只能从低层往高层流
delta=dinic(v,min(edge[e].w,flow-res),m);
if(delta>){//如果增广
edge[e].w-=delta;//正向边容量减少
edge[e^].w+=delta;//反向边仍量增加(暗示退流)
res+=delta;//扩张流量增加
if(res==flow)break;//可流的都流完了,及时跳出
}
}
}
if(res!=flow)lev[u]=-;//没流完,说明以后不能从这个点流出任何流量,那就不需要这个点了
return res;
}
double suan(int a1,int b1,int a2,int b2){
return sqrt(pow((a2-a1),)+pow((b2-b1),));
}
int a[],b[],c[],e[];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
double d;
int sum=;
scanf("%d%lf",&n,&d);
for(int i=;i<=maxn;i++){
head[i]=-;
}
cnt=-;
for(int i=;i<=n;i++){
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&e[i]);
add(i*,i*+,e[i]);
add(i*+,i*,);
sum+=c[i];
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j)continue;
if(suan(a[i],b[i],a[j],b[j])<=d){
add(i*+,j*,INF);
add(j*,i*+,);
}
}
}
for(int i=;i<=n;i++){
if(c[i]!=){
add(,i*,c[i]);
add(i*,,);
}
}
int ok=;
int first=;
for(int i=;i<=n;i++){
int zui=i*;
int ans=;
while(bfs(zui,*n+)==){
ans+=dinic(,INF,zui);
}
if(ans==sum){
if(first!=)printf(" ");
printf("%d",i-);//编号以0开始所以-1(才不是我懒得改了)
first=;
ok=;
}
for(int i=;i<=cnt;i++){
edge[i].w=edge[i].cun;
}
}
if(ok==)printf("-1");
printf("\n");
}
return ;
}
 

POJ3498: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. 【POJ3498】March of the Penguins(最大流,裂点)

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

  4. March of the Penguins

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

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

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

  6. hdu 2334 March of the Penguins

      题意大意 在X,Y坐标系中有N(N<=100)个冰块,有些冰块上有1若干只企鹅,每只企鹅一次最多跳M距离,一个冰块在有Mi个企鹅离开,就会消失,问有哪些冰块可以作为集合点,就是所有企鹅都能成 ...

  7. poj 3498 March of the Penguins(最大流+拆点)

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

  8. UVA 12125 March of the Penguins

    题意: 给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1 ...

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

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

随机推荐

  1. facebook原生广告添加adchoice图标

    1.在需要显示adchoice的地方添加一个textview: <LinearLayout android:id="@+id/ad_ic_action" android:la ...

  2. 01-JVM内存模型:程序计数器

    一.JVM模型概述 java虚拟机(JVM)在java程序运行的过程中,会将它所管理的内存划分为若干个不同的数据区域,这些区域有的随着JVM的启动而创建,有的随着用户线程的启动和结束而建立和销毁.一个 ...

  3. php单例模式和工厂模式

    单例模式:防止重复实例化,避免大量的new操作,减少消耗系统和内存的资源,使得有且仅有一个实例对象 header("Content-type: text/html; charset=utf- ...

  4. 如何实现iframe页面与父级页面js交互

    处理办法:1.同一域下,相同端口2.父级.子集页面上同时标记 document.domain = "xxx.com" 操作内部元素:1.jQuery使用 iframe.conten ...

  5. Java基础知识:Java实现Map集合二级联动3

    * Returns an image stored in the file at the specified path * @param path String The path to the ima ...

  6. 日历(Calendar)模块

    #usr/bin/python3 #! -*-conding : utf-8 -*- #2018.3.14 """ 日历(Calendar)模块 此模块的函数都是日历相关 ...

  7. 小米 OJ 编程比赛 02 月常规赛

    Carryon 数数字 描述 Carryon 最近迷上了数数字,然后 Starry 给了他一个区间[l,r] ,然后提了几个要求, 需要将 ll 到 rr 之间的数全部转化成 16 进制,然后连起来. ...

  8. POJ 1696 Space Ant(凸包变形)

    Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scie ...

  9. 关于set和get机制的整理

    首先这是es5新增的:定义是设置和获取对象属性时候出发的方法,属于修饰器: 犀牛书例子: function test(n){ return { get count(){ return n }, set ...

  10. Android 上实现非root的 Traceroute -- 非Root权限下移植可执行二进制文件 脚本文件

    作者 : 万境绝尘 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/36438365 示例代码下载 : -- CSDN : htt ...