Pahom on Water

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



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


//大神的题意和思路,写的很好。。


图上有N个圆形的光谱,一个人想从红色圆谱到紫罗兰圆谱,再由紫罗兰光谱返回红色光谱。图中可能需要经过其它的圆形光谱,现在给出如下限制:



1,红色光谱和紫罗兰光谱允许走两次,其它的光谱若走过一次不能再走。



2,对于任意的光谱A和光谱B,只要两个光谱相交,就可以从光谱频率大的圆经过光谱频率小的圆。



问你这个人能不能实现这个过程。




思路:建立超级源点0,超级汇点N + 1.



一:源点连接紫罗兰光谱,边权为2。


二:红色光谱连接汇点,边权为2。


三:对于任意相交的两个光谱,从频率大的引一条到频率小的边,边权为1。



最后从超级源 到 超级汇 跑一下最大流,看Maxflow 是否为2。若为2就说明可以实现,否则不能实现。


 

#include<stdio.h>
#include<string.h>
#include<cmath>//这块很坑,用math.h就一直不行,换成cmath,才行。。(坑了我30多分钟) 
#include<algorithm>
#include<queue>
#define N 1010
#define ee 1e-5
#define INF 0x3f3f3f3f
using namespace std;
int head[N],edgenum;
int vis[N];
int dis[N];
int c[N];
int n,s,e;
struct zz
{
	int from;
	int to;
	int cap;
	int flow;
	int next;
}edge[N<<8];
struct z
{
	double hz,x,y,r;
}a[N];
void add(int u,int v,int w)
{
	zz E={u,v,w,0,head[u]};
	edge[edgenum]=E;
	head[u]=edgenum++;
	zz EE={v,u,0,0,head[v]};
	edge[edgenum]=EE;
	head[v]=edgenum++;
}
bool judge(double hz1,double x1,double y1,double r1,double hz2,double x2,double y2,double r2)
{
	if(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))<(r1+r2)*(r1+r2)&&hz1>hz2)
		return true;
	return false;
}
bool bfs(int s,int e)
{
	queue<int>q;
	memset(dis,-1,sizeof(dis));
	memset(vis,0,sizeof(vis));
	dis[s]=0;
	vis[s]=1;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			zz v=edge[i];
			if(!vis[v.to]&&v.cap>v.flow)
			{
				vis[v.to]=1;
				dis[v.to]=dis[u]+1;
				if(v.to==e)
					return true;
				q.push(v.to);
			}
		}
	}
	return false;
}
int dfs(int x,int a,int e)
{
	if(x==e||a==0)
		return a;
	int flow=0,f;
	for(int i=head[x];i!=-1;i=edge[i].next)
	{
		zz &v=edge[i];
		if(dis[v.to]==dis[x]+1&&(f=dfs(v.to,min(a,v.cap-v.flow),e))>0)
		{
			v.flow+=f;
			edge[i^1].flow-=f;
			flow+=f;
			a-=f;
			if(a==0)
				break;
		}
	}
	return flow;
}
int maxflow(int s,int e)
{
	int flow=0;
	while(bfs(s,e))
	{
		flow+=dfs(s,INF,e);
	}
	return flow;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		edgenum=0;
		memset(head,-1,sizeof(head));
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%lf%lf%lf%lf",&a[i].hz,&a[i].x,&a[i].y,&a[i].r);
		s=0;e=n+1;
		for(int i=1;i<=n;i++)
		{
			if(abs(a[i].hz-789.0)<=ee)
				add(0,i,2);
			if(abs(a[i].hz-400.0)<=ee)
				add(i,n+1,2);
			for(int j=1;j<=n;j++)
			{
				if(i==j)
					continue;
				if(judge(a[i].hz,a[i].x,a[i].y,a[i].r,a[j].hz,a[j].x,a[j].y,a[j].r))
					add(i,j,1);
			}
		}
		if(maxflow(0,n+1)==2)
			printf("Game is VALID\n");
		else
			printf("Game is NOT VALID\n");
	}
	return 0;
}