洛谷P2526[SHOI2001]小狗散步
思路:
题意可以转换为,选择一段路径去相遇点或者喜欢的景点。这个点就被分成了两类,自然想到了二分图,将相遇点和可以到达的景点连边,求出二分图的最大匹配,再加上 n n n就是答案一。求经过的点,只要在残余网络上判断一个由相遇点到景点的边权是否为 0 0 0。
代码:
#include<bits/stdc++.h>
#define pii pair<int,int>
#define int long long
#define cl(x,y) memset(x,y,sizeof(x))
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
const int N=5e5+210;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
using namespace std;
struct edge
{
int u,v,w;
}maze[N<<1];
int len=1,head[N]={0};
int dep[N];//深度
void add(int u,int v,int w)
{
maze[++len]={head[u],v,w};
head[u]=len;
}
void inc(int u,int v,int w)
{
add(u,v,w);
add(v,u,0);
}
int dfs(int u,int f,int t)
{
int ans=0,i;
if(u==t)
return f;
for(i=head[u];i && f;i=maze[i].u)
{
int v=maze[i].v,w=maze[i].w;
if(dep[v]==dep[u]+1 && w)//符合深度关系且能流
{
int sum=dfs(v,min(f,w),t);
maze[i].w-=sum;
maze[i^1].w+=sum;
f-=sum;
ans+=sum;
}
}
if(!ans)
dep[u]=-2;
return ans;
}
int bfs(int s,int t)
{
queue<int> q;
cl(dep,0);
dep[s]=1;//源点深度为1
q.push(s);
while(!q.empty())
{
int u=q.front(),i;
q.pop();
for(i=head[u];i;i=maze[i].u)
{
int v=maze[i].v,w=maze[i].w;
if(w && !dep[v])//有深度且能流
{
dep[v]=dep[u]+1;
q.push(v);
}
}
}
return dep[t];
}
int dinic(int s,int t)
{
int ans=0;
while(bfs(s,t))
ans+=dfs(s,inf,t);
return ans;
}
struct point
{
int x,y;
}a[110],b[110];
double dis(point a,point b)
{
double ans=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
return ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,m,i,j;
cin>>n>>m;
int s=0,t=n+m+1;
for(i=1;i<=n;i++)
{
cin>>a[i].x>>a[i].y;
inc(s,i,1);
}
for(i=1;i<=m;i++)
{
cin>>b[i].x>>b[i].y;
inc(i+n,t,1);
}
for(i=1;i<n;i++)
for(j=1;j<=m;j++)
{
if(dis(a[i],a[i+1])*2>dis(a[i],b[j])+dis(b[j],a[i+1]))
inc(i,j+n,1);
}
cout<<n+dinic(s,t)<<endl;
for(i=1;i<n;i++)
{
cout<<a[i].x<<" "<<a[i].y<<" ";
for(j=head[i];j;j=maze[j].u)
if(!maze[j].w && maze[j].v!=s)
{
cout<<b[maze[j].v-n].x<<" "<<b[maze[j].v-n].y<<" ";
break;
}
}
cout<<a[n].x<<" "<<a[n].y<<" ";
return 0;
}