Fiber Communications (暴力)

Fiber Communications
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4150 Accepted: 1251

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个点  给出p个点对 求当这p个点对连接起来的时候 最少用几条线,且连接的时候只能绕着圆 顺时针或者逆时针连

思路 :n个点 最多n-1条线就能使所有点相连接,存在两个点 它们之间不需存在线,暴力枚举所有的点,首尾相连的圆不易计算距离,化为对应的直线较容易计算,在枚举到一点时,将该点设为起始点,则相应的终点也在该起始点的基础上增大n-1

code:

#include<stdio.h>
#include<cstring>
using namespace std;
struct node
{
    long x,y;
} p[10001];
char flag[2001];//<strong><span style="color:#ff0000;">如果是int 超时!!!</span></strong>
long st,ed,n,m,i,j,tem,ans,tx,ty,k,tt;
<span style="color:#ff0000;"><strong>inline</strong></span> long jug(long x)//inline <strong>用时少一点</strong>
{
    if(x<st)
        return x+n;
    return x;
}
int main()
{
    scanf("%ld%ld",&n,&m);
    for(i=1; i<=m; i++)
    {
        scanf("%ld%ld",&p[i].x,&p[i].y);
        if(p[i].x>p[i].y)
        {
            tt=p[i].x;
            p[i].x=p[i].y;
            p[i].y=tt;
        }
    }
    ans=n;
    for(i=1; i<n; i++)//枚举每一个点
    {
        memset(flag,0,sizeof(flag));
        st=i,ed=st+n-1,tem=0;//<span style="font-size:18px;">在枚举到一点时,将该点设为起始点,则相应的终点也在改起始点的基础上增大n-1  </span>
        for(j=1; j<=m; j++)
        {
            tx=jug(p[j].x);//判断 如果点对中的点<起始点 将该点增大n,变成直线中的对应的新点
            ty=jug(p[j].y);
            if(tx>ty)//输入的点对 在起始点不同的时候 相对编号也不同
            {
                tt=tx;
                tx=ty;
                ty=tt;
            }
            for(k=tx; k<ty; k++)//将每一个点对之间的连线记录
                flag[k]=1;
        }
        for(j=st; j<=ed; j++)
            if(flag[j])
                tem++;
        if(ans>tem)
            ans=tem;
    }
    printf("%ld",ans);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值