题目大意:有n个带有裂缝的冰块。已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数。所有的企鹅都想聚集在同一个冰块上并且都不想掉进水里,如果给出每只企鹅能跳跃的最大距离D,问哪几个冰块能让所有的企鹅聚集在一起?

题目分析:枚举所有的冰块,依次判断其是否满足题意。将每一个冰块视作一个节点,以冰块间企鹅能否跳跃建边,得到一张节点带有容量的图。对于节点容量的图拆点。以当前枚举的冰块为汇点t,增加源点s,从s向所有的冰块连一条弧,容量为冰块上起始的企鹅数目,将每个冰块拆开后的内弧的容量置为该冰块所有承受的最大跳跃次数。如果最大流等于企鹅总数,则当前枚举的冰块是可以的。

代码如下:

# include<iostream>
# include<cstdio>
# include<cmath>
# include<string>
# include<vector>
# include<list>
# include<set>
# include<map>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; # define LL long long
# define REP(i,s,n) for(int i=s;i<n;++i)
# define CL(a,b) memset(a,b,sizeof(a))
# define CLL(a,b,n) fill(a,a+n,b) const double inf=1e30;
const int INF=1<<30;
const int N=1000; struct Edge
{
int fr,to,cap,fw;
Edge(int _fr,int _to,int _cap,int _fw):fr(_fr),to(_to),cap(_cap),fw(_fw){}
};
struct Dinic{
vector<Edge>edges;
vector<int>G[N];
int d[N],vis[N],cur[N];
int s,t; void init(int n,int s,int t){
this->s=s,this->t=t;
REP(i,0,n) G[i].clear();
edges.clear();
} void addEdge(int u,int v,int cap)
{
edges.push_back(Edge(u,v,cap,0));
edges.push_back(Edge(v,u,0,0));
int len=edges.size();
G[u].push_back(len-2);
G[v].push_back(len-1);
} bool BFS()
{
CL(vis,0);
d[s]=0;
vis[s]=1;
queue<int>q;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
REP(i,0,G[x].size()){
Edge &e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.fw){
d[e.to]=d[x]+1;
vis[e.to]=1;
q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t||a==0) return a;
int flow=0,f;
for(int &i=cur[x];i<G[x].size();++i){
Edge &e=edges[G[x][i]];
if(d[e.to]==d[x]+1&&(f=DFS(e.to,min(a,e.cap-e.fw)))>0){
e.fw+=f;
edges[G[x][i]^1].fw-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
} int MaxFlow()
{
int flow=0;
while(BFS()){
CL(cur,0);
flow+=DFS(s,INF);
}
return flow;
}
};
Dinic dinic; struct Floe
{
int x,y,n,m;
};
Floe f[N];
int ans[N],total;
double D; double dist(int i,int j)
{
return sqrt(1.0*(f[i].x-f[j].x)*(f[i].x-f[j].x)+1.0*(f[i].y-f[j].y)*(f[i].y-f[j].y));
} bool judge(int n,int m,int t)
{
dinic.init(n,0,t);
REP(i,1,m+1){
dinic.addEdge(0,i*2-1,f[i].n);
dinic.addEdge(i*2-1,i*2,f[i].m);
REP(j,1,m+1){
if(i==j) continue;
if(dist(i,j)<=D) dinic.addEdge(2*i,j*2-1,INF);
}
}
return dinic.MaxFlow()==total;
} int main()
{
int T,n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%lf",&m,&D);
dinic.init(m*2+2,0,m*2+1);
total=0;
REP(i,1,m+1){
scanf("%d%d%d%d",&f[i].x,&f[i].y,&f[i].n,&f[i].m);
total+=f[i].n;
}
ans[0]=0;
REP(i,1,m+1) if(judge(m*2+2,m,i*2-1)) ans[++ans[0]]=i;
if(ans[0]==0) printf("-1\n");
else REP(i,1,ans[0]+1) printf("%d%c",ans[i]-1,(i==ans[0])?'\n':' ');
}
return 0;
}

  

UVALive-3972 March of the Penguins (最大流:节点容量)的更多相关文章

  1. UVALIVE 3972 March of the Penguins

    最大流建图比较容易第一次Dicnc抄了下别人的版 存一下以后方便查 #include <map> #include <set> #include <list> #i ...

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

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

  3. [POJ 3498] March of the Penguins

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

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

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

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

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

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

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

  7. hdu 2334 March of the Penguins

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

  8. UVALive - 6571 It Can Be Arranged 最大流

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48415 It Can Be Arranged Time Limit: 3000MS 问题描述 Every y ...

  9. 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)

    [题意] 有n个队伍进行比赛,每场比赛,恰好有一支队伍取胜.一支队伍败.每个队伍需要打的比赛场数相同,给你每个队伍目前已经赢得场数和输得场数,再给你一个矩阵,第 i 行第 j 列 表示队伍 i 和队伍 ...

随机推荐

  1. Haskell中cabal install glib遇到的问题

    1. 运行命令cabal install glib时出现错误: Cannot find gtk2hsC2hs Please install `gtk2hs-buildtools` first and ...

  2. could not execute menu item系统找不到指定的文件

    Wamp3.0.6 64bit,系统任务栏图标,左键,Apache菜单,httpd.conf,报错“could not execute menu item.....系统找不到指定的文件” 根据网上的搜 ...

  3. nginx repos

    vim /etc/yum.repos.d/nginx.repo 然后将下面的内容复制进去: [nginx] name=nginx repo baseurl=http://nginx.org/packa ...

  4. Can you solve this equation?---hdu2199(二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=2199 给出y的值求x: 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 = Y x是0到100的 ...

  5. Ubuntu18.04 英文系统下安装中文输入法

    今天尝试了Ubuntu18.04LTS(依旧装的英文版)发现按照之前的方法( http://www.cnblogs.com/asmer-stone/p/5227188.html)安装中文输入法不行了, ...

  6. Maven安装(linux系统)

    解压: 修改配置: export JAVA_HOME=/usr/java/jdk1..0_80 export MAVEN_HOME=/software/apache-maven- export PAT ...

  7. js cookie的读写

    cookie是一小段信息,以键/值对的信息保存在计算机硬盘上的字符串, cookie存储容量大概在4kb,不同的浏览器厂家对cookie大小的限制有微微的差异:cookie主要的本质是“识别”,通过识 ...

  8. Python的星号(*)和双星号(**)用法

    ①引言 在Python中,星号除了用于乘法数值运算和幂运算外,还有一种特殊的用法是在变量前加单个星号或两个星号,实现多参数的传人或变量的拆解. ②什么是星号变量 最初,星号变量是用在函数的参数传递上的 ...

  9. 【运维技术】Jenkins配置使用教程

    Jenkins配置使用教程 单机jenkins启动 软件安装和启动,必须含有java环境 # 安装jdk,参考其他教程,创建文件目录 mkdir -p /app/jenkins cd /app/jen ...

  10. 从知乎了解到,为什么Mysql禁用存储过程、外键和级联?

    打开帖子直接一张醒目的图,是阿里巴巴的Java开发手册对Mysql相关的要求. 看看下面的回复 灵剑 存储过程没有版本控制,版本迭代的时候要更新很麻烦.存储过程如果和外部程序结合起来用,更新的时候很难 ...