luogu2526 [SHOI2001]小狗散步

注意一个景点只能去一次。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
int n, m, xx[205], yy[205], hea[205], ss, tt, cnt, lev[205], maxFlow;
const int oo=0x3f3f3f3f;
queue<int> d;
struct Edge{
	int too, nxt, val;
}edge[45005];
double dis(int i, int j){
	return sqrt((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]));
}
void add_edge(int fro, int too, int val){
	edge[cnt].nxt = hea[fro];
	edge[cnt].too = too;
	edge[cnt].val = val;
	hea[fro] = cnt++;
}
void addEdge(int fro, int too, int val){
	add_edge(fro, too, val);
	add_edge(too, fro, 0);
}
bool bfs(){
	memset(lev, 0, sizeof(lev));
	lev[ss] = 1;
	d.push(ss);
	while(!d.empty()){
		int x=d.front();
		d.pop();
		for(int i=hea[x]; i!=-1; i=edge[i].nxt){
			int t=edge[i].too;
			if(!lev[t] && edge[i].val>0){
				lev[t] = lev[x] + 1;
				d.push(t);
			}
		} 
	}
	return lev[tt]!=0;
}
int dfs(int x, int lim){
	if(x==tt)	return lim;
	int addFlow=0;
	for(int i=hea[x]; i!=-1 && addFlow<lim; i=edge[i].nxt){
		int t=edge[i].too;
		if(lev[t]==lev[x]+1 && edge[i].val>0){
			int tmp=dfs(t, min(lim-addFlow, edge[i].val));
			edge[i].val -= tmp;
			edge[i^1].val += tmp;
			addFlow += tmp;
		}
	}
	return addFlow;
}
void dinic(){
	while(bfs())	maxFlow += dfs(ss, oo);
}
int main(){
	memset(hea, -1, sizeof(hea));
	cin>>n>>m;
	for(int i=1; i<=n+m; i++)	scanf("%d %d", &xx[i], &yy[i]);
	ss = 0; tt = n + m + 1;
	for(int i=1; i<=n; i++){
		addEdge(ss, i, 1);
		if(i==n)	break;
		double disii=dis(i, i+1);
		for(int j=n+1; j<=n+m; j++)
			if(dis(i,j)+dis(j,i+1)<disii*2.0+1e-7)
				addEdge(i, j, 1);
	}
	for(int j=n+1; j<=n+m; j++)
		addEdge(j, tt, 1);
	dinic();
	cout<<n+maxFlow<<endl;
	for(int i=1; i<=n; i++){
		printf("%d %d ", xx[i], yy[i]);
		for(int j=hea[i]; j!=-1; j=edge[j].nxt){
			if(edge[j^1].val && edge[j].too!=ss){
				int t=edge[j].too;
				printf("%d %d ", xx[t], yy[t]);
			}
		}
	}
	return 0;
}
posted @ 2018-01-19 15:57  poorpool  阅读(150)  评论(0编辑  收藏  举报