# 洛谷P1948 [USACO08JAN]电话线Telephone Lines

## 题目描述

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.

## 输入输出样例

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

4分析:一开始看错题目了，以为要求最小的总长度，打了个分层图最短路发现总是WA，后来才看到要求路径上的最大值，同时要求这个最大值最小，那不就是二分嘛，二分答案判断可行性.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

const int maxn = 1010,maxm = 20010,inf = 0x7ffffff;

int n,p,k,l,r;

{
w[tot] = z;
to[tot] = y;
}

int spfa()
{
queue <int> q;
memset(vis,0,sizeof(vis));
for (int i = 1; i <= n; i++)
d[i] = inf;
d[1] = 0;
vis[1] = 1;
q.push(1);
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (d[v] > d[u] + ww[i])
{
d[v] = d[u] + ww[i];
if (!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
return d[n];
}

bool check(int x)
{
for (int i = 1; i <= n; i++)
for (int j = head[i];j;j = nextt[j])
{
if (w[j] > x)
ww[j] = 1;
else
ww[j] = 0;
}
if (spfa() > k)
return false;
return true;
}

int main()
{
scanf("%d%d%d",&n,&p,&k);
for (int i = 1; i <= p; i++)
{
int a,b,l;
scanf("%d%d%d",&a,&b,&l);
r = max(r,l);
}
l = 1;
while (l <= r)
{
int mid = (l + r) >> 1;
if (check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
if (!ans)
printf("-1\n");
else
printf("%d\n",ans);

return 0;
}


posted @ 2017-09-13 16:23 zbtrs 阅读(...) 评论(...) 编辑 收藏