Cod sursa(job #2252892)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 3 octombrie 2018 11:37:15
Problema Drumuri minime Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ifstream in("dmin.in");
ofstream out("dmin.out");
int viz[1505];
ll dist[1505];
vector< pair<int,int> > v[1505];

int main()
{
    int n,m;
    in >> n >> m;
    for (int i = 1; i<=m; i++)
    {
        int x,y,z;
        in >> x >> y >> z;
        v[x].push_back({y,z});
        v[y].push_back({x,z});
    }
    viz[1] = 1;
    queue<int> q;
    q.push(1);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        for (auto it: v[now])
        {
            int next = it.first, cost = it.second;
            if (!viz[next])
            {
                viz[next] = 1;
                dist[next] = dist[now]+cost;
                q.push(next);
            }
            else if (dist[next] > dist[now]+cost)
            {
                viz[next] = 1;
                dist[next] = dist[now]+cost;
                q.push(next);
            }
            else if (dist[next] == dist[now]+cost){
                viz[next]++;
                q.push(next);}
        }
    }
    for (int i = 2; i<=n; i++)
        out << viz[i] << " ";
}