hdu 4183 Pahom on Water(最大流)

来源:互联网 发布:数据库的物理结构 编辑:程序博客网 时间:2024/06/06 01:01

Pahom on Water

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


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
22 400.0 0 0 4789.0 7 0 24400.0 0 0 4789.0 7 0 2500.35 5 0 2500.32 5 0 3
 

Sample Output
Game is NOT VALIDGame is VALID
 

Source
The 2011 South Pacific Programming Contest
题意:坐标轴上有很多个圈,现在问能不能从最小的圈走到最大的圈再从最大的圈走回来,不能经过走过的圈

圈给出坐标和半径,相交的圈才能走过去,不会有圈相切的情况

思路 :把所有从小到大能走到的边连接,判断从最小的走到最大的能不能至少有两种方式(也就是最大流>=2)

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define N 1000#define INF 99999999struct Node{    double v;    int x,y,r;} a[N];struct Edge{    int v,next,cap;} edge[N*N];int cnt,head[N],d[N];void init(){    memset(head,-1,sizeof(head));    cnt=0;}void addedge(int u,int v,int cap){    edge[cnt].v=v,edge[cnt].cap=cap;    edge[cnt].next=head[u],head[u]=cnt++;    edge[cnt].v=u,edge[cnt].cap=0;    edge[cnt].next=head[v],head[v]=cnt++;}int bfs(int s,int t){    memset(d,-1,sizeof(d));    d[s]=0;    queue<int>q;    q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v,cap=edge[i].cap;            if(d[v]==-1&&cap>0)            {                d[v]=d[u]+1;                q.push(v);            }        }    }    return d[t]!=-1;}int dfs(int s,int t,int f){    if(s==t||f==0) return f;    int flow=0;    for(int i=head[s]; i!=-1; i=edge[i].next)    {        int v=edge[i].v,cap=edge[i].cap;        if(d[v]==d[s]+1&&cap>0)        {            int x=min(f-flow,cap);            x=dfs(v,t,x);            flow+=x;            edge[i].cap-=x;            edge[i^1].cap+=x;        }    }    if(!flow) d[s]=-2;    return flow;}int Dinic(int s,int t){    int flow=0,f;    while(bfs(s,t))    {        while(f=dfs(s,t,INF))            flow+=f;    }    return flow;}int judge(Node a,Node b){    long long dist=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);    long long r=(a.r+b.r)*(a.r+b.r);    return dist<r;}bool cmp(Node a,Node b){    return a.v<b.v;}int main(){    int T,n;    scanf("%d",&T);    while(T--)    {        init();        scanf("%d",&n);        for(int i=1; i<=n; i++)            scanf("%lf %d %d %d",&a[i].v,&a[i].x,&a[i].y,&a[i].r);        sort(a+1,a+1+n,cmp);        for(int i=1; i<=n; i++)            for(int j=i+1; j<=n; j++)                if(judge(a[i],a[j]))                        addedge(i,j,1);        if(Dinic(1,n)>=2) printf("Game is VALID\n");        else printf("Game is NOT VALID\n");    }    return 0;}





0 0
原创粉丝点击