• POJ1944 Fiber Communications


    POJ1944 Fiber Communications
    Time Limit: 1000MS Memory Limit: 30000K
    Total Submissions: 2830 Accepted: 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;
    }
    

      

  • 相关阅读:
    ASP.NET请求管道、应用程序生命周期、整体运行机制
    PHP面试总结
    ASP.NET MVC源码分析系列
    SQL中的重要语句
    Nicescroll滚动条插件的用法
    Nunit2.5.10快速上手(笔记)
    ucore 源码剖析
    《ucore lab8》实验报告
    《ucore lab7》实验报告
    《ucore lab6》实验报告
  • 原文地址:https://www.cnblogs.com/Fatedayt/p/2207749.html
Copyright © 2020-2023  润新知