https://vjudge.net/problem/HDU-4183
题意:
这道题目的英文实在是很难理解啊。
给出n个圆,每个圆有频率,x、y轴和半径r4个属性,每次将频率为400的圆作为起点,频率为789点作为终点。从源点到汇点时必须从频率小的到频率大的,而从汇点到源点时必须从频率大的到频率小的。前提时这两个圆必须严格相交。每个点只能走一次。判断是否能从起点出发到达终点,并再次返回起点。
思路:
其实就是判断最大流是否大于等于2。因为每个点只能走一次,用拆点法。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std; const int maxn=+;
const int INF=0x3f3f3f3f; struct Point
{
double p;
int x,y,r;
}point[maxn],s,t; bool cacl(Point a,Point b)
{
double l1=(b.y-a.y)*(b.y-a.y)+(b.x-a.x)*(b.x-a.x);
double l2=(double)(a.r+b.r)*(a.r+b.r);
if(l1<l2) return true;
else return false;
} struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int w,int f):from(u),to(v),cap(w),flow(f){}
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
for(int i=;i<n;++i) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,) );
edges.push_back( Edge(to,from,,) );
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
queue<int> Q;
memset(vis,,sizeof(vis));
vis[s]=true;
d[s]=;
Q.push(s);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=;i<G[x].size();++i)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t || a==) return a;
int flow=, f;
for(int &i=cur[x];i<G[x].size();++i)
{
Edge &e=edges[G[x][i]];
if(d[e.to]==d[x]+ && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>)
{
e.flow +=f;
edges[G[x][i]^].flow -=f;
flow +=f;
a -=f;
if(a==) break;
}
}
return flow;
} int Maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow +=DFS(s,INF);
}
return flow;
}
}DC; int n; int main()
{
//freopen("D:\\input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
DC.init(*n+);
for(int i=;i<=n;i++)
{
scanf("%lf%d%d%d",&point[i].p,&point[i].x,&point[i].y,&point[i].r);
if(point[i].p==)
{
s=point[i];
i--;
n--;
}
else if(point[i].p==)
{
t=point[i];
i--;
n--;
}
} if(cacl(s,t))
{
printf("Game is VALID\n");
continue;
} for(int i=;i<=n;i++)
{
DC.AddEdge(i,n+i,); //拆点
if(cacl(s,point[i]) && s.p<point[i].p) DC.AddEdge(,i,);
if(cacl(point[i],t) && point[i].p<t.p) DC.AddEdge(n+i,*n+,);
for(int j=;j<=n;j++)
{
if(i==j) continue;
if(cacl(point[i],point[j]) && point[i].p<point[j].p)
DC.AddEdge(n+i,j,);
}
}
int ans=DC.Maxflow(,*n+);
if(ans>=) printf("Game is VALID\n");
else printf("Game is NOT VALID\n");
}
return ;
}