题目好长好难懂0 0,直接翻了翻网上的题解。
大意是这样的:有
n(2≤n≤300)
个点,每个点有个频率
f(400.0≤f≤789.0)
和坐标还有半径。如果点i能走到点j,那么以两个点为圆心半径分别为
Ri,Rj
的圆相交,即
(Xi−Xj)2+(Yi−Yj)2−−−−−−−−−−−−−−−−−−−√<Ri+Rj
,且需要
fi<fj
。问是否存在两条以上从起点走到终点的路径(起点的f值为400.0,终点为789.0),除起点终点以外每个点只能经过一次。
别看题意那么复杂,题却不难。因为每个点只能走一遍,因此要拆点,容量为1。然后,直接
O(N2)
连边吧,之后跑网络流。
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define MAXN 1010
using namespace std;
inline int Min(int a,int b)
{return a<b?a:b;}
struct E
{
int v,w,op;
E(){}
E(int a,int b,int c)
{v = a; w = b; op = c;}
};
vector<E> g[MAXN];
int d[MAXN],vd[MAXN],n,s,t,flow;
struct Node
{
double f;
int x,y,r;
bool operator <(const Node &b)const
{return f < b.f;}
}a[MAXN];
bool inarea(int i,int j)
{
return (a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y) < (a[i].r+a[j].r)*(a[i].r+a[j].r);
}
int aug(int i,int augco)
{
int j,augc = augco,mind = t-1,delta,sz = g[i].size();
if(i == t) return augco;
for(j = 0; j < sz; j++)
{
int v = g[i][j].v;
if(g[i][j].w)
{
if(d[i] == d[v]+1)
{
delta = Min(augc,g[i][j].w);
delta = aug(v,delta);
g[i][j].w -= delta;
g[v][g[i][j].op].w += delta;
augc -= delta;
if(d[s] >= t) return augco - augc;
if(augc == 0) break;
}
if(d[v] < mind) mind = d[v];
}
}
if(augc == augco)
{
vd[d[i]]--;
if(vd[d[i]] == 0) d[s] = t;
d[i] = mind+1;
vd[d[i]]++;
}
return augco - augc;
}
void sap()
{
flow = 0;
memset(d,0,sizeof d);
memset(vd,0,sizeof vd);
vd[0] = t;
while(d[s] < t)
flow += aug(s,0x3f3f3f3f);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(a,0,sizeof a);
scanf("%d",&n);
for(int i = 1; i <= n; i++)
scanf("%lf%d%d%d",&a[i].f,&a[i].x,&a[i].y,&a[i].r);
sort(a+1,a+n+1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j < i; j++)
if(inarea(i,j))
{
g[j+n].push_back(E(i,1,g[i].size()));
g[i].push_back(E(j+n,0,g[j+n].size()-1));
}
}
g[1].push_back(E(1+n,0x3f3f3f3f,g[1+n].size()));
g[1+n].push_back(E(1,0,g[1].size()-1));
g[n].push_back(E(2*n,0x3f3f3f3f,g[2*n].size()));
g[2*n].push_back(E(n,0,g[n].size()-1));
for(int i = 2; i < n; i++)
{
g[i].push_back(E(i+n,1,g[i+n].size()));
g[i+n].push_back(E(i,0,g[i].size()-1));
}
s = 1,t = 2*n;
sap();
if(flow >= 2)
printf("Game is VALID\n");
else printf("Game is NOT VALID\n");
for(int i = 1; i <= t; i++) g[i].clear();
}
}