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