POJ 1944

题意:n个点的环,只能邻接点建边,现在要求m对点相连,求最小边。

题解:最后必定是生成的一些链,否则可以把环打开少用一条边。那么可以枚举这个断点在哪,计算取最小值。

View Code
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 #include<queue>

 5 using namespace std;

 6 const int N=1005;

 7 int to[N],po[10005][2],n,m;

 8 int main()

 9 {

10     while(scanf("%d%d",&n,&m)!=EOF)

11     {

12         int ans=N;

13         for(int i=0; i<m; i++)

14         {

15             scanf("%d%d",&po[i][0],&po[i][1]);

16             if(po[i][0]>po[i][1])

17                 swap(po[i][0],po[i][1]);

18         }

19         for(int i=1; i<=n; i++)

20         {

21             memset(to,0,sizeof(to));

22             for(int j=0; j<m; j++)

23             {

24                 int a=po[j][0],b=po[j][1];

25                 if(a<=i&&b>i)

26                 {

27                     to[0]=1;

28                     to[1]=max(to[1],a);

29                     to[b]=n;

30                 }

31                 else

32                 {

33                     to[a]=max(b,to[a]);

34                 }

35             }

36             int sum=0,k=0;

37             for(int j=0; j<n; j++)

38             {

39                 if(to[j]<=k)

40                     continue;

41                 sum+=to[j]-max(k,j);

42                 k=to[j];

43             }

44             ans=min(ans,sum);

45         }

46         printf("%d\n",ans);

47     }

48     return 0;

49 }

你可能感兴趣的:(poj)