P1948 [USACO08JAN] Telephone Lines S

点击查看代码
#include<bits/stdc++.h>
using namespace std;

const int N=1e3+10,M=2e4+10;
typedef pair<int,int> PII;
int n,p,k;
int h[N],ne[M],w[M],idx,e[M];
int dist[N];
bool st[N];

void add(int a,int b,int l)
{
    e[idx]=b,ne[idx]=h[a],w[idx]=l,h[a]=idx++;
}

bool check(int limit)
{
    memset(dist,0x3f,sizeof dist);
    memset(st,0,sizeof st);
    dist[1]=0;
    priority_queue<PII,vector<PII>,greater<PII>> heap;
    heap.push({dist[1],1});

    while(!heap.empty()){
        auto t=heap.top();
        heap.pop();
        int ver=t.second;

        if(st[ver]) continue;
        st[ver]=true;

        for(int i=h[ver];i!=-1;i=ne[i]){
            int j=e[i];
            int cost=(w[i]>limit ? 1 : 0);

            if(dist[j]>dist[ver]+cost){
                dist[j]=dist[ver]+cost;
                heap.push({dist[j],j});
            }
        }
    }
    return dist[n]<=k;
}

int main()
{
    memset(h,-1,sizeof h);
    
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n>>p>>k;

    int max_fee=0;
    for(int i=1;i<=p;i++){
        int a,b,l;
        cin>>a>>b>>l;
        add(a,b,l),add(b,a,l);
        max_fee=max(max_fee,l);
    }

    int ans=-1;
    int l=0,r=max_fee;
    while(l<=r){
        int mid=l+(r-l)/2;
        if(check(mid)){
            r=mid-1;
            ans=mid;
        }else{
            l=mid+1;
        }
    }

    cout<<ans<<endl;
}

posted @ 2026-01-25 21:33  AnoSky  阅读(3)  评论(0)    收藏  举报