POJ3662 Telephone Lines (dijkstra+二分)

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
复制代码
复制代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
int n,p,k;
int d[1005];
int head[1005],ver[20005],edge[20005],Next[20005];//开两倍存储双向边 
bool v[1005];
int tot=0;
priority_queue<pair<int,int> >q;
void add(int x,int y,int z)
{
    ver[++tot]=y;
    edge[tot]=z;
    Next[tot]=head[x];
    head[x]=tot;
}
int dijkstra(int mid)//花费小于等于mid记为0 否则记为1 //只需要为一条路买单 //dij返回的是大于mid的数 
{
    int cnt=0;
    memset(d,0x3f,sizeof(d));
    memset(v,0,sizeof(v));
    d[1]=0;
    q.push(make_pair(0,1));
    int i,j;
    while(q.size())
    {
        int x=q.top().second;
        q.pop();
        if(v[x])continue;
        v[x]=1;
        for(i=head[x];i;i=Next[i])
        {
            int y=ver[i],z=edge[i];
            int z1;
            if(z<=mid)z1=0;
            else z1=1;
            if(d[y]>d[x]+z1)// 注意 这里要求的最短路并不是原来费用的最短路 
            {
                d[y]=d[x]+z1;
                q.push(make_pair(-d[y],y));
            }
        }
    }
    return d[n];    
}
bool check(long long mid)
{
    if(dijkstra(mid)<=k)
    {
        return true;
    }
    else return false;
}
int main()
{
    int i;
    memset(v,0,sizeof(v));
    scanf("%d%d%d",&n,&p,&k);
    for(i=1;i<=p;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    long long l=0,r=1000002,mid;
    while(l<r)
    {
        mid=(l+r)>>1;
        if(check(mid))//钱数够了缩小 
        {
            r=mid;
        }
        else//钱数不够扩大 
        {
            l=mid+1;
        }
    }
    if(r>1000000)cout<<-1<<endl;
    else if(r<0)cout<<0<<endl; 
    else cout<<l<<endl;
    return 0;
}
复制代码

 

复制代码

 

posted @   脂环  阅读(203)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
主题色彩