您的位置:首页 > 其它

poj 3498(枚举汇点的最大流)

2011-09-02 17:57 411 查看
March of the Penguins

Time Limit: 8000MSMemory Limit: 65536K
Total Submissions: 3061Accepted: 1411
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
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


Source

Northwestern Europe 2007

题目:http://poj.org/problem?id=3498

分析:这题没什么好讲了,直接枚举加最大流,记得把点二分就行。。。

代码:

#include<cstdio>
using namespace std;
const int mm=44444;
const int mn=222;
const int oo=1000000000;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn],in[mn],x[mn],y[mn];
bool get[mn];
double D;
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int main()
{
    int i,j,c,m,n,t,sum;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lf",&n,&D);
        prepare(n+n+1,0,0);
        for(i=1,sum=0;i<=n;++i)
        {
            scanf("%d%d%d%d",&x[i],&y[i],&c,&m);
            if(c)addedge(src,i,c);
            if(m)addedge(i,i+n,m);
            sum+=c;
        }
        for(i=1;i<=n;++i)
            for(j=i+1;j<=n;++j)
            if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])-D*D<1e-8)
                addedge(i+n,j,oo),addedge(j+n,i,oo);
        for(i=1,m=0;i<=n;++i)
        {
            for(j=0;j<edge;j+=2)flow[j]+=flow[j^1],flow[j^1]=0;
            dest=i,get[i]=(Dinic_flow()==sum),m+=get[i];
        }
        if(m)
        {
            for(i=1,j=0;i<=n;++i)
            if(get[i])printf("%d%c",i-1,(++j<m)?' ':'\n');
        }
        else printf("-1\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: