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[];
int edge[],n,LOVE,i,k,Ans=,farthest,ans;
int main()
{
scanf("%d %d",&n,&LOVE);
for(i=;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=;k<=n;k++)
{
memset(edge,,sizeof(edge));
ans=;
for(i=;i<=LOVE;i++)
{
if(k+<=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+;
edge[]=max(love[i].girl,edge[]);
}
}
farthest=;
for(i=;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 ;
}

POJ1944 Fiber Communications (USACO 2002 February)的更多相关文章

  1. [USACO2002][poj1944]Fiber Communications(枚举)

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3804   Accepted: 1 ...

  2. TOJ1550: Fiber Communications

    1550: Fiber Communications  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal ...

  3. POJ 1944:Fiber Communications

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4236   Accepted: 1 ...

  4. usaco 2002 月赛 Fiber Communications 题解

    Description Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a ...

  5. USACO 2017 February Platinum

    第二次参加USACO 本来打算2016-2017全勤的 January的好像忘记打了 听群里有人讨论才想起来铂金组三题很有意思,都是两个排列的交叉对问题 我最后得分889/1000(真的菜) T1.W ...

  6. USACO 2016 February Contest, Gold解题报告

    1.Circular Barn   http://www.usaco.org/index.php?page=viewproblem2&cpid=621 贪心 #include <cstd ...

  7. POJ 1944 - Fiber Communications

    原题地址:http://poj.org/problem?id=1944 题目大意:有n个点排成一圈,可以连接任意两个相邻的点,给出 p 对点,要求这 p 对点必须直接或间接相连,求最少的连接边数 数据 ...

  8. usaco 2002 月赛 Chores 题解

    Description Farmer John's family pitches in with the chores during milking, doing all the chores as ...

  9. USACO 2017 February Gold

    那天打cf前无聊练手 T1.Why Did the Cow Cross the Road 题目大意:N*N的矩阵,从左上角走到右下角,走一步消耗T,每走3步消耗当前所在位置上的权值,求最小消耗 思路: ...

随机推荐

  1. 转:JSP之include动态包含与静态包含

    原文地址:http://www.cnblogs.com/ygj0930/p/6044676.html JSP中,include是一个经常用到的标签.当应用程序中所有的页面的某些部分(如标题.页脚和导航 ...

  2. LeetCode 637 Average of Levels in Binary Tree 解题报告

    题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  3. Struts2中.properties文件放置路径(classpath)

    一.web应用的classpath简介   classpath路径,即WEB-INF下面的classes目录,所有src目录下面的java.xml.properties等文件编译后都会在此. Stru ...

  4. Python开发【笔记】:列表转字典

    列表转字典 it = [1,2,3,4] print(dict(zip(it, it))) # {1: 1, 2: 2, 3: 3, 4: 4} it = iter(it) print(dict(zi ...

  5. glove理解

    先贴个看起来讲的不错的文章链接,后续详细看了再补充https://blog.csdn.net/leyounger/article/details/78206832 2018.10.28大概了解了下gl ...

  6. jquery代码修改input的value值,而页面上input框的值没有改变的解决办法

    问题描述: 在搜索框中输入一些字符,并且点击搜索框右边的五角星做收藏操作时,打开的弹框中Save Search:后面的input中的值被赋值了外面搜索框的值,但是当此次操作完成之后,再次做同样的操作, ...

  7. 封装||property

    封装 封装:主要是指在类的定义阶段将,以__开头的属性名进行变形..例如:__name ==> _People__name 封装的主要特点: 1.在类外部无法直接__name,想要在外部调用可以 ...

  8. python 内置方法expandtabs 把字符串格式化成列表输出

    #!/usr/bin/python3 # -*- coding: utf-8 -*- test = "username\tmail\tage\nzhangsen\tzhangsen@qq.c ...

  9. linux 执行远程linux上的shell脚本或者命令以及scp 上传文件到ftp--免密码登陆

    场景:在linux A 上执行Linux B上的shell脚本和命令 步骤1.设置ssh免登陆 1.SSH无密码登录 # 本地服务器执行(A机器):生成密钥对 ssh-keygen -t dsa -P ...

  10. test4