zoukankan      html  css  js  c++  java
  • uva 12125

    Problem B - 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
    
    The 2007 ACM Northwestern European Programming Contest
     
    建模:只要加源点、汇点,每个冰块为一个点,如果两个冰块间可达,则连一条弧,比如u指向v.
    其中冰块间弧的定义:指向表企鹅的跳向;容量表最大跳跃次数;流量表已从u冰块跳出的次数;
    源点到冰块弧:容量表v上企鹅数,其它同上;
    汇点弧:容量表可达u点最多企鹅数。
     
    然后枚举每个冰块,即将其作为唯一连接汇点的‘终点’即可。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    #define INF 10000000
    #define MAXN 111
    #define MAXM 2222222
    
    struct ICE{
        int x, y, pen, hop;
    }ice[MAXN];
    
    struct edge{
        int u, v, cap, flow, nxt;
    }e[MAXM];
    int h[MAXN], cc;
    
    void add(int u, int v, int cap){
        e[cc]=(edge){u, v, cap, 0, h[u]};
        h[u]=cc++;
        e[cc]=(edge){v, u, 0, 0, h[v]};
        h[v]=cc++;
    }
    
    int d[MAXN], vis[MAXN];
    void bfs(const int t){
        queue<int> q;   q.push(t);
        memset(vis, 0, sizeof(vis));
        d[t]=0;     vis[t]=1;
        while(!q.empty()){
            int u=q.front();    q.pop();
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                i^=1;
                int v=e[i].u, cap=e[i].cap, ef=e[i].flow;
                if(!vis[v] && cap>ef){
                    vis[v]=1;
                    d[v]=d[u]+1;
                    q.push(v);
                }
                i^=1;
            }
        }
    }
    
    int num[MAXN], cur[MAXN], p[MAXN];
    
    int Augment(const int s, const int t){
        int a=INF, u;
        for(u=t; u!=s; u=e[p[u]].u)
            a=MIN(a, e[p[u]].cap-e[p[u]].flow);
        for(u=t; u!=s; u=e[p[u]].u)
            e[p[u]].flow+=a,
            e[p[u]^1].flow-=a;
        return a;
    }
    
    int isap(const int s, const int t, int n){
        int i, flow=0;
        bfs(t);
        memset(num, 0, sizeof(num));
        for(i=0; i<n; i++) num[d[i]]++;
        for(i=0; i<n; i++) cur[i]=h[i];
        for(int u=s; d[s]<n; ){
            if(u==t){
                flow+=Augment(s, t);
                u=s;
            }
            bool ok=false;
            for(i=cur[u]; i!=-1; i=e[i].nxt){
                int v=e[i].v, cap=e[i].cap, ef=e[i].flow;
                if(d[v]+1==d[u] && cap>ef){
                    ok=true;
                    cur[u]=i;
                    p[v]=i;
                    u=v;
                    break;
                }
            }
            if(!ok){
                int tmp=n-1;
                for(i=h[u]; i!=-1; i=e[i].nxt){
                    int v=e[i].v, cap=e[i].cap, ef=e[i].flow;
                    if(cap>ef) tmp=MIN(tmp, d[v]);
                }
                if(--num[d[u]]==0) break;
                num[d[u]=tmp+1]++;
                cur[u]=h[u];
                if(u!=s) u=e[p[u]].u;
            }
        }
        return flow;
    }
    
    int n;
    bool test(int x, const int sum, const int tc){
        int i=tc;
        while(e[i].u!=x+1) i+=2;
        e[i].cap=sum;
    
        int flow=isap(0, n+1, n+2);
        e[i].cap=0;
        for(i=0; i<cc; i++) e[i].flow=0;
    //    cout<<x<<":	"<<flow<<' '<<sum<<endl;
        return flow==sum;
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int T;
        scanf(" %d", &T);
        while(T--){
            int i, j, sum=0;
            double td;
            scanf(" %d %lf", &n, &td);
            for(i=0; i<n; i++){
                int x, y, pen, hop;
                scanf(" %d %d %d %d", &x, &y, &pen, &hop);
                ice[i]=(ICE){x, y, pen, hop};
                sum+=pen;
            }
    
            memset(h, -1, sizeof(h));   cc=0;
    
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++){
                    double dis=sqrt((ice[i].x-ice[j].x)*(ice[i].x-ice[j].x)+
                                    (ice[i].y-ice[j].y)*(ice[i].y-ice[j].y));
                    if(dis>td) continue;
                    if(ice[i].hop) add(i+1, j+1, ice[i].hop);
                    if(ice[j].hop) add(j+1, i+1, ice[j].hop);
                }
    
            for(i=0; i<n; i++) if(ice[i].pen)
                add(0, i+1, ice[i].pen);
            const int tc=cc;
            for(i=0; i<n; i++)
                add(i+1, n+1, 0);
            vector<int> ans;
            for(i=0; i<n; i++) if(test(i, sum, tc))
                ans.push_back(i);
            if(!ans.size()){
                printf("-1
    ");     continue;
            }
            printf("%d", ans[0]);
            for(i=1; i<ans.size(); i++)
                printf(" %d", ans[i]);
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    利用树莓派把普通打印机变成网络打印机方法
    Python之datetime库
    CentOS7从默认/home中转移空间到根分区/
    更改Azure虚拟机账号密码
    创建一个托管磁盘的Windows定制镜像
    IO多路复用详解
    玩转redis
    EF Linq to Sql 多表left join查询并对结果group by分组之后进行count,max等处理
    免费,主流的在线办公/协作,会议,文档,调查,分享工具推荐(持续维护中)
    多sql查询count合并为一行
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3348217.html
Copyright © 2011-2022 走看看