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[];
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)的更多相关文章
- [USACO2002][poj1944]Fiber Communications(枚举)
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3804 Accepted: 1 ...
- TOJ1550: Fiber Communications
1550: Fiber Communications Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal ...
- POJ 1944:Fiber Communications
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4236 Accepted: 1 ...
- usaco 2002 月赛 Fiber Communications 题解
Description Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a ...
- USACO 2017 February Platinum
第二次参加USACO 本来打算2016-2017全勤的 January的好像忘记打了 听群里有人讨论才想起来铂金组三题很有意思,都是两个排列的交叉对问题 我最后得分889/1000(真的菜) T1.W ...
- USACO 2016 February Contest, Gold解题报告
1.Circular Barn http://www.usaco.org/index.php?page=viewproblem2&cpid=621 贪心 #include <cstd ...
- POJ 1944 - Fiber Communications
原题地址:http://poj.org/problem?id=1944 题目大意:有n个点排成一圈,可以连接任意两个相邻的点,给出 p 对点,要求这 p 对点必须直接或间接相连,求最少的连接边数 数据 ...
- usaco 2002 月赛 Chores 题解
Description Farmer John's family pitches in with the chores during milking, doing all the chores as ...
- USACO 2017 February Gold
那天打cf前无聊练手 T1.Why Did the Cow Cross the Road 题目大意:N*N的矩阵,从左上角走到右下角,走一步消耗T,每走3步消耗当前所在位置上的权值,求最小消耗 思路: ...
随机推荐
- 洛谷P2303 [SDOi2012] Longge的问题 数论
看懂了题解,太妙了TT但是想解释的话可能要很多数学公式打起来太麻烦了TT所以我就先只放代码具体推演的过程我先写在纸上然后拍下来做成图片放上来算辣quq 好的那我先滚去做题了做完这题就把题解放上来.因为 ...
- install python in wine
wget http://www.kegel.com/wine/winetricks chmod +x winetricks ./winetricks 钩选msxml3就可以了. ___________ ...
- nodejs 学习五 单元测试一
一. chai chai 自身是依赖nodejs的 assert,让检测更加语义化. chai 采用两种模式,TDD和BDD, TDD是类似自然语言方式 BDD是结构主义 chai文旦地址 二.moc ...
- DNS搜索
dig(Domain Information Groper) dig @dnsserver name querytype 如果你设置的dnsserver是一个域名,那么dig会首先通过默认的上连DNS ...
- sql语句优化(二)
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 建立索引好处 : 之前做的一个项目 , 一个查询10w多条的数据 ,需要20s ,后来加 ...
- 转场动画UINavigationControllerDelegate
从iOS7开始,苹果更新了自定义ViewController转场的API,这些新增的类和接口让很多人困惑,望而却步.本文就从这些API入口,让读者理清这些API错综复杂的关系. 几个protocol ...
- Python3学习之路~4.3 装饰器
定义:本质是函数,装饰其他函数就是为其他函数添加附加功能. 原则: 不能修改被装饰函数的源代码 不能修改被装饰函数的调用方式 实现装饰器知识储备: 函数即“变量” 高阶函数 把一个函数名当做实参传递给 ...
- 下载以及安装VMware Player Pro 64位
1.下载地址:https://download3.vmware.com/software/wkst/file/VMware-workstation-full-14.0.0-6661328.exe 2. ...
- web应用1
阅读内容 一.web应用 二.web协议 三.web框架 四.补充知识 回到顶部 一.web应用 二.web协议 三.web框架 四.补充知识 五.补充知识
- 自定义安装visual studio 2010开发asp.net
VS2010的安装对于VS的安装大家肯定都熟悉,不过我在很多地方看到的是大家讲VS的全部组件都安装了,不但浪费磁盘空间,还降低了系统性能,除此之外,还有人安装了VS之后不知道顺手把MSDN安装上,害得 ...