Cod sursa(job #2041371)

Utilizator Andrei2000Andrei Mihailescu Andrei2000 Data 17 octombrie 2017 10:17:00
Problema Drumuri minime Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define nmax 1502
using namespace std;

ifstream fin ("dmin.in");
ofstream fout ("dmin.out");

priority_queue< pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > Q;
vector< pair<int,int> > G[nmax];
int n,m,d[nmax];

void dijkstra(int node){
    memset(d,inf,sizeof d);
    d[node]=0;
    Q.push({0,node});
    while(!Q.empty()){
        int dist=Q.top().first;
        int node_st=Q.top().second;
        if(d[node_st]<dist)continue;
        for(auto edge: G[node_st])
            if(d[edge.first]>d[node_st]+edge.second){
                d[edge.first]=d[node_st]+edge.second;
                Q.push({d[edge.first],edge.first});
            }
    }
}

int main()
{
    int a,b,c;
    fin>>n>>m;
    for(int i=1;i<=m;++i){
        fin>>a>>b>>c;
        G[a].push_back({b,31-__builtin_clz(c)});
        G[b].push_back({a,31-__builtin_clz(c)});
    }
    dijkstra(1);
    return 0;
}