P1948 Telephone Lines S (双端队列 bfs + 二分

#include "bits/stdc++.h"
using namespace std;
using PII = pair;
using VI = vector;
int n,p,k;
int dis[1010];
int vis[1010];
VI g[1010];
VI gv[1010];
int check(int len){
    memset(vis,0,sizeof vis);
    memset(dis,0x3f,sizeof dis);
    deque dq;
    dis[1] = 0;
    dq.push_back(1);
    while (dq.size()){
        auto x = dq.front();
        dq.pop_front();
        //cout< len) w = 1;
            //cout< dis[x] + w){
                dis[to] = dis[x] + w;
                if(w) dq.push_back(to);
                else dq.push_front(to);
            }

        }
    }
    if(dis[n] == 0x3f3f3f3f){
        cout<<-1;
        exit(0);
    }
    {
        if(dis[n] > k) return 0;
        else return 1;
    }
}


int main(){
    cin>>n>>p>>k;
    int t = 0;
    for(int i=1;i<=p;i++){
        int a,b,l;
        cin>>a>>b>>l;
        t = max(t,l);
        g[a].push_back(b);
        gv[a].push_back(l);
        g[b].push_back(a);
        gv[b].push_back(l);
    }
    int left = 0 ;
    int right = t;
    while (left < right){
        int mid = (left + right) >> 1;
        if(check(mid)){
            right = mid;
        }else{
            left = mid + 1;
        }
    }
    cout<

大概应该是会写的吧

你可能感兴趣的:(搜索,图,算法,图论,数据结构)