Cod sursa(job #2357898)

Utilizator crion1999Anitei cristi crion1999 Data 27 februarie 2019 19:51:29
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda simulare_oji_cls12_nr6 Marime 1.79 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

#define NMAX 2005
using namespace std;

ifstream fi("ubuntzei.in");
ofstream fo("ubuntzei.out");

struct Edge
{
    int node;
    int cost;
    int city;
    Edge(int aNode, int aCost)
    {
        node = aNode;
        cost = aCost;
    }
    Edge(int aNode, int aCost, int aCity)
    {
        node = aNode;
        cost = aCost;
        city = aCity;
    }

    bool operator <(const Edge & other) const
    {
        if(cost > other.cost)
            return true;
        else if(cost < other.cost)
            return false;
        else
            return city < other.city;
    }
};

int N, M, K;

int dist[NMAX][NMAX];


bool locations[NMAX];

vector<Edge> graph[NMAX];

priority_queue<Edge> q;



int main()
{
    int a, b, c;
    fi >> N >> M;
    fi >> K;
    for(int i = 1; i <= K; ++i)
    {
        fi >> a;
        locations[a] = 1;
    }
    for(int i = 1; i <= M; ++i)
    {
        fi >> a >> b >> c;
        graph[a].push_back(Edge(b, c));
        graph[b].push_back(Edge(a, c));
    }

    for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= N; ++j)
            dist[i][j] = 0x3f3f3f3f;

    q.push(Edge(1, 0, 0));

    dist[1][0]= 0;

    while(!q.empty() && (q.top().node != N || q.top().city != K))
    {
        Edge e = q.top();
        q.pop();

        for(auto y : graph[e.node])
        {
            if(dist[y.node][e.city + locations[y.node]] >  y.cost + e.cost)
            {
                dist[y.node][e.city + locations[y.node]] = y.cost + e.cost;
                q.push(Edge(y.node, dist[y.node][e.city + locations[y.node]], e.city + locations[y.node]));
            }
        }
    }

    fo << q.top().cost;



}