POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)

题意

Farmer John想从电话公司修一些电缆连接到他农场。已知N个电线杆编号为1,2,?N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接。

现给出P对电线杆距离Ai,Bi,Li表示Ai和Bi可连接,需要长度为Li的电缆。

电话公司赞助FJ K条免费电缆,额外的支出为剩下所需电缆的最大长度。求出最小费用。

思路

设mid为某条线的长度,长于mid的线放到免费额度里,否则自己掏钱。如果存在一个临界值x,使得长于x的电线数量恰好等于K,这个临界值对应的解就是最优解。如何计算长于mid的电线数量呢?排序比较肯定不行的,因为不知道这条电线用不用得上。所以需要Dijkstra,又因为在mid固定的条件下,电线的花费只取决于长度是否大于mid,所以可以将大于的取为1,小于的取为0。这样花费就可以计算了,并且花费等于长于mid的电线数量,也就是需要自己掏钱的电线的数量。

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
int N, P, K;
struct  edge { // 顶点属性
	int to, length;
	edge(int to, int length) : to(to), length(length) {}
	bool operator<(const edge &b) const {
		return length > b.length;
	}
};
vector<edge> G[1005];
int d[1005];
bool dijkstra_C(int s, int x) { // 求出长度>x的最少个数(>x边权为1,否则为0,求最短路),判断x是否免费,当二分判定出第一个不免费的x,就是所求的最小的最大x。
	priority_queue<edge> que;
	memset(d, 0x3f, sizeof(d));
	d[s] = 0;
	que.push(edge(s, 0));
	while (!que.empty()) {
		edge p = que.top(); que.pop();
		int v = p.to;
		if (d[v] < p.length) continue;
		for (unsigned int i = 0; i<G[v].size(); ++i) {
			edge e = G[v][i];
			int len = e.length > x;
			if (d[e.to] > d[v] + len) {
				d[e.to] = d[v] + len;
				que.push(edge(e.to, d[e.to]));
			}
		}
	}
	return d[N] <= K;
}
void solve() {
	dijkstra_C(1, 1);
	if (d[N] == 0x3f3f3f3f) {
		printf("-1\n");
		return;
	}
	int lb = -1, ub = 1000000 + 5; // (lb, ub)
	while (ub - lb > 1) {
		int mid = (lb + ub) >> 1;
		if (dijkstra_C(1, mid)) ub = mid; // (lb, ub]
		else lb = mid;
	}
	printf("%d\n", ub);
}
int main() {
	scanf("%d%d%d", &N, &P, &K);
	int a, b, l;
	for (int i = 0; i < P; ++i) {
		scanf("%d%d%d", &a, &b, &l);
		G[a].push_back(edge(b, l));
		G[b].push_back(edge(a, l));
	}
	solve();
	return 0;
}
时间: 2024-10-11 18:19:29

POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)的相关文章

POJ 3662 Telephone Lines(二分+最短路)

查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <vector> 5 #include <functional> 6 using namespace std; 7 #define maxv 1010 8 #define maxl 1000000 9 struct edge 10 { 11 i

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

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

poj 3662 Telephone Lines dijkstra+二分搜索

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

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

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

POJ 3662 Telephone Lines (分层图)

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

poj 3662 Telephone Lines spfa算法的灵活运用

题意: 给一个有n个结点的无向图,要求一条从1到n的路径,你可以让其中的k条免费,这条路径的花费是这条路径上剩下的边中长度的最大值,现在要求花费的最小值. 思路: 这道题可以首先想到二分枚举路径上的最大值,我觉得用spfa更简洁一些.spfa的本质是一种搜索算法,既然是搜索,就涉及到状态的转移.在一般求最短路的spfa算法中,当到结点u时,对e(u,v)只需做如下转移:if(d[v]>d[u]+w(e)) d[v]=d[u]+w(e).在跟一般的情况下,到结点u,对e(u,v)需做多种转移,比如

POJ 3662 Telephone Lines(二分答案+SPFA)

[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求最小费用. [题解] 二分答案,对于大于二分答案的边权置为1,小于等于的置为0, 则最短路就是超出二分答案的线数,如果小于等于k,则答案是合法的 [代码] #include <cstdio> #include <cstring> using namespace std; const i

poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system. There are N (1 ≤ N ≤ 1,000) forlorn t

POJ 3662 Telephone Lines (分层图做法)

Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system. There are N (1 ≤ N ≤ 1,000) forlorn t