UVALive-3972 March of the Penguins (最大流:节点容量)
题目大意:有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 (最大流:节点容量)的更多相关文章
- UVALIVE 3972 March of the Penguins
最大流建图比较容易第一次Dicnc抄了下别人的版 存一下以后方便查 #include <map> #include <set> #include <list> #i ...
- poj 3498 March of the Penguins(拆点+枚举汇点 最大流)
March of the Penguins Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 4873 Accepted: ...
- [POJ 3498] March of the Penguins
March of the Penguins Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 4378 Accepted: ...
- POJ 3498 March of the Penguins(网络最大流)
Description Somewhere near the south pole, a number of penguins are standing on a number of ice floe ...
- poj 3498 March of the Penguins(最大流+拆点)
题目大意:在南极生活着一些企鹅,这些企鹅站在一些冰块上,现在要让这些企鹅都跳到同一个冰块上.但是企鹅有最大的跳跃距离,每只企鹅从冰块上跳走时会给冰块造成损害,因此企鹅跳离每个冰块都有次数限制.找出企鹅 ...
- 【POJ3498】March of the Penguins(最大流,裂点)
题意:在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃,但是它们的跳跃距离,有一个上限. ...
- hdu 2334 March of the Penguins
题意大意 在X,Y坐标系中有N(N<=100)个冰块,有些冰块上有1若干只企鹅,每只企鹅一次最多跳M距离,一个冰块在有Mi个企鹅离开,就会消失,问有哪些冰块可以作为集合点,就是所有企鹅都能成 ...
- UVALive - 6571 It Can Be Arranged 最大流
题目链接: http://acm.hust.edu.cn/vjudge/problem/48415 It Can Be Arranged Time Limit: 3000MS 问题描述 Every y ...
- 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)
[题意] 有n个队伍进行比赛,每场比赛,恰好有一支队伍取胜.一支队伍败.每个队伍需要打的比赛场数相同,给你每个队伍目前已经赢得场数和输得场数,再给你一个矩阵,第 i 行第 j 列 表示队伍 i 和队伍 ...
随机推荐
- poj1463 Strategic game【树形DP】
Strategic game Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 9582 Accepted: 4516 De ...
- CentOS下LVM逻辑卷管理技术解释
1.LVM逻辑卷管理技术产生的背景 企业日益变化的存储需要使得传统的磁盘分区存储显得不够灵活 2.磁盘分区存储 对于这样的三个物理分区的话,迟早有一天会被数据填满,因为它是死的,无法进行缩放. 假设下 ...
- ubuntu打开txt乱码
因为不支持中文 输入命令: iconv -f gbk -t utf8 filename.txt > filename.txt.utf8
- Gunicorn独角兽
1. 关于Gunicorn Gunicorn是一个开源的Python WSGI HTTP服务器,移植于Ruby的Unicorn项目的采用pre-fork模式的服务器.Gunicorn服务器可与各种We ...
- Python开发【前端】:Ajax(二)
原生Ajax.JQuery.伪Ajax三种方式使用优先级 如果发送的是[普通数据] jQuery XMLHttpRequest iframe 如果发送的是[文件] iframe jQuery(Form ...
- 13.Github使用
我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...
- 用Python实现的数据结构与算法:双端队列
一.概述 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的线性数据结构.双端队列也拥有两端:队首(front).队尾(rear),但与队列不同的是,插入操作在两 ...
- arthas使用介绍
背景: 一次线上问题的综合排查排查,两个相同的系统的某个模块,数据量更少的系统查询更慢. 先说下整体思路: 查看系统整理负载,网络有100左右毫秒的延迟,看起来影响不大 查看正序运行整体情况,一次查询 ...
- PAT 1053 Path of Equal Weight[比较]
1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight Wi assigned t ...
- Deep Learning(2)
二.Deep Learning的基本思想和方法 实际生活中,人们为了解决一个问题,如对象的分类(对象可是是文档.图像等),首先必须做的事情是如何来表达一个对象,即必须抽取一些特征来表示一个对象,如文本 ...