POJ1944 Fiber Communications (USACO 2002 February)

Fiber Communications

总时间限制: 
1000ms
内存限制: 
65536kB
描述
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).
输入
* 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.
输出
One line with a single integer which is the minimum number of direct connections FJ needs to make.
样例输入
5 2
1 3
4 5
样例输出
3
提示
[Which connect barn pairs 1-2, 2-3, and 4-5.]
来源
USACO 2002 February
题目大意是:有N个农场,编号分别是1-n,有P对奶牛想沟通。这n个农场的路构成了一个环,每次只能连接相邻的农场。问要使这P对奶牛都相互沟通,最少连多少条边(保证答案不等于N+1)
思路:如果有5个农场,1和3想沟通,那么1-3有两条路径
  1->2->3
  3->4->5->1
所以枚举断边的时候判断断的这条边对当前这对奶牛的沟通有没有影响,如果有,就倒着走。
edge【i】指向i所要连的边,如果从i倒着走daoj(i>j) 那么edge【i】=n+1;edge【1】=j;
最后愉快的贴出代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int girl,boy;
}love[10007];
int edge[1007],n,LOVE,i,k,Ans=1000000000,farthest,ans;
int main()
{
    scanf("%d %d",&n,&LOVE);
    for(i=1;i<=LOVE;i++)
    {
        scanf("%d %d",&love[i].girl,&love[i].boy);
        if(love[i].girl>love[i].boy)
        {
            k=love[i].girl;
            love[i].girl=love[i].boy;
            love[i].boy=k;
        }
    }
    for(k=1;k<=n;k++)
    {
        memset(edge,0,sizeof(edge));
        ans=0;
        for(i=1;i<=LOVE;i++)
        {
            if(k+1<=love[i].girl||k>=love[i].boy)
              edge[love[i].girl]=max(love[i].boy,edge[love[i].girl]);
            else
            {
                edge[love[i].boy]=n+1;
                edge[1]=max(love[i].girl,edge[1]);
            }
        }
        farthest=0;
        for(i=1;i<=n;i++)
          if(edge[i]>farthest)
          {
            ans+=edge[i]-max(i,farthest);
            farthest=edge[i];
          }
        if(Ans>ans) Ans=ans;
    }
    printf("%d",Ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/Peper/p/7231025.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值