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

Telephone Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6262 Accepted: 2311

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 telephone poles conveniently numbered 1..N that are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.

The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and need to be connected by a path of cables; the rest of the poles might be used or might not be used.

As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.

Determine the minimum amount that Farmer John must pay.

Input

* Line 1: Three space-separated integers: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, and Li

Output

* Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.

Sample Input

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

Sample Output

4
题意:有n个节点p条无向边,现在可以选择其中的任意K条免费,如果必须的边多与K跳,则花费多余所需边中权值最大的一个,求最小花费多少,即求解从1到N的路径中第K+1大的边最小。
 
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>



using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e4 + 4;
const int MAXM = 1e6 + 5;
typedef pair<int, int> P;

struct Edge {
	int v, w, nxt;
} E[MAXN << 2];
int rear, Head[MAXN << 2], d[MAXN];
int N, p, K;

void edge_Init() {
	rear = 0;
	memset(Head, -1, sizeof(Head));
}

void edge_add(int u, int v, int cost) {
	E[rear].v = v;
	E[rear].w = cost;
	E[rear].nxt = Head[u];
	Head[u] = rear ++;
}

bool C(int m) {
	priority_queue<P, vector<P>, greater<P> >que;
	fill(d, d + N + 1, INF);
	d[1] = 0;
	que.push(P(0, 1));
	while(!que.empty()) {
		P p = que.top();
		que.pop();
		int u = p.second;
		if(d[u] < p.first) continue;
		for(int i = Head[u]; ~i; i = E[i].nxt) {
			int cost = d[u] + (E[i].w >= m);
			if(d[E[i].v] > cost) {
				d[E[i].v] = cost;
				que.push(P(cost, E[i].v));
			}
		}
	}
	return d[N] > K;
}

void solve() {
	int lb = 0, ub = MAXM + 5;
	while(ub - lb > 1) {
		int mid = (ub + lb) >> 1;
		if(C(mid)) {
			lb = mid;
		} else {
			ub = mid;
		}
	}
	printf("%d\n", lb > MAXM ? -1 : lb);
}

int main() {
	int a, b, c;
	while(~scanf("%d%d%d", &N, &p, &K)) {
		edge_Init();
		for(int i = 0; i < p; i ++) {
			scanf("%d%d%d", &a, &b, &c);
			edge_add(a, b, c);
			edge_add(b, a, c);
		}
		solve();
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值