POJ - 3662 Telephone Lines(二分+spfa最短路)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8182 | Accepted: 2950 |
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 {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole Nis 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
Source
这道题就是给了你n点p条路线~~让你求怎样建造才能使你的路中第k大的边的长度最小~~;
这样的话~求起来目的性就好办多了~我们可以让在建图的过程中让大于k的长度的边权为1~其他为0~这样跑一边spfa~~如果d[n]<=k就代表所求的k要比如今的要小~~如果d[n]==inf就代表无法形成回路~退出就好了;
操作想好了~~那怎么找k呢~~如果一个个遍历的话绝壁会超时~所有我们用挂壁之二分法就好了~;
#include<cstdio>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int n, p, k;
struct ***
{
int len, to, ne;
}ed[50000];
struct ***er
{
int from, to, len;
}e[50000];
int cnt;
int head[2000];
int d[2000];
int vis[2000];
void init()
{
memset(head, -1, sizeof(head));
cnt = 0;
}
void add(int from, int to, int len)
{
ed[cnt].len = len;
ed[cnt].to = to;
ed[cnt].ne = head[from];
head[from] = cnt++;
ed[cnt].len = len;
ed[cnt].to = from;
ed[cnt].ne = head[to];
head[to] = cnt++;
}
void build(int mid)
{
init();
for (int s = 0; s < p; s++)
{
if (mid < e[s].len)
{
add(e[s].from,e[s].to,1);
}
else
{
add(e[s].from, e[s].to, 0);
}
}
}
void spfa()
{
memset(vis, 0, sizeof(vis));
memset(d, inf, sizeof(d));
queue<int>q;
q.push(1);
vis[1] = 1;
d[1] = 0;
while (!q.empty())
{
int t = q.front();
q.pop();
vis[t] = 0;
for (int s = head[t]; ~s; s = ed[s].ne)
{
// cout << s << endl;
if (d[ed[s].to] > d[t] + ed[s].len)
{
d[ed[s].to] = d[t] + ed[s].len;
if (!vis[ed[s].to])
{
vis[ed[s].to] = 1;
q.push(ed[s].to);
}
}
}
}
}
int main()
{
scanf("%d%d%d", &n, &p, &k);
int maxn = -1;
for (int s = 0; s < p; s++)
{
scanf("%d%d%d", &e[s].from, &e[s].to, &e[s].len);
maxn = max(maxn, e[s].len);
}
int li = 0, ri = maxn;
while (li < ri)
{
int mid = (li + ri) / 2;
build(mid);
spfa();
if (d[n] == inf)
{
li= -1;
break;
}
if (d[n] <= k)
{
ri = mid;
}
else
{
li = mid + 1;
}
}
cout << li<< endl;
return 0;
}