地铁修建

来源:互联网 发布:王健林 知乎 编辑:程序博客网 时间:2024/04/25 03:17

用到了动态规划思想,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;}