POJ 1944 Fiber Communications(暴力枚举)

Description:

Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a new fiber-optic network. However, the barns are located in a circle around the edge of a large pond, so he can only connect pairs of adjacent barns. The circular configuration means that barn N is adjacent to barn 1. 

FJ doesn't need to connect all the barns, though, since only certain pairs of cows wish to communicate with each other. He wants to construct as few 
connections as possible while still enabling all of these pairs to communicate through the network. Given the list of barns that wish to communicate with each other, determine the minimum number of lines that must be laid. To communicate from barn 1 to barn 3, lines must be laid from barn 1 to barn 2 and also from barn 2 to barn 3(or just from barn 3 to 1,if n=3).

Input

* Line 1: Two integers, N and P (the number of communication pairs, 1 <= P <= 10,000) 

* Lines 2..P+1: two integers describing a pair of barns between which communication is desired. No pair is duplicated in the list. 

Output

One line with a single integer which is the minimum number of direct connections FJ needs to make.

Sample Input

5 2
1 3
4 5

Sample Output

3

Hint

[Which connect barn pairs 1-2, 2-3, and 4-5.] 

题意:

N头牛,要连接M个区间求需要连接的最小线段数,因为是一个圆圈,所以最大就是n-1,就枚举从每个点截开后所以结果的最小值。

AC代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
using namespace std;
int i,j,k,l;
int ans,cnt;
char c[510][510];
int n,m,x,y,b;
bool flag1;
int move1[5]={0,0,1,-1};
int move2[5]={1,-1,0,0};
int a[10005][2],t[3000];
int main()
{
    int ans,now;
    scanf("%d %d",&n,&m);
    for(i=1; i<=m; i++)
        scanf("%d %d",&a[i][0],&a[i][1]);
        ans=INF;
    for(i=1;i<=n;i++)
    {
        memset(t,0,sizeof(t));
        now=0;
        for(j=1; j<=m; j++)
        {
            a=a[j][0];
            if(a<i)
                a+=n;
            b=a[j][1];
            if(b<i)
                b+=n;
            if(a>b)
                swap(a,b);
            t[a]=max(t[a],b);
        }
        a=i;
        for(j=i;j<i+n;j++)
            if(t[j]>a)
            {
                now+=t[j]-max(a,j);
                a=t[j];
            }
        ans=min(ans,now);
    }
    printf("%d\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值