Pahom on Water HDU

来源:互联网 发布:珀莱雅淘宝店真假 编辑:程序博客网 时间:2024/06/03 15:47

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
这题主要是题目太难读了,读懂了会很简单,就是抽象出来以后图建边,边权为1,判断源点到汇点的流量是否大于2即可

#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>#define INF  0x3f3f3f3fusing namespace std;const int maxn=305;const int maxx=90005;int edge;int to[maxx],flow[maxx],nex[maxx];int head[maxn];void addEdge(int v,int u,int cap){    to[edge]=u,flow[edge]=cap,nex[edge]=head[v],head[v]=edge++;    to[edge]=v,flow[edge]=0,nex[edge]=head[u],head[u]=edge++;}int vis[maxn];int pre[maxn];bool bfs(int s,int e){    queue<int> que;    pre[s]=-1;    memset(vis,-1,sizeof(vis));    que.push(s);    vis[s]=0;    while(!que.empty())    {        int u=que.front();        que.pop();        for(int i=head[u];~i;i=nex[i])        {            int v=to[i];            if(vis[v]==-1&&flow[i])            {                vis[v]=vis[u]+1;                if(v==e)                    return true;                que.push(v);            }        }    }    return false;}int dfs(int s,int t,int f){    if(s==t||!f)        return f;    int r=0;    for(int i=head[s];~i;i=nex[i])    {        int v=to[i];        if(vis[v]==vis[s]+1&&flow[i])        {            int d=dfs(v,t,min(f,flow[i]));            if(d>0)            {                flow[i]-=d;                flow[i^1]+=d;                r+=d;                f-=d;                if(!f)                    break;            }        }    }    if(!r)        vis[s]=INF;    return r;}int maxFlow(int s ,int e)//然后直接调用这个即可{    int ans=0;    while(bfs(s,e))        ans+=dfs(s,e,INF);    return ans;}void init()//记得每次使用前初始化{    memset(head,-1,sizeof(head));    edge=0;}struct node{    double f;    int x,y;    int r;}p[305];bool judge(int i,int j){    int x1=p[i].x-p[j].x;    int y1=p[i].y-p[j].y;    int R=p[i].r+p[j].r;    if(x1*x1+y1*y1<=R*R)        return true;    else        return false;}int main(){    int t;    int n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        int s,e;        init();        for(int i=1;i<=n;i++)        {            scanf("%lf%d%d%d",&p[i].f,&p[i].x,&p[i].y,&p[i].r);            if(p[i].f==400.0)                s=i;            if(p[i].f==789.0)                e=i;        }        for(int i=1;i<=n;i++)        {            for(int j=i+1;j<=n;j++)            {                if(judge(i,j))                {                    if(p[i].f<p[j].f)                        addEdge(i,j,1);                    if(p[i].f>p[j].f)                        addEdge(j,i,1);                }            }        }        if(maxFlow(s,e)>=2)            printf("Game is VALID\n");        else            printf("Game is NOT VALID\n");    }    return 0;}
原创粉丝点击