Telephone Lines POJ - 3662 Dijkstra+ 二分答案

本文介绍了一种解决农民John连接农场到电话系统的电缆铺设问题的算法。通过使用二分查找确定最小成本,并利用最短路径算法进行优化,确保了在有限免费电缆长度下连接路径的成本最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N 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: N, P, and K
  • Lines 2…P+1: Line i+1 contains the three space-separated integers: Ai, Bi, 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

题意:简而言之,就是使第 k+1 大的路径权值尽量小,那么我们遇到这种问题,即可二分解决;
将>mid 的路径看成1,其余的为0 ,那么跑最短路的时候,看长度与 k 进行比较,调整上下界;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 300005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>


ll mul(ll a, ll b, ll mod) {
	ll rt = 0;
	while (b) {
		if (b & 1)rt = (rt + a) % mod;
		b = b / 2;
		a = (a << 1) % mod;
	}
	return rt;
}


ll quickpow(ll a, ll b,ll mod) {
	ll ans = 1, tmp = a;
	while (b > 0) {
		if (b % 2)ans = mul(ans, tmp, mod);
		b = b / 2;
		tmp = mul(tmp, tmp, mod);
	}
	return ans;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}

struct node {
	int w, to, nxt;
	bool operator < (const node& rhs)const {
		return w > rhs.w;
	}
}edge[maxn];

int n, m, k;
int tot;
int head[maxn];
int vis[maxn], dis[maxn];

void init() {
	ms(vis);
	memset(head, -1, sizeof(head));
	tot = 0;
	for (int i = 0; i <= maxn; i++)dis[i] = inf;
}

void addedge(int u, int v, int w) {
	edge[tot].to = v;
	edge[tot].w = w;
	edge[tot].nxt = head[u];
	head[u] = tot++;
}


int dijkstra(int s, int k) {
	memset(dis, 0x3f, sizeof(dis));
	dis[s] = 0;
	priority_queue<node>q;
	node tmp1, tmp2;
	tmp1.to = s;
	q.push(tmp1);
	ms(vis);
	while (!q.empty()) {
		tmp1 = q.top();
		q.pop();
		int u = tmp1.to;
		if (vis[u])continue;
		vis[u] = 1;
		for (int i = head[u]; i != -1; i = edge[i].nxt) {
			int tt = 0;
			int v = edge[i].to;
			if (edge[i].w >= k)tt = 1;
			if (vis[v] == 0 && dis[v] > dis[u] + tt) {
				dis[v] = dis[u] + tt;
				tmp2.to = v;
				tmp2.w = dis[v];
				q.push(tmp2);
			}
		}
	}
	return dis[n];
}

int main() {
	ios::sync_with_stdio(false);
	while (cin >> n >> m >> k) {
		init();
		int maxx = 0;
		for (int i = 0; i < m; i++) {
			int u, v, w; cin >> u >> v >> w;
			addedge(u, v, w); addedge(v, u, w);
			maxx = max(maxx, w);
		}
		int l = 0, r = maxx;
		int ans = 0;
		while (l <= r) {
			int mid = (r + l) >> 1;
			if (dijkstra(1, mid) <= k) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		if (l > maxx)cout << -1 << endl;
		else {
			cout << ans << endl;
		}
	}
}

最短路应该信手拈来,以及其相关的变形也应该很熟练,不应该存板子就完事,自己会默写,理解其中的原理才最重要;
洛谷的最短路模板题

内容概要:本文档提供了关于“微型车间生产线的设计与生产数据采集试验研究”的毕业设计复现代码,涵盖从论文结构生成、机械结构设计、PLC控制系统设计、生产数据采集与分析系统、有限元分析、进度管理、文献管理和论文排版系统的完整实现。通过Python代码和API调用,详细展示了各个模块的功能实现和相互协作。例如,利用SolidWorks API设计机械结构,通过PLC控制系统模拟生产流程,使用数据分析工具进行生产数据的采集和异常检测,以及利用进度管理系统规划项目时间表。 适合人群:具有机械工程、自动化控制或计算机编程基础的学生或研究人员,尤其是从事智能制造领域相关工作的人员。 使用场景及目标:①帮助学生或研究人员快速搭建和理解微型车间生产线的设计与实现;②提供完整的代码框架,便于修改和扩展以适应不同的应用场景;③作为教学或科研项目的参考资料,用于学习和研究智能制造技术。 阅读建议:此资源不仅包含详细的代码实现,还涉及多个学科领域的知识,如机械设计、电气控制、数据分析等。因此,在学习过程中,建议读者结合实际操作,逐步理解每个模块的功能和原理,并尝试调整参数以观察不同设置下的系统表现。同时,可以参考提供的文献资料,深入研究相关理论和技术背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值