HDU4183 Pahom on Water (maximum flow of network flow)

Title address: http://acm.hdu.edu.cn/showproblem.php?pid=4183

Question meaning: Given n points, each point has a frequency, origin coordinate and radius, i to j is reachable if and only if the frequency of i is less than the frequency of j and the two circles intersect, ask you from the smallest frequency to Whether the scheme from the largest frequency to the smallest frequency is valid (each edge can only be used once).

Ideas: starting point-end point, end point-start point is actually the start point-end point whether there are more than two different paths, because each edge can only be used once, so the weight is 1, and then violently build the map and run down the maximum flow template. .

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define N 310
#define M 90010
#define inf 1000000007
typedef struct
{
    int v,w,next;
}Node;
Node e[M];
typedef struct
{
    int x,y,r;
    double f;
}node;
node s[N];
int head[N],vis[N],d[N];
int n,tot;
int cmp(node u,node v)
{
    return u.f<v.f;
}
void add(int x,int y,int z)
{
    e[tot].v=y;
    e[tot].w=z;
    e[tot].next=head[x];
    head[x]=tot++;
}
int check(int a,int b)
{
    double t,ans;
    t=(s[a].x-s[b].x)*(s[a].x-s[b].x)+(s[a].y-s[b].y)*(s[a].y-s[b].y);
    ans=sqrt(t)-s[a].r-s[b].r;
    if(ans>0)
    return 0;
    return 1;
}
int bfs(int x,int y){
    int i,j,k,t;
    memset(d,-1,sizeof(d));
    queue<int> q;
    q.push(x);
    d[x]=0;
    while(!q.empty()){
        t=q.front();
        q.pop();
        for(i=head[t];i!=-1;i=e[i].next){
            j=e[i].v;
            k=e[i].w;
            if(k&&d[j]==-1){
                d[j]=d[t]+1;
                if(j==y)
                    return 1;
                q.push(j);
            }
        }
    }
    return 0;
}
int dfs(int x,int y,int z){
    if(x==y)
        return z;
    int i,j,k,ans=0;
    for(i=vis[x];i!=-1;i=e[i].next){
        j=e[i].v;
        k=e[i].w;
        if(d[j]==d[x]+1){
            k=dfs(j,y,min(z-ans,k));
            e[i].w-=k;
            e[i^1].w+=k;
            ans+=k;
            if(e[i].w)
                vis[x]=i;
            if(ans==z)
                return ans;
        }
    }
    if(ans==0)
        d[x]=-1;
    return ans;
}
int dinic(int x,int y){
    int ans=0;
    while(bfs(x,y)){
        memcpy(vis,head,sizeof(head));
        ans=ans+dfs(x,y,inf);
    }
    return ans;
}
int main()
{
    int i,j,k,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        tot=0;
        memset(head,-1,sizeof(head));
        for(i=0;i<n;i++)
            scanf("%lf%d%d%d",&s[i].f,&s[i].x,&s[i].y,&s[i].r);
        sort(s,s+n,cmp);
        for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
        if(check(i,j))
        {
            add(i,j,1);
            add(j,i,0);
        }
        if(dinic(0,n-1)>=2)
            printf("Game is VALID\n");
        else
            printf("Game is NOT VALID\n");
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324835860&siteId=291194637