您的位置:首页 > 理论基础 > 计算机网络

March of the Penguins - UVa 12125 网络流

2015-06-02 14:52 756 查看

March of the Penguins

Time limit: 4 seconds

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 ≤ 100000), 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 (-10000 ≤ xi, yi ≤ 10000, 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


题意:一些企鹅在一些冰上面,这些冰都有坐标和最大跳走次数,企鹅有最大跳跃距离。问这些企鹅可以在哪些点冰上汇合。

思路:拆点,将点拆成i*2和i*2+1,从第一个点到第二个点的流量最多为该冰的最大调走次数。然后枚举汇点,求出答案。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
struct node1
{
    double x,y;
    int num,f;
}ice[110];
struct node2
{
    int u,v,f,next;
}edge[100010];
int T,n,m,Head[210],d[210],s,t,tot,INF=1e9,point[110],link[110][110],sum;
double eps=1e-9,D;
queue<int> qu;
int dcmp(double x){return (x>eps)-(x<-eps);}
void add(int u,int v,int f)
{
    edge[tot].v=v;
    edge[tot].f=f;
    edge[tot].next=Head[u];
    Head[u]=tot++;
}
void init(int p)
{
    int i,j,k;
    memset(Head,-1,sizeof(Head));
    tot=0;
    for(i=1;i<=n;i++)
    {
        add(i*2,i*2^1,ice[i].f);
        add(i*2^1,i*2,0);
        if(ice[i].num>0)
        {
            add(0,i*2,ice[i].num);
            add(i*2,0,0);
        }
    }
    for(i=1;i<=n;i++)
       for(j=i+1;j<=n;j++)
          if(link[i][j])
          {
              add(i*2^1,j*2,INF);
              add(j*2,i*2^1,0);
              add(j*2^1,i*2,INF);
              add(i*2,j*2^1,0);
          }
    add(p*2,1,INF);
    add(1,p*2,0);
    s=0;t=1;
}
int bfs()
{
    int i,j,k,u,v;
    while(!qu.empty())
      qu.pop();
    memset(d,-1,sizeof(d));
    d[s]=0;
    qu.push(s);
    while(!qu.empty())
    {
        u=qu.front();
        qu.pop();
        for(i=Head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].v;
            if(edge[i].f>0 && d[v]==-1)
            {
                d[v]=d[u]+1;
                qu.push(v);
                if(v==t)
                  return 1;
            }
        }
    }
    return 0;
}
int dfs(int u,int f)
{
    if(u==t || f==0)
      return f;
    int i,j,k,v,ans=0;
    for(i=Head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].v;
        if(edge[i].f>0 && d[v]==d[u]+1)
        {
            k=dfs(v,min(f,edge[i].f));
            edge[i].f-=k;
            edge[i^1].f+=k;

            ans+=k;
            f-=k;
            if(f==0)
              break;
        }
    }
    d[u]=-1;
    return ans;
}
int dinic()
{
    int i,j,k,ans=0;
    while(bfs())
      ans+=dfs(0,INF);
    return ans;
}
int main()
{
    int i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%lf",&n,&D);
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%lf%lf%d%d",&ice[i].x,&ice[i].y,&ice[i].num,&ice[i].f);
            sum+=ice[i].num;
        }
        memset(link,0,sizeof(link));
        for(i=1;i<=n;i++)
           for(j=i+1;j<=n;j++)
              if(i!=j)
                if(dcmp(  (ice[i].x-ice[j].x)*(ice[i].x-ice[j].x)+   (ice[i].y-ice[j].y)*(ice[i].y-ice[j].y)-D*D  )<=0)
                  link[i][j]=link[j][i]=1;
        point[0]=0;
        for(i=1;i<=n;i++)
        {
            init(i);
            k=dinic();
            if(sum==k)
              point[++point[0]]=i;
        }
        if(point[0]==0)
          printf("-1\n");
        else
        {
            printf("%d",point[1]-1);
            for(i=2;i<=point[0];i++)
               printf(" %d",point[i]-1);
            printf("\n");
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: