[POJ 3498] March of the Penguins
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 4378 | Accepted: 1988 |
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.
![](http://poj.org/images/3498_1.png)
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
经典最大流、- -
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> using namespace std; #define INF 0x3f3f3f3f #define N 1010 #define M 100010 struct Edge { int to,next,val; }edge[M]; int tot; int head[N]; int pre[N]; int cur[N]; int gap[N]; int dep[N]; void init() { tot=0; memset(head,-1,sizeof(head)); } void add(int u,int v,int w,int rw=0) { edge[tot].to=v; edge[tot].val=w; edge[tot].next=head[u]; head[u]=tot++; edge[tot].to=u; edge[tot].val=rw; edge[tot].next=head[v]; head[v]=tot++; } int SAP(int src,int des,int n) { memset(dep,0,sizeof(dep)); memset(gap,0,sizeof(gap)); memcpy(cur,head,sizeof(head)); int u=pre[src]=src; int maxflow=0,aug=-1; gap[0]=n; while(dep[src]<n){ loop: for(int &i=cur[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(edge[i].val && dep[u]==dep[v]+1){ aug==-1?(aug=edge[i].val):(aug=min(aug,edge[i].val)); pre[v]=u; u=v; if(v==des){ maxflow+=aug; for(u=pre[v];v!=src;v=u,u=pre[u]){ edge[cur[u]].val-=aug; edge[cur[u]^1].val+=aug; } aug=-1; } goto loop; } } int Min=n; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(edge[i].val && Min>dep[v]){ cur[u]=i; Min=dep[v]; } } if(--gap[dep[u]]==0) break; dep[u]=Min+1; gap[dep[u]]++; u=pre[u]; } return maxflow; } int n,sum,k1[N],k2[N]; double d,x[N],y[N]; double dis(int i,int j) { return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } int main() { int T; scanf("%d",&T); while(T--) { init(); sum=0; scanf("%d%lf",&n,&d); for(int i=1;i<=n;i++){ scanf("%lf%lf%d%d",&x[i],&y[i],&k1[i],&k2[i]); sum+=k1[i]; } for(int i=1;i<=n;i++) add(0,i,k1[i]); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i!=j && dis(i,j)<=d){ add(i+n,j,INF); } } } for(int i=1;i<=n;i++) add(i,i+n,k2[i]); int ans[N],ansd=0; for(int i=1;i<=n;i++){ int t=SAP(0,i,2*n+1); if(t==sum) ans[++ansd]=i; for(int j=0;j<tot;j+=2){ edge[j].val+=edge[j^1].val,edge[j^1].val=0; } } for(int i=1;i<=ansd;i++){ if(i==ansd) printf("%d\n",ans[i]-1); else printf("%d ",ans[i]-1); } if(!ansd) printf("-1\n"); } return 0; }