Cod sursa(job #1107005)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 13 februarie 2014 16:01:30
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <memory.h>

#define nn second
#define cc first

using namespace std;

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

const int NMAX = 1e4 + 100;
const int WMAX = 1e6 + 100;
const int oo = 0x3f3f3f3f;

int N, M, W, T[WMAX], D[NMAX];
typedef pair <int, int> node;
set <node> G[NMAX];
priority_queue <node, vector <node>, greater<node> > H;
vector <bool> INQ(NMAX);

int Dijkstra()
{
    memset(D, oo, sizeof D);
    D[1] = 0; H.push(node(0, 1));
    INQ[1] = 1;

    while(!H.empty())
    {
        int nod = H.top().nn, newcost; H.pop();
        if(nod == N)
            return D[N];
        INQ[nod] = 0;
        set <node> :: iterator it = G[nod].begin();

        for(; it != G[nod].end(); it++)
        {
            node newn = *it;
            for(newcost = D[nod] + 1; newcost <= W + 1 && newn.cc > T[newcost]; newcost++);
            if(newcost != W + 1 && D[newn.nn] > newcost)
            {
                D[newn.nn] = newcost;
                if(!INQ[newn.nn]) INQ[newn.nn] = 1, H.push(node(D[newn.nn], newn.nn));
            }
        }
    }
}

int main()
{
    fin >> N >> M;
    for(int i = 1; i <= M; i++)
    {
        int x, y, z;
        fin >> x >> y >> z;
        G[x].insert(node(z, y));
        G[y].insert(node(z, x));
    }

    fin >> W;
    for(int i = 1; i <= W; i++)
        fin >> T[i];

    int rez = Dijkstra();
    if(rez != oo)
        fout<<rez;
    else
        fout<<"-1";
    return 0;
}