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.
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
Source
考虑紫色区域为源点S,红色区域为汇点T S->S' 容量为2 ,若max_flow=2 则将S->T其中一条流反向,可得T->S->T
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
typedef unsigned long long ull;
const int mm=1111111;
const int mn=888;
int node,s,t,edge,max_flow;
int ver[mm],cap[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline void init(int _node,int _s,int _t)
{
node=_node, s=_s, t=_t;
for(int i=0;i<node;++i)
head[i]=-1;
edge=max_flow=0;
}
inline void addedge(int u,int v,int c)
{
ver[edge]=v,cap[edge]=c,flow[edge]=0,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,cap[edge]=0,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
int i,u,v,l,r=0;
for(i=0;i<node;++i) dis[i]=-1;
dis[ q[r++]=s ] = 0;
for(l=0;l<r;l++)
{
for(i=head[ u=q[l] ]; ~i ;i=next[i])
if(flow[i]<cap[i] && dis[ v=ver[i] ]<0)
{
dis[ q[r++]=v ]=dis[u]+1;
if(v==t) return 1;
}
}
return 0;
}
int Dinic_dfs(int u,int exp)
{
if(u==t) return exp;
for(int &i=work[u],v,temp; ~i ;i=next[i])
{
if(flow[i]<cap[i] && dis[ v=ver[i] ]==dis[u]+1 && ( temp=Dinic_dfs(v,min(exp,cap[i]-flow[i])) )>0)
{
flow[i]+=temp;
flow[i^1]-=temp;
return temp;
}
}
return 0;
}
int Dinic_flow()
{
int res,i;
while(Dinic_bfs())
{
for(i=0;i<node;++i) work[i]=head[i];
while( ( res=Dinic_dfs(s,INF) ) ) max_flow+=res;
}
return max_flow;
}
struct node{
double f;
int x,y,r;
}e[333];
bool check(int i,int j)
{
if(( (e[i].x-e[j].x)*(e[i].x-e[j].x)+(e[i].y-e[j].y)*(e[i].y-e[j].y) ) <(e[i].r+e[j].r)*(e[i].r+e[j].r))
return true;
return false;
}
int main()
{
int t;
cin>>t;
int n;
while(t--)
{
scanf("%d",&n);
int S,T;
for(int i=0;i<n;i++)
{
scanf("%lf%d%d%d",&e[i].f,&e[i].x,&e[i].y,&e[i].r);
if(e[i].f==400.0)
T=i;
if(e[i].f==789.0)
S=i;
}
if(check(T,S))
{
printf("Game is VALID\n");
continue;
}
init(2*n,S*2,T*2);
addedge(S*2,S*2+1,2);
for(int i=0;i<n;i++)
{
if(i!=S && i!=T)
addedge(i*2,i*2+1,1);
for(int j=0;j<i;j++)
{
if(check(i,j))
{
if(e[i].f<e[j].f)
addedge(j*2+1,i*2,1);
else
addedge(i*2+1,j*2,1);
}
}
}
int ans=Dinic_flow();
if(ans>=2)
printf("Game is VALID\n");
else
printf("Game is NOT VALID\n");
}
return 0;
}