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. Web发展简史

    World Wide Web--万维网,常指网页,网站 发展至今仅30年(1989-2019) Tim Berners-Lee(蒂姆.伯纳斯.李)英国计算机科学家,万维网的发明者. 1990年Tim ...

  2. js字符串方法汇总

    1.length方法 var stringObject=new String("hellow world"); console.log(stringObject.length);/ ...

  3. SpringMVC(三):参数绑定、输入输出转换

    一.参数解析绑定 1. 自定义绑定:不绑定某些项 @InitBinder private void initBinder(WebDataBinder dataBinder) { dataBinder. ...

  4. 第八节:分支开发之合并到master

    流程:在客户端创建分支,修改代码,并push,然后在页面处理即可.(区别在于一个在客户端,一个在页面) 实际的开放中要记得打tag,不然到时候出问题了以后不知道从哪里开始.

  5. urllib2 python3错误?用from urllib import request来代替!

    今天ytkah在一个python3项目要引用urllib2,可是提示ImportError: No module named 'urllib2'错误了.原来是urllib2可以在python2.x适用 ...

  6. 组合覆盖与PICT的使用

    组合覆盖法是一种有效减少测试用例个数的测试用例设计方法.根据覆盖程度的不同,可以分为单因素覆盖.成对组合覆盖.三三组合覆盖等.其中又以成对组合覆盖最常用. 关于组合覆盖的更多内容,参考:http:// ...

  7. 小程序编辑器vscode

    安装中文版 1)打开vscode工具: 2)使用快捷键组合[Ctrl+Shift+p],在搜索框中输入“configure display language”,点击确定后: 3)如图所示 =>安 ...

  8. K-means &K-medoids 聚类

    k-平均值算法对孤立点很敏感!因为具有特别大的值的对象可能显著地影响数据的分布. k-中心点(k-Medoids): 不采用簇中对象的平均值作为参照点, 而是选用簇中位置最中心的对象, 即中心点(me ...

  9. 列表 list 容器类型数据(str字符串, list列表, tuple元组, set集合, dict字典)--->元组 tuple-->字符串 str

    # ### 列表 list 容器类型数据(str字符串, list列表, tuple元组, set集合, dict字典) # (1)定义一个列表 listvar = [] print(listvar, ...

  10. Snmp学习总结(一)——Snmp的基本概念

    一.SNMP简单概述 1.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议& ...