POJ3662-Telephone Lines

52 篇文章 0 订阅
44 篇文章 0 订阅

Telephone Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6952 Accepted: 2544

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

Source


题意:一共有N个电线杆,有P对电线杆是可以连接的,用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电线杆N能相互通信,并且电线公司提出K条电线是可以免费使用的,当使用电线的数量超过K条,超出的电线要收费,收的总费用为去掉免费使用的K条电线之后最长的那条电线的长度。现在需要尽可能的减少费用,问最少费用是多少

解题思路:最短路+二分,二分第k+1条长的长度,然后按照二分的值用0,1处理整个图:将比二分值大的边都置为1,将比二分值小的边都置位0,然后进行找1到n的最短路。不过若1到n的边数小于等于k,答案为0


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m, k;
int dis[1005], visit[1005], ans[1005];
int s[1005], nt[20005], e[20005], l[20005];

struct node
{
	int id,dis;
	friend bool operator <(node a, node b)
	{
		return a.dis > b.dis;
	}
}pre,nt1;

int Dijkstra(int x)
{
	memset(visit, 0, sizeof visit);
	memset(dis, INF, sizeof dis);
	priority_queue<node>q;
	pre.id = 1, pre.dis = 0;
	dis[1] = 0;
	q.push(pre);
	while (!q.empty())
	{
		pre = q.top();
		q.pop();
		visit[pre.id] = 1;
		for (int i = s[pre.id]; ~i; i = nt[i])
		{
			int ee = e[i];
			if (visit[ee]) continue;
			int kk = dis[pre.id] + (l[i] >= x ? 1 : 0);
			if (dis[ee] > kk)
			{
				dis[ee] = kk;
				nt1.id = ee;
				nt1.dis = dis[ee];
				q.push(nt1);
			}
		}
	}
	return dis[n] >= k + 1;
}

int main()
{
	while (~scanf("%d%d%d", &n, &m, &k))
	{
		memset(s, -1, sizeof s);
		int cnt = 1,u,v,w;
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d%d", &u, &v, &w);
			nt[cnt] = s[u], s[u] = cnt, e[cnt] = v, l[cnt++] = w;
			nt[cnt] = s[v], s[v] = cnt, e[cnt] = u, l[cnt++] = w;
		}
		int l = 0, r = 1000002, ans = -1;
		while (l <= r)
		{
			int mid = (l + r) / 2;
			if (Dijkstra(mid)) l = mid + 1,ans=mid;
			else r = mid - 1;
		}
		if(ans>1000000) ans=-1;
		if(r<0) ans=0;
		printf("%d\n", ans);
	}
	return 0;
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值