POJ 3498 最大流
2017-02-26 00:44
330 查看
March of the Penguins
Description
Somewhere near the south pole, a number of penguins are standing on a number of ice floes. Being social animals, the penguins would like to get together, all on the same floe. The penguins do not want to get wet, so they have use their limited jump distance
to get together by jumping from piece to piece. However, temperatures have been high lately, and the floes are showing cracks, and they get damaged further by the force needed to jump to another floe. Fortunately the penguins are real experts on cracking ice
floes, and know exactly how many times a penguin can jump off each floe before it disintegrates and disappears. Landing on an ice floe does not damage it. You have to help the penguins find all floes where they can meet.
A sample layout of ice floes with 3 penguins on them.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
One line with the integer N (1 ≤ N ≤ 100) and a floating-point number D (0 ≤ D ≤ 100 000), denoting the number of ice pieces and the maximum distance a penguin can jump.
N lines, each line containing xi, yi, ni and mi, denoting for each ice piece its X and Y coordinate, the number of penguins on it and the maximum number
of times a penguin can jump off this piece before it disappears (−10 000 ≤ xi, yi ≤ 10 000, 0 ≤ ni ≤ 10, 1 ≤ mi ≤ 200).
Output
Per testcase:
One line containing a space-separated list of 0-based indices of the pieces on which all penguins can meet. If no such piece exists, output a line with the single number −1.
Sample Input
Sample Output
题意:一些企鹅站在浮冰上,想要见到别的企鹅。企鹅不想变湿,所以他们直接从一个浮冰跳到另一个浮冰。
每个浮冰有一个跳跃次数上限,可供企鹅从当前浮冰跳到别的浮冰。
问有哪些浮冰可以让所有企鹅在这碰面,输出浮冰的序号(序号下标从0开始)。如果不存在这样的浮冰,输出-1。
题解:把一个点i拆成ai和bi,ai代表这个点的入口,bi代表出口,那么ai流向bi的流量只能是这块浮冰最大的跳跃数。
然后暴力寻找当前浮冰能跳到哪上面,bi跳到它的aj上,因为ai限制了bi,所以bi到aj的流量可以是无穷。
定义起点为0,那么0需要与每个i连边,流量是第i块浮冰的企鹅数。
对于判断每块浮冰是否符合条件,只要在ai处加一条流往另一个点无穷的流量作为终点,判断0到这个点的最大流量是否等于sum即可。
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;
const int Ni = 310;
const int MAX = 1<<26;
struct Edge{
int u,v,c;
int next;
}edge[2000*Ni];
int n,m;
int edn;//边数
int p[Ni];//父亲
int d[Ni];
int sp,tp;//原点,汇点
vector<int>spp;
struct node{
double x,y;
int num,dis;
}e[305];
void addedge(int u,int v,int c)
{
edge[edn].u=u; edge[edn].v=v; edge[edn].c=c;
edge[edn].next=p[u]; p[u]=edn++;
edge[edn].u=v; edge[edn].v=u; edge[edn].c=0;
edge[edn].next=p[v]; p[v]=edn++;
}
int bfs()
{
queue <int> q;
memset(d,-1,sizeof(d));
d[sp]=0;
q.push(sp);
while(!q.empty())
{
int cur=q.front();
q.pop();
for(int i=p[cur];i!=-1;i=edge[i].next)
{
int u=edge[i].v;
if(d[u]==-1 && edge[i].c>0)
{
d[u]=d[cur]+1;
q.push(u);
}
}
}
return d[tp] != -1;
}
int dfs(int a,int b)
{
int r=0;
if(a==tp)return b;
for(int i=p[a];i!=-1 && r<b;i=edge[i].next)
{
int u=edge[i].v;
if(edge[i].c>0 && d[u]==d[a]+1)
{
int x=min(edge[i].c,b-r);
x=dfs(u,x);
r+=x;
edge[i].c-=x;
edge[i^1].c+=x;
}
}
if(!r)d[a]=-2;
return r;
}
int dinic(int sp,int tp)
{
int total=0,t;
while(bfs())
{
while(t=dfs(sp,MAX))
total+=t;
}
return total;
}
int main()
{
int i,u,v,c,t,j,k;
double D;
scanf("%d",&t);
while(t--)
{
int sum=0;
scanf("%d%lf",&n,&D);
for(i=1;i<=n;i++){
scanf("%lf%lf%d%d",&e[i].x,&e[i].y,&e[i].num,&e[i].dis);
sum+=e[i].num;
}
spp.clear();
sp=0;
for(i=1;i<=n;i++){
memset(p,-1,sizeof(p));
edn=0;//初始化
for(k=1;k<=n;k++){
for(j=k+1;j<=n;j++){
if((e[k].x-e[j].x)*(e[k].x-e[j].x)+(e[k].y-e[j].y)*(e[k].y-e[j].y)<D*D){
addedge(k+100,j,2005);
addedge(j+100,k,2005);
}
}
addedge(k,k+100,e[k].dis);
addedge(0,k,e[k].num);
}
tp=i+200;
addedge(i,i+200,2005);
int ans=dinic(sp,tp);
if(ans==sum){
spp.push_back(i-1);
}
}
if(spp.empty())printf("-1\n");
else{
for(i=0;i<spp.size();i++){
if(i)printf(" %d",spp[i]);
else printf("%d",spp[i]);
}
printf("\n");
}
}
return 0;
}
Time Limit: 8000MS | Memory Limit: 65536K | |
Somewhere near the south pole, a number of penguins are standing on a number of ice floes. Being social animals, the penguins would like to get together, all on the same floe. The penguins do not want to get wet, so they have use their limited jump distance
to get together by jumping from piece to piece. However, temperatures have been high lately, and the floes are showing cracks, and they get damaged further by the force needed to jump to another floe. Fortunately the penguins are real experts on cracking ice
floes, and know exactly how many times a penguin can jump off each floe before it disintegrates and disappears. Landing on an ice floe does not damage it. You have to help the penguins find all floes where they can meet.
A sample layout of ice floes with 3 penguins on them.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
One line with the integer N (1 ≤ N ≤ 100) and a floating-point number D (0 ≤ D ≤ 100 000), denoting the number of ice pieces and the maximum distance a penguin can jump.
N lines, each line containing xi, yi, ni and mi, denoting for each ice piece its X and Y coordinate, the number of penguins on it and the maximum number
of times a penguin can jump off this piece before it disappears (−10 000 ≤ xi, yi ≤ 10 000, 0 ≤ ni ≤ 10, 1 ≤ mi ≤ 200).
Output
Per testcase:
One line containing a space-separated list of 0-based indices of the pieces on which all penguins can meet. If no such piece exists, output a line with the single number −1.
Sample Input
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
Sample Output
1 2 4 -1
题意:一些企鹅站在浮冰上,想要见到别的企鹅。企鹅不想变湿,所以他们直接从一个浮冰跳到另一个浮冰。
每个浮冰有一个跳跃次数上限,可供企鹅从当前浮冰跳到别的浮冰。
问有哪些浮冰可以让所有企鹅在这碰面,输出浮冰的序号(序号下标从0开始)。如果不存在这样的浮冰,输出-1。
题解:把一个点i拆成ai和bi,ai代表这个点的入口,bi代表出口,那么ai流向bi的流量只能是这块浮冰最大的跳跃数。
然后暴力寻找当前浮冰能跳到哪上面,bi跳到它的aj上,因为ai限制了bi,所以bi到aj的流量可以是无穷。
定义起点为0,那么0需要与每个i连边,流量是第i块浮冰的企鹅数。
对于判断每块浮冰是否符合条件,只要在ai处加一条流往另一个点无穷的流量作为终点,判断0到这个点的最大流量是否等于sum即可。
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;
const int Ni = 310;
const int MAX = 1<<26;
struct Edge{
int u,v,c;
int next;
}edge[2000*Ni];
int n,m;
int edn;//边数
int p[Ni];//父亲
int d[Ni];
int sp,tp;//原点,汇点
vector<int>spp;
struct node{
double x,y;
int num,dis;
}e[305];
void addedge(int u,int v,int c)
{
edge[edn].u=u; edge[edn].v=v; edge[edn].c=c;
edge[edn].next=p[u]; p[u]=edn++;
edge[edn].u=v; edge[edn].v=u; edge[edn].c=0;
edge[edn].next=p[v]; p[v]=edn++;
}
int bfs()
{
queue <int> q;
memset(d,-1,sizeof(d));
d[sp]=0;
q.push(sp);
while(!q.empty())
{
int cur=q.front();
q.pop();
for(int i=p[cur];i!=-1;i=edge[i].next)
{
int u=edge[i].v;
if(d[u]==-1 && edge[i].c>0)
{
d[u]=d[cur]+1;
q.push(u);
}
}
}
return d[tp] != -1;
}
int dfs(int a,int b)
{
int r=0;
if(a==tp)return b;
for(int i=p[a];i!=-1 && r<b;i=edge[i].next)
{
int u=edge[i].v;
if(edge[i].c>0 && d[u]==d[a]+1)
{
int x=min(edge[i].c,b-r);
x=dfs(u,x);
r+=x;
edge[i].c-=x;
edge[i^1].c+=x;
}
}
if(!r)d[a]=-2;
return r;
}
int dinic(int sp,int tp)
{
int total=0,t;
while(bfs())
{
while(t=dfs(sp,MAX))
total+=t;
}
return total;
}
int main()
{
int i,u,v,c,t,j,k;
double D;
scanf("%d",&t);
while(t--)
{
int sum=0;
scanf("%d%lf",&n,&D);
for(i=1;i<=n;i++){
scanf("%lf%lf%d%d",&e[i].x,&e[i].y,&e[i].num,&e[i].dis);
sum+=e[i].num;
}
spp.clear();
sp=0;
for(i=1;i<=n;i++){
memset(p,-1,sizeof(p));
edn=0;//初始化
for(k=1;k<=n;k++){
for(j=k+1;j<=n;j++){
if((e[k].x-e[j].x)*(e[k].x-e[j].x)+(e[k].y-e[j].y)*(e[k].y-e[j].y)<D*D){
addedge(k+100,j,2005);
addedge(j+100,k,2005);
}
}
addedge(k,k+100,e[k].dis);
addedge(0,k,e[k].num);
}
tp=i+200;
addedge(i,i+200,2005);
int ans=dinic(sp,tp);
if(ans==sum){
spp.push_back(i-1);
}
}
if(spp.empty())printf("-1\n");
else{
for(i=0;i<spp.size();i++){
if(i)printf(" %d",spp[i]);
else printf("%d",spp[i]);
}
printf("\n");
}
}
return 0;
}
相关文章推荐
- poj 3498(枚举人数+最大流) 满满都是泪
- POJ 3498 March of the Penguins(枚举+最大流)
- poj 3498(最大流+拆点)
- POJ 3498 拆点 最大流
- poj 3498(最大流+拆点)
- poj 3498 March of the Penguins 带点容量限制的做法是拆点,把一个点拆成两个点,他们直接连一条边就是点容量。然后再求最大流
- poj 3498 March of the Penguins(最大流+拆点)
- poj 3498(枚举加最大流dinic)
- POJ3498最大流,枚举终点,企鹅,基础最大流
- POJ 3498 - March of the Penguins 拆点构图最大流
- POJ 3498 最大流
- poj 3498(枚举汇点的最大流)
- POJ 3498 March of the Penguins(枚举+最大流)
- POJ 3498 March of the Penguins(枚举+最大流)
- POJ - 3498 March of the Penguins 最大流(拆点 + 枚举汇点)
- poj 3498 March of the Penguins 点流量有限制的最大流
- POJ 3498 && HDU 2334 March of the Penguins(最大流-Dinic)
- poj 3498 最大流
- POJ 3498 March of the Penguins(网络最大流)
- poj 3498 (最大流,枚举汇点)