Cod sursa(job #1378022)

Utilizator MarronMarron Marron Data 6 martie 2015 10:08:56
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <cstring>
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>


typedef std::vector< std::pair<int, int> >::iterator iter;


const int MAXN = 30005;
const int MAXM = 100030;
// <parsing>
const int MAXB = 8192;
FILE* f = fopen("sate.in", "r");
char buf[MAXB]; int ptr = MAXB;
int getInt()
{
    while (buf[ptr] < '0' || '9' < buf[ptr]) {
        if (++ptr >= MAXB) {
            fread(buf, MAXB, 1, f);
            ptr = 0;
        }
    }
    int nr = 0;
    while ('0' <= buf[ptr] && buf[ptr] <= '9') {
        nr = nr * 10 + buf[ptr] - '0';
        if (++ptr >= MAXB) {
            fread(buf, MAXB, 1, f);
            ptr = 0;
        }
    }
    return nr;
}
// </parsing>
std::ofstream g("sate.out");
std::vector< std::pair<int, int> > G[MAXN]; int n, m, X, Y;
std::priority_queue< std::pair<int, int> > pq;
std::bitset<MAXN> viz;
int dist[MAXN];


void dijkstra(int node)
{
    dist[node] = 0;

    pq.push(std::make_pair(0, node));
    while (!pq.empty()) {
        int nd = pq.top().second;
        pq.pop();


        if (viz[nd] == true) {
            continue;
        }
        viz[nd] = true;


        for (iter it = G[nd].begin(); it != G[nd].end(); it++) {
            if (viz[it->first] == true) {
                continue;
            }
            if (dist[it->first] > dist[nd] + it->second) {
                dist[it->first] = dist[nd] + it->second;
                pq.push(std::make_pair(-dist[it->first], it->first));
            }
        }
    }
}


int main()
{
    memset(dist, 0x3f, sizeof(dist));

    n = getInt(); m = getInt(); X = getInt(); Y = getInt();
    for (int i = 1, x, y, c; i <= m; i++) {
        x = getInt(); y = getInt(); c = getInt();
        G[x].push_back(std::make_pair(y, c));
        G[y].push_back(std::make_pair(x, -c));
    }


    dijkstra(X);
    g << dist[Y] << '\n';


    fclose(f);
    g.close();
    return 0;
}