Cod sursa(job #1816050)

Utilizator c0mradec0mrade c0mrade Data 26 noiembrie 2016 01:22:09
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("transport2.in");
ofstream fout("transport2.out");

struct cmp{
   bool operator()(pair<int,int> a, pair<int,int> b){
        return a.first < b.second;
   }
};

int n, m, wmax[100100];
vector<pair<int, int>> G[100100];
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;

void dijkstra()
{
    pq.push({10001, 1});
    while(!pq.empty())
    {
        int w = pq.top().first;
        int x = pq.top().second;
        pq.pop();

        for(int i=0; i<G[x].size(); ++i)
            if(wmax[G[x][i].first] == 0)
            {
                wmax[G[x][i].first] = G[x][i].second;
                pq.push({wmax[G[x][i].first], G[x][i]. first});
            }
            else if(wmax[G[x][i].first] < min(wmax[x], G[x][i].second))
            {
                wmax[G[x][i].first] = min(wmax[x], G[x][i].second);
                pq.push({wmax[G[x][i].first], G[x][i]. first});
            }
    }
}

int main()
{
    fin >> n >> m;
    for(int i=0; i<m; ++i)
    {
        int x, y, w;
        fin >> x >> y >> w;
        G[x].push_back({y, w});
        G[y].push_back({x, w});
    }

    dijkstra();

    fout << wmax[n];

    return 0;
}