地铁修建

用到了动态规划思想,spfa

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

const int oo = 1e9;
const int MAXN = 1e5+5;
const int MAXM = 2*1e5 + 5;
int N,M;
bool inq[MAXN] = {0};
int opt[MAXN];

struct edge{
    int from;
    int to;
    int weight;
    edge(int _from, int _to, int _weight) :from(_from), to(_to), weight(_weight) {}

};

vector<edge> ve;
vector<int> v[MAXN];

void bfs(int s){
    queue<int> q;
    q.push(s);
    inq[s] = true;
    opt[s] = 0;
    for(;!q.empty();){
        int t =  q.front();
        q.pop();
        inq[t] = false;
        for(int i = 0; i < v[t].size();i++){
            int e = v[t][i];
            int next = ve[e].to;
            int m;
            if((m = max(opt[t],ve[e].weight))<opt[next]){
                opt[next] = m;
                if(!inq[next]){
                    q.push(next);
                    inq[next] = true;
                }
            }
        }
    }
}

int main(){
    scanf("%d%d",&N,&M);
    fill(opt+1,opt+N+1,oo);
    int f,t,w;
    for(int i = 0; i < M; i++){
        scanf("%d%d%d", &f, &t, &w);
        ve.push_back(edge(f,t,w));
        ve.push_back(edge(t,f,w));
        v[f].push_back(ve.size()-2);
        v[t].push_back(ve.size()-1);
    }
    bfs(1);
    cout << opt[N] << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值