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.
样例输入
  1. 5 2
  2. 1 3
  3. 4 5
样例输出
  1. 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;
最后愉快的贴出代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. struct node
  6. {
  7. int girl,boy;
  8. }love[];
  9. int edge[],n,LOVE,i,k,Ans=,farthest,ans;
  10. int main()
  11. {
  12. scanf("%d %d",&n,&LOVE);
  13. for(i=;i<=LOVE;i++)
  14. {
  15. scanf("%d %d",&love[i].girl,&love[i].boy);
  16. if(love[i].girl>love[i].boy)
  17. {
  18. k=love[i].girl;
  19. love[i].girl=love[i].boy;
  20. love[i].boy=k;
  21. }
  22. }
  23. for(k=;k<=n;k++)
  24. {
  25. memset(edge,,sizeof(edge));
  26. ans=;
  27. for(i=;i<=LOVE;i++)
  28. {
  29. if(k+<=love[i].girl||k>=love[i].boy)
  30. edge[love[i].girl]=max(love[i].boy,edge[love[i].girl]);
  31. else
  32. {
  33. edge[love[i].boy]=n+;
  34. edge[]=max(love[i].girl,edge[]);
  35. }
  36. }
  37. farthest=;
  38. for(i=;i<=n;i++)
  39. if(edge[i]>farthest)
  40. {
  41. ans+=edge[i]-max(i,farthest);
  42. farthest=edge[i];
  43. }
  44. if(Ans>ans) Ans=ans;
  45. }
  46. printf("%d",Ans);
  47. return ;
  48. }

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. Flink - InputGate

    初始化 Task List<InputGateDeploymentDescriptor> consumedPartitions = tdd.getInputGates(); // Cons ...

  2. C# decimal指定精度

    最近做一个项目.遇到了decimal 如何指定精度的问题 一般的指定参数    param = new SqlParameter(ParamName, DbType); 但decimal就不能只通过构 ...

  3. Nand Flash 驱动框架

    框架入口源文件:s3c_nand.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6   硬件平台:JZ2440 以下是驱动框架: 以下是驱动代码 s3c_nan ...

  4. Log4j rootLogger配置

    Log4j 根配置语法 log4j.rootLogger = [ level ] , appenderName, appenderName, … 指代 把指定级别的日志信息输出到指定的一个或者多个位置 ...

  5. oracle中索引快速全扫描和索引全扫描的区别

    当进行index full scan的时候 oracle定位到索引的root block,然后到branch block(如果有的话),再定位到第一个leaf block, 然后根据leaf bloc ...

  6. 20171018 在小程序页面去获取用户的OpenID

    1. 在小程序的.js 文件中增加代码 //加载页面时到后台服务去获取openID onLoad: function (options) { //OpenId wx.login({ //获取code ...

  7. 并发编程---IO模型

    IO模型 任务的提交方式有两种: 同步:应用程序提交完任务,等待结果结果,之后在执行下一个任务 异步:应用程序提交完任务,继续执行不等待结果,任务执行完,会自动出发异步中的会带哦函数 同步不等于阻塞: ...

  8. 004-docker命令-容器生命周期管理、容器操作

    1.容器生命周期管理 docker run :创建一个新的容器并运行一个命令 语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: - ...

  9. git diff 与 git diff --cached的不用

    git diff比较的是工作目录中当前文件和暂存区域快照之间的差异, 也就是修改之后还没有暂存起来的变化内容.若要查看已暂存的将要添加到下次提交里的内容,可以用 git diff --cached 命 ...

  10. poi 生成图片到excel

    try { InputStream iss = new FileInputStream("D:\\test.xlsx"); XSSFWorkbook wb = new XSSFWo ...