电话线

  题目大意:一堆电话线要你接,现在有N个接口,总线已经在1端,要你想办法接到N端去,电话公司发好心免费送你几段不用拉网线,剩下的费用等于剩余最长电话线的长度,要你求出最小的费用。

  这一看又是一个最小化最大值的问题(也可以看成是最大化最小值的问题),常规方法一样的就是把这个费用二分就好,但是这道题是道图论题,不一定经过所有的点,那我们就以二分基准长度为界限,把小于基准长度的那一部分看成是0,大于等于基准长度的看成是1,这样我们只用SPFA算法算最短路径就可以了,非常的巧妙

  参考:http://poj.org/showmessage?message_id=181794

  PS:好久没写SPFA了,都忘记是怎么写了,重新定义长度的时候又忘记乘以2了WA一个晚上真是日了

  

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #define SIZE 1010
  5.  
  6. using namespace std;
  7. typedef int Position;
  8.  
  9. struct _set
  10. {
  11. Position ed;
  12. int next;
  13. int length;
  14. }Path[];
  15. struct _head
  16. {
  17. int point;
  18. }Heads[SIZE];
  19. static int dist[SIZE];
  20. static bool visit[SIZE], oep[];
  21. static Position que[(SIZE + ) * ];
  22.  
  23. void solve(const int, const int, const int, const int);
  24. bool SPFA(const int, const int, const int, const int);
  25.  
  26. int main(void)
  27. {
  28. int Sum_Poles, Free_Cables, Sum_Path, length, L_Max;
  29. Position st, ed;
  30.  
  31. while (~scanf("%d%d%d", &Sum_Poles, &Sum_Path, &Free_Cables))
  32. {
  33. L_Max = -;
  34. for (int i = ; i <= Sum_Poles; i++)
  35. Heads[i].point = -;
  36. for (int i = ; i < * Sum_Path;)
  37. {
  38. scanf("%d%d%d", &st, &ed, &length);
  39. //无向图,两边都要存
  40. Path[i].ed = ed; Path[i].length = length; Path[i].next = Heads[st].point;
  41. Heads[st].point = i++;
  42.  
  43. Path[i].ed = st; Path[i].length = length; Path[i].next = Heads[ed].point;
  44. Heads[ed].point = i++;
  45.  
  46. L_Max = max(L_Max, length);
  47. }
  48. solve(Sum_Poles, Sum_Path, Free_Cables, L_Max);
  49. }
  50. return ;
  51. }
  52.  
  53. void solve(const int Sum_Poles, const int Sum_Path, const int Free_Cables, const int L_Max)
  54. {
  55. int lb = , rb = L_Max + , mid;
  56.  
  57. while (rb - lb > )//对距离二分
  58. {
  59. mid = (lb + rb) >> ;
  60. if (SPFA(mid, Sum_Path, Sum_Poles, Free_Cables)) lb = mid;
  61. else rb = mid;
  62. if (dist[Sum_Poles] == 0x3fffffff)
  63. //任何一次寻找过后,如果图能到N点,那么N的dist值一定不是0x3fffffff
  64. //否则,一定是不联通
  65. {
  66. printf("-1\n");
  67. return;
  68. }
  69. }
  70. printf("%d\n", lb);
  71. }
  72.  
  73. bool SPFA(const int x, const int Sum_Path, const int Sum_Poles, const int Free_Cables)
  74. {
  75. int head = , back = , out, to;
  76.  
  77. que[head] = ; //开始是从1开始的
  78.  
  79. for (int i = ; i < * Sum_Path; i++)
  80. oep[i] = Path[i].length < x ? : ;
  81.  
  82. fill(dist, dist + Sum_Poles + , 0x3fffffff);
  83. memset(visit, , sizeof(visit));
  84. dist[] = ;
  85.  
  86. while (head != back)
  87. {
  88. out = que[head]; head = (head + ) % ( * SIZE);
  89. visit[out] = ;
  90.  
  91. for (int k = Heads[out].point; k != -; k = Path[k].next)
  92. {
  93. to = Path[k].ed;
  94. if (dist[out] + oep[k] < dist[to])
  95. {
  96. dist[to] = dist[out] + oep[k];
  97. if (!visit[to])
  98. {
  99. visit[to] = ;
  100. que[back] = to; back = (back + ) % ( * SIZE);
  101. }
  102. }
  103. }
  104. }
  105. return dist[Sum_Poles] > Free_Cables;
  106. }

Divide and conquer:Telephone Lines(POJ 3662)的更多相关文章

  1. Telephone Lines POJ - 3662 (二分+spfa)

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...

  2. Divide and conquer:K Best(POJ 3111)

     挑选最美的珠宝 题目大意:挑选k个珠宝使得∑a/∑b最大,输出组合数 最大化平均值的标准题型,二分法就好了,一定要注意范围(10e-7),如果是10e-8就会tle,10e-6就是wa #inclu ...

  3. Divide and conquer:Dropping tests(POJ 2976)

    最大化平均值 题目大意:给定你n个分数,从中找出k个数,使∑a/∑b的最大值 这一题同样的也可以用二分法来做(用DP会超时,可见二分法是多么的实用呵!),大体上是这样子:假设最大的平均值是w,那么题目 ...

  4. Divide and conquer:Aggressive Cows(POJ 2456)

    侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 #include <iostream> #include <algorithm> #include < ...

  5. Divide and Conquer:Cable Master(POJ 1064)

    缆绳大师 题目大意,把若干线段分成K份,求最大能分多长 二分法模型,C(x)就是题干的意思,在while那里做下文章就可以了,因为这个题目没有要求长度是整数,所以我们要不断二分才行,一般50-100次 ...

  6. Divide and Conquer:Monthly Expense(POJ 3273)

    Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...

  7. Divide and Conquer:River Hopscotch(POJ 3258)

     去掉石头 题目大意:一群牛在河上的石头上跳来跳去,现在问你如何通过去掉M个石头,使得牛跳过石头的最短距离变得最大? 这一题比较经典,分治法的经典,二分法可以很方便处理这个问题,我们只要明白比较函数这 ...

  8. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  9. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

随机推荐

  1. WCF服务显示的是服务器名称而不是IP地址...

    打开http://xx.xx.xx.xx:端口号/Service1.svc页面显示的服务地址为: http://xx_yy_server:端口号/Service1.svc?wsdl 是显示的服务器的名 ...

  2. 面试集锦-常量,const, const 对指针的影响

    在C语言中不可改变的数据(量)就是常量    在C语言中有三种常量        字面量(直接量),就是直接写出来的,从写法上就可以看出值与类型等,例如:19,123.456等        名字常量 ...

  3. java httpclient发送json 请求 ,go服务端接收

    /***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...

  4. PHP array_multisort() 函数详解 及 二维数组排序(模拟数据表记录按字段排序)

    一.先看最简单的情况. 有两个数组: $arr1 = array(1, 9, 5); $arr2 = array(6, 2, 4); array_multisort($arr1, $arr2); pr ...

  5. mysql解决自动断开8小时未曾用过的链接

    今天有运维的同事反映,发布关键词不太稳定,点了没反应.就去线上看了一下日志,发现数据库没有链接,就查了一下问题 关于mysql自动断开的问题研究结果如下,在mysql中有相关参数设定,当数据库连接空闲 ...

  6. php网络编程

    php面试题之三--PHP网络编程(高级部分) 三.PHP网络编程 [!!!]1.禁用COOKIE后SEESION还能用吗?(51.com笔试题) 可以,COOKIE和SESSION都是用来实现会话机 ...

  7. 使用Ant部署应用程序系统

    1. 首先下载Ant http://ant.apache.org/ 配置环境变量 2. 编写build.xml部署文件如下: <?xml version="1.0" enco ...

  8. 关于NGUI与原生2D混用相互遮盖的问题心得

    http://www.fzgh.org.cn/zuixindianying/144224.html Native2D自己可以使用Sort Layer来排序,每层又有不同的Order In Layer, ...

  9. mac 下载安装 IntelliJ IDEA Tomcat

    (1)Download IntelliJ IDEA https://www.jetbrains.com/idea/download/ (2)找了个激活码 http://www.oschina.net/ ...

  10. Open CV缩放图像

    缩放图像是图像处理中需要经常使用的操作.太小的图像在图像识别中不能很好的处理,需要将其放大,太大的图像不方便储存,需要将其缩小,下面记录OpenCV图片缩放方法. 缩放函数 , , int inter ...