网络流(好)hdu4183

本文介绍了一个基于计算机游戏PahomonWater的路径验证问题。游戏目标是引导角色从红色区域走向紫色区域再返回,路径需遵循颜色频率变化规则。文章通过构建图论模型并运用最大流算法来判断是否存在合法路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

Pahom on Water

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 484    Accepted Submission(s): 219


Problem Description
Pahom on Water is an interactive computer game inspired by a short story of Leo Tolstoy about a poor man who, in his lust for land, forfeits everything. The game's starting screen displays a number of circular pads painted with colours from the visible light spectrum. More than one pad may be painted with the same colour (defined by a certain frequency) except for the two colours red and violet. The display contains only one red pad (the lowest frequency of 400 THz) and one violet pad (the highest frequency of 789 THz). A pad may intersect, or even contain another pad with a different colour but never merely touch its boundary. The display also shows a figure representing Pahom standing on the red pad.
The game's objective is to walk the figure of Pahom from the red pad to the violet pad and return back to the red pad. The walk must observe the following rules:
1.If pad α and pad β have a common intersection and the frequency of the colour of pad α is strictly smaller than the frequency of the colour of pad β, then Pahom figure can walk from α to β during the walk from the red pad to the violet pad
2. If pad α and pad β have a common intersection and the frequency of the colour of pad α is strictly greater than the frequency of the colour of pad β, then Pahom figure can walk from α to β during the walk from the violet pad to the red pad
3. A coloured pad, with the exception of the red pad, disappears from display when the Pahom figure walks away from it.
The developer of the game has programmed all the whizzbang features of the game. All that is left is to ensure that Pahom has a chance to succeed in each instance of the game (that is, there is at least one valid walk from the red pad to the violet pad and then back again to the red pad.) Your task is to write a program to check whether at least one valid path exists in each instance of the game.
 

Input
The input starts with an integer K (1 <= K <= 50) indicating the number of scenarios on a line by itself. The description for each scenario starts with an integer N (2 <= N <= 300) indicating the number of pads, on a line by itself, followed by N lines that describe the colors, locations and sizes of the N pads. Each line contains the frequency, followed by the x- and y-coordinates of the pad's center and then the radius. The frequency is given as a real value with no more than three decimal places. The coordinates and radius are given, in meters, as integers. All values are separated by a single space. All integer values are in the range of -10,000 to 10,000 inclusive. In each scenario, all frequencies are in the range of 400.0 to 789.0 inclusive. Exactly one pad will have a frequency of “400.0” and exactly one pad will have a frequency of “789.0”.
 

Output
The output for each scenario consists of a single line that contains: Game is VALID, or Game is NOT VALID
 

Sample Input
      
2 2 400.0 0 0 4 789.0 7 0 2 4 400.0 0 0 4 789.0 7 0 2 500.35 5 0 2 500.32 5 0 3
 

Sample Output
      
Game is NOT VALID Game is VALID
题意:从400.0的走到789.0的,只能沿着相交的并且频率升高的路走,然偶从789.0的再走回到400.0只能要这频率低的走,并且每个点只能走一次

思路:拆点,以789.0的为原点,400.0为汇点,原点起始流量为2,然后如果i比j频率达,则建一条(i+n,j,1)的边,求一次最大流,如果流量为2则存在这样的路,因为如果流量为2,我们可以把其中一条反向,就可以得到题目中要求的路径

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=310*2;
const int INF=1000000000;
const double eps=1e-9;
int n,m,k,nn,st,en;
int vis[maxn],pre[maxn],cnt;
int dis[maxn],gap[maxn],cur[maxn],head[maxn];
struct C
{
    double p;
    int x,y,r;
}cir[maxn];
struct node
{
    int v,f,next;
}edge[maxn*maxn*3];
void add_edge(int x,int y,int f)
{
    edge[cnt].v=y;
    edge[cnt].f=f;
    edge[cnt].next=head[x];
    head[x]=cnt++;
    edge[cnt].v=x;
    edge[cnt].f=0;
    edge[cnt].next=head[y];
    head[y]=cnt++;
}
int SAP(int st,int en)
{
    for(int i=0;i<=nn;i++)
    {
        cur[i]=head[i];
        dis[i]=gap[i]=0;
    }
    int u=0;
    int flow=0,aug=INF;
    gap[st]=nn;
    u=pre[st]=st;
    bool flag;
    while(dis[st]<nn)
    {
        flag=0;
        for(int &j=cur[u];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].f>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].f<aug)aug=edge[j].f;
                pre[v]=u;
                u=v;
                if(u==en)
                {
                    flow+=aug;
                    while(u!=st)
                    {
                        u=pre[u];
                        edge[cur[u]].f-=aug;
                        edge[cur[u]^1].f+=aug;
                    }
                    aug=INF;
                }
                break;
            }
        }

        if(flag)continue;
        int mindis=nn;
        for(int j=head[u];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(dis[v]<mindis&&edge[j].f>0)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];

    }
    return flow;
}
bool can(int x,int y)
{
    int l=(cir[x].x-cir[y].x)*(cir[x].x-cir[y].x)+(cir[x].y-cir[y].y)*(cir[x].y-cir[y].y);
    int r=(cir[x].r+cir[y].r)*(cir[x].r+cir[y].r);
    if(l<=r&&cir[x].p>cir[y].p)
    {return true;}
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
            scanf("%lf%d%d%d",&cir[i].p,&cir[i].x,&cir[i].y,&cir[i].r);
        for(int i=1;i<=n;i++)
        {
            if(fabs(cir[i].p-789)<eps)
            {
                st=i;
                add_edge(st,st+n,2);
            }
            else if(fabs(cir[i].p-400)<eps)
            {
                en=i+n;
                add_edge(i,i+n,2);
            }
            else add_edge(i,i+n,1);
            for(int j=1;j<=n;j++)
            {
                if(i==j)continue;
                if(can(i,j))add_edge(i+n,j,1);
            }
        }
        nn=2*n+1;
        int flow=SAP(st,en);
        if(flow>=2)printf("Game is VALID\n");
        else printf("Game is NOT VALID\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值