(poj 3662) Telephone Lines 最短路+二分

题目链接:http://poj.org/problem?id=3662

Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8248   Accepted: 2977

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

 
题目大意:找到一条从1 到n的最短路,使这条路的第(K+1)大的边最小。
思路:二分答案,对于每个mid进行最短路,对于大于mid的边变成1,小于等于mid的变成0,如果最短路的值小于等于k,则这个mid满足条件,找到最小的mid。
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include <cmath>
#include<vector>
using namespace std;
#define LL long long
#define N 1010
#define mod 1000000007
#define INF 0x3f3f3f3f
struct node
{
int u,v,c,next,w; }s[N*];
int head[N],k = ,mi,n;
int dis[N],vis[N];
void add(int u,int v,int c)
{
s[k].u = u;
s[k].v = v;
s[k].c = c;
s[k].next = head[u];
head[u] = k++;
} int spfa(int u)
{
queue<int>que;
que.push(u);
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
vis[u] = ;
dis[u] = ;
while(que.size())
{
int x = que.front();
q.pop();
vis[x] = ;
for(int i = head[x];i != -;i = s[i].next)
{
int v = s[i].v;
if(dis[v]>dis[x]+s[i].w)
{
dis[v] = dis[x]+s[i].w;
if(!vis[v])
{
vis[v] = ;
que.push(v);
}
}
}
}
return dis[n] <= mi;
} int ok(int m)
{
for(int i=;i<=n;i++)
{
for(int j = head[i];j != -;j = s[j].next)
{
if(s[j].c<=m)
s[j].w = ;
else s[j].w = ;
}
}
return spfa();
} int main()
{
int m;
while(scanf("%d %d %d",&n, &m,&mi)!=EOF)
{
memset(head,-,sizeof(head));
k = ;
int u,v,c;
int r = ;
for(int i = ; i < m; i++)
{
scanf("%d %d %d",&u,&v,&c);
add(u,v,c);
add(v,u,c);
r = max(r,c);
}
int l = ,mid;
int ans = -;
while(l <= r)
{
mid = (l+r)/;
if(ok(mid))
{
ans = mid;
r = mid-;
}
else l = mid+;
}
printf("%d\n",ans);
}
}
上一篇:C#使用System.Data.SQLite操作SQLite


下一篇:Python IDE Spyder的简单介绍