• [POJ 3498] March of the Penguins


    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.

    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 xiyini 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 ≤ xiyi ≤ 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
    ",ans[i]-1);
                else printf("%d ",ans[i]-1);
            }
            if(!ansd) printf("-1
    ");
        }
        return 0;
    }
  • 相关阅读:
    数据库水平切分(拆库拆表)的实现原理解析(转)
    json序列化 & 反序列化
    数据库工作原理
    【原创】python多线程测试接口性能
    XML解析(DOM、ElementTree)及转换为JSON
    nginx+supervisor+gunicorn+flask
    3、爬取干货集中营的福利图片
    Python多环境扩展管理
    九、frp对外提供简单的文件访问服务
    八、frps服务端与nginx可共用80端口
  • 原文地址:https://www.cnblogs.com/hate13/p/4598317.html
Copyright © 2020-2023  润新知