Divide and conquer:Telephone Lines(POJ 3662)
题目大意:一堆电话线要你接,现在有N个接口,总线已经在1端,要你想办法接到N端去,电话公司发好心免费送你几段不用拉网线,剩下的费用等于剩余最长电话线的长度,要你求出最小的费用。
这一看又是一个最小化最大值的问题(也可以看成是最大化最小值的问题),常规方法一样的就是把这个费用二分就好,但是这道题是道图论题,不一定经过所有的点,那我们就以二分基准长度为界限,把小于基准长度的那一部分看成是0,大于等于基准长度的看成是1,这样我们只用SPFA算法算最短路径就可以了,非常的巧妙
参考:http://poj.org/showmessage?message_id=181794
PS:好久没写SPFA了,都忘记是怎么写了,重新定义长度的时候又忘记乘以2了WA一个晚上真是日了
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #define SIZE 1010
- using namespace std;
- typedef int Position;
- struct _set
- {
- Position ed;
- int next;
- int length;
- }Path[];
- struct _head
- {
- int point;
- }Heads[SIZE];
- static int dist[SIZE];
- static bool visit[SIZE], oep[];
- static Position que[(SIZE + ) * ];
- void solve(const int, const int, const int, const int);
- bool SPFA(const int, const int, const int, const int);
- int main(void)
- {
- int Sum_Poles, Free_Cables, Sum_Path, length, L_Max;
- Position st, ed;
- while (~scanf("%d%d%d", &Sum_Poles, &Sum_Path, &Free_Cables))
- {
- L_Max = -;
- for (int i = ; i <= Sum_Poles; i++)
- Heads[i].point = -;
- for (int i = ; i < * Sum_Path;)
- {
- scanf("%d%d%d", &st, &ed, &length);
- //无向图,两边都要存
- Path[i].ed = ed; Path[i].length = length; Path[i].next = Heads[st].point;
- Heads[st].point = i++;
- Path[i].ed = st; Path[i].length = length; Path[i].next = Heads[ed].point;
- Heads[ed].point = i++;
- L_Max = max(L_Max, length);
- }
- solve(Sum_Poles, Sum_Path, Free_Cables, L_Max);
- }
- return ;
- }
- void solve(const int Sum_Poles, const int Sum_Path, const int Free_Cables, const int L_Max)
- {
- int lb = , rb = L_Max + , mid;
- while (rb - lb > )//对距离二分
- {
- mid = (lb + rb) >> ;
- if (SPFA(mid, Sum_Path, Sum_Poles, Free_Cables)) lb = mid;
- else rb = mid;
- if (dist[Sum_Poles] == 0x3fffffff)
- //任何一次寻找过后,如果图能到N点,那么N的dist值一定不是0x3fffffff
- //否则,一定是不联通
- {
- printf("-1\n");
- return;
- }
- }
- printf("%d\n", lb);
- }
- bool SPFA(const int x, const int Sum_Path, const int Sum_Poles, const int Free_Cables)
- {
- int head = , back = , out, to;
- que[head] = ; //开始是从1开始的
- for (int i = ; i < * Sum_Path; i++)
- oep[i] = Path[i].length < x ? : ;
- fill(dist, dist + Sum_Poles + , 0x3fffffff);
- memset(visit, , sizeof(visit));
- dist[] = ;
- while (head != back)
- {
- out = que[head]; head = (head + ) % ( * SIZE);
- visit[out] = ;
- for (int k = Heads[out].point; k != -; k = Path[k].next)
- {
- to = Path[k].ed;
- if (dist[out] + oep[k] < dist[to])
- {
- dist[to] = dist[out] + oep[k];
- if (!visit[to])
- {
- visit[to] = ;
- que[back] = to; back = (back + ) % ( * SIZE);
- }
- }
- }
- }
- return dist[Sum_Poles] > Free_Cables;
- }
Divide and conquer:Telephone Lines(POJ 3662)的更多相关文章
- Telephone Lines POJ - 3662 (二分+spfa)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...
- Divide and conquer:K Best(POJ 3111)
挑选最美的珠宝 题目大意:挑选k个珠宝使得∑a/∑b最大,输出组合数 最大化平均值的标准题型,二分法就好了,一定要注意范围(10e-7),如果是10e-8就会tle,10e-6就是wa #inclu ...
- Divide and conquer:Dropping tests(POJ 2976)
最大化平均值 题目大意:给定你n个分数,从中找出k个数,使∑a/∑b的最大值 这一题同样的也可以用二分法来做(用DP会超时,可见二分法是多么的实用呵!),大体上是这样子:假设最大的平均值是w,那么题目 ...
- Divide and conquer:Aggressive Cows(POJ 2456)
侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 #include <iostream> #include <algorithm> #include < ...
- Divide and Conquer:Cable Master(POJ 1064)
缆绳大师 题目大意,把若干线段分成K份,求最大能分多长 二分法模型,C(x)就是题干的意思,在while那里做下文章就可以了,因为这个题目没有要求长度是整数,所以我们要不断二分才行,一般50-100次 ...
- Divide and Conquer:Monthly Expense(POJ 3273)
Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...
- Divide and Conquer:River Hopscotch(POJ 3258)
去掉石头 题目大意:一群牛在河上的石头上跳来跳去,现在问你如何通过去掉M个石头,使得牛跳过石头的最短距离变得最大? 这一题比较经典,分治法的经典,二分法可以很方便处理这个问题,我们只要明白比较函数这 ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
随机推荐
- WCF服务显示的是服务器名称而不是IP地址...
打开http://xx.xx.xx.xx:端口号/Service1.svc页面显示的服务地址为: http://xx_yy_server:端口号/Service1.svc?wsdl 是显示的服务器的名 ...
- 面试集锦-常量,const, const 对指针的影响
在C语言中不可改变的数据(量)就是常量 在C语言中有三种常量 字面量(直接量),就是直接写出来的,从写法上就可以看出值与类型等,例如:19,123.456等 名字常量 ...
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- PHP array_multisort() 函数详解 及 二维数组排序(模拟数据表记录按字段排序)
一.先看最简单的情况. 有两个数组: $arr1 = array(1, 9, 5); $arr2 = array(6, 2, 4); array_multisort($arr1, $arr2); pr ...
- mysql解决自动断开8小时未曾用过的链接
今天有运维的同事反映,发布关键词不太稳定,点了没反应.就去线上看了一下日志,发现数据库没有链接,就查了一下问题 关于mysql自动断开的问题研究结果如下,在mysql中有相关参数设定,当数据库连接空闲 ...
- php网络编程
php面试题之三--PHP网络编程(高级部分) 三.PHP网络编程 [!!!]1.禁用COOKIE后SEESION还能用吗?(51.com笔试题) 可以,COOKIE和SESSION都是用来实现会话机 ...
- 使用Ant部署应用程序系统
1. 首先下载Ant http://ant.apache.org/ 配置环境变量 2. 编写build.xml部署文件如下: <?xml version="1.0" enco ...
- 关于NGUI与原生2D混用相互遮盖的问题心得
http://www.fzgh.org.cn/zuixindianying/144224.html Native2D自己可以使用Sort Layer来排序,每层又有不同的Order In Layer, ...
- mac 下载安装 IntelliJ IDEA Tomcat
(1)Download IntelliJ IDEA https://www.jetbrains.com/idea/download/ (2)找了个激活码 http://www.oschina.net/ ...
- Open CV缩放图像
缩放图像是图像处理中需要经常使用的操作.太小的图像在图像识别中不能很好的处理,需要将其放大,太大的图像不方便储存,需要将其缩小,下面记录OpenCV图片缩放方法. 缩放函数 , , int inter ...