hdu4606 计算几何+二分+二分图匹配+最短路

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include<queue>
using namespace std;
const int N=1e2+10;
const int M=1<<21;
const int INF=1e9+10;
const double esp=1e-8;
int n,m;
int p;
int x[550],y[550];
struct Point{
	double x,y;
	void print(){
		printf("(%.2f,%.2f)\n",x,y);
	}
	Point(double x,double y):x(x),y(y){}
};
struct Seg{
	Point s,e;
	Seg(Point s,Point e):s(s),e(e){}
};
double cr(Point a,Point b,Point c){
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
int sgn(double x){return x<-esp?-1:x<esp?0:1;}
bool jiao(Point a,Point b,Point c,Point d){
	return 
	min(a.x,b.x)<=max(c.x,d.x) &&
    min(c.x,d.x)<=max(a.x,b.x) &&
	min(a.y,b.y)<=max(c.y,d.y) &&	
	min(c.y,d.y)<=max(a.y,b.y) &&
	sgn(cr(a,b,c))*sgn(cr(a,b,d))<0 &&
	sgn(cr(c,d,a))*sgn(cr(c,d,b))<0;
}
double dis(Point a,Point b){
	return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}


double G[550][550];
int match[N];
vector<int> vec[N];
bool vis[N];
bool find(int u){
	for(int k=0;k<vec[u].size();k++){
		int v=vec[u][k];
		if(!vis[v]){
			vis[v]=1;
		if(match[v]==-1 || find(match[v])){
			match[v]=u;
			return true;
		}
		}
	}
	return false;
}
int solve(){
	int num=0;
	memset(match,-1,sizeof match);
	for(int i=1;i<=n;i++){
		memset(vis,0,sizeof vis);
		if(find(i)) num++;
	}
	return num;
}
int order[N];
bool ok(double vo){
	for(int i=1;i<=n;i++) vec[i].clear();
	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++){
			int u=order[i],v=order[j];
			if(G[u][v]<=vo) vec[u].push_back(v);
		}
	int ans=n-solve();
	return ans<=p;
}
int main()
{
   //freopen("in","r",stdin);
    //freopen("out.txt","w",stdout);
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&n,&m,&p);
		for(int i=1;i<=n;i++) {
			scanf("%d%d",&x[i],&y[i]);
		}
		int tot=n;
		for(int i=1;i<=m;i++){
			tot++;
			scanf("%d%d",&x[tot],&y[tot]);
			tot++;
			scanf("%d%d",&x[tot],&y[tot]);
		}
		for(int i=1;i<=tot;i++) for(int j=1;j<=tot;j++){
			if(i==j) G[i][j]=0;
			else G[i][j]=INF;
		}
		for(int i=1;i<=tot;i++) for(int j=i+1;j<=tot;j++){
		   bool ok=true;
		   for(int k=0;k<m && ok;k++){
		   	 int x1=x[n+k*2+1],y1=y[n+k*2+1];
		   	 int x2=x[n+k*2+2],y2=y[n+k*2+2];
			 if(jiao(Point(x1,y1),Point(x2,y2),Point(x[i],y[i]),Point(x[j],y[j]))) ok=false;
		   }
		   if(ok) G[i][j]=G[j][i]=dis(Point(x[i],y[i]),Point(x[j],y[j]));
		}
		for(int k=1;k<=tot;k++)
			for(int i=1;i<=tot;i++)
				for(int j=1;j<=tot;j++)
					G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
		for(int i=1;i<=n;i++) scanf("%d",&order[i]);
		double l=0,r=40000;
		while(r-l>esp){
			double m=(l+r)/2.0;
			if(ok(m)) r=m;
			else l=m;
		}
		printf("%.2f\n",(l+r)/2.0);
	}
    return 0;
}

你可能感兴趣的:(hdu4606 计算几何+二分+二分图匹配+最短路)