POJ1944 Fiber Communications

POJ1944 Fiber Communications
Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 2830Accepted: 871

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对点对,要求每个点对之间都有线,连线只能绕着圈连,问完成任务,最少需要多少条线。
解题思路:暴力枚举+优化。
1.最后肯定不需要围成一个圈,那么就至少有一个点是间断点,即连线不能跨过这个点;
2.所以在n的范围内,枚举这个点所在的位置,然后计算出连线的分布,求出最小值即可。
但这里有一个优化。如果十分暴力,这道题目还是会超时的,这个优化能算是dp吗,有些东西被归在dp里,可是我总也不觉得它和平时的dp有共同性,比方这个优化:我们用一个数组来记录某个点到另一个点之间有连线,dp[i]=j;这样能方便地求出需要连多少线。如果只是单纯地记录哪些点被访问过,这样做会错。
看代码:
#include <stdio.h>
#include <string.h>
#define N 1005
#define INF 0x3f3f3f3f
#define MAX(a,b) ((a)>(b)?(a):(b))

int n,p,to[N];
struct E
{
    int sta,ed;
}e[10005];

void swap(int &a,int &b)
{
    int temp=a;
    a=b;b=temp;
}

void re(void)
{
    scanf("%d%d",&n,&p);
    for(int i=1;i<=p;i++)
    {
        scanf("%d%d",&e[i].sta,&e[i].ed);
        if(e[i].sta>e[i].ed) swap(e[i].sta,e[i].ed);
    }
}

void run(void)
{
    int ans=INF;
    for(int i=1;i<=n;i++)
    {
        memset(to,0,sizeof(to));
        int sum=0;
        for(int j=1;j<=p;j++)
        {
            if(i>e[j].sta&&i<=e[j].ed)
            {
                to[1]=MAX(to[1],e[j].sta);
                to[e[j].ed]=n+1;
            }
            else
                to[e[j].sta]=MAX(to[e[j].sta],e[j].ed);
        }
        sum=0;
        int ri=0;
        for(int j=1;j<=n;j++)
        {
            if(!to[j])continue;
            if(j>ri)
            {
                sum+=to[j]-j;
                ri=to[j];
            }
            else if(to[j]>ri)
            {
                sum+=to[j]-ri;
                ri=to[j];
            }
        }
        ans=ans<sum?ans:sum;
    }
    printf("%d\n",ans);
}

int main()
{
    re();
    run();
    return 0;
}

  

转载于:https://www.cnblogs.com/Fatedayt/archive/2011/10/11/2207749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值