题目大意:
有n个点,依次排成一个圆圈,给出一些点对,要求在这个圆圈添加一些边,是的这些点对相连,边只能在相邻的点之间添加,求最少添加边数
分析:
对于一个点对(x,y)
我们可以x—x+1—x+2….—y使这两个点相连
也可以y—y+1—y+2……n—1—……..x使这两个点相连
所以我们可以枚举一个分界线,也就是说,i和i+1这两个点之间不能有边相连,然后暴力模拟计算寻找最小值….
代码如下:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
const int maxn=1000+5,maxm=10000+5;
int n,p,lala[maxn];
struct M{
int x,y;
}s[maxm];
signed main(void){
int ans=1000000;
scanf("%d%d",&n,&p);
for(int i=1;i<=p;i++){
scanf("%d%d",&s[i].x,&s[i].y);
if(s[i].x>s[i].y)
swap(s[i].x,s[i].y);
}
for(int i=1;i<=n;i++){
memset(lala,0,sizeof(lala));
for(int j=1;j<=p;j++){
if(s[j].x<i&&s[j].y>=i)
lala[1]=max(lala[1],s[j].x),lala[s[j].y]=max(lala[s[j].y],n+1);
else
lala[s[j].x]=max(lala[s[j].x],s[j].y);
}
int tmp=0,r=0;
for(int i=1;i<=n;i++)
if(lala[i]){
if(i>r)
r=lala[i],tmp+=r-i;
else if(lala[i]>r)
tmp+=lala[i]-r,r=lala[i];
}
ans=min(ans,tmp);
}
cout<<ans<<endl;
return 0;
}
by >_< NeighThorn

本文针对一个特定的图论问题——时空隧道,提出了有效的解决方案。该问题要求在一系列按圆形排列的点中添加最少数量的边来连接指定的点对。通过枚举分界线并进行暴力模拟计算,找到最优解。
2610

被折叠的 条评论
为什么被折叠?



