Cod sursa(job #1145483)

Utilizator andreiagAndrei Galusca andreiag Data 18 martie 2014 11:07:51
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <string.h>
#include <vector>
#include <queue>

using namespace std;
const int Nmax = 30005;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;

int dist[Nmax];
vector<pii> G[Nmax];
queue<int> Q;

int main()
{
    ifstream f ("sate.in");
    ofstream g ("sate.out");

    int N, M, x, y, a, b, d;
    f >> N >> M >> x >> y;

    if (x > y) { int tmp = x; x = y; y = tmp; }

    memset(dist, INF, sizeof(dist));
    for (int m = 0; m < M; m++)
    {
        f >> a >> b >> d;
        G[a].push_back(make_pair(b, d));
        G[b].push_back(make_pair(a,-d));
    }

    dist[x] = 0;
    Q.push(x);

    while(!Q.empty())
    {
        a = Q.front(); Q.pop();
        if (a == y)
        {
            g << dist[a] << '\n';
            break;
        }

        for (auto P: G[a])
        {
            if (dist[P.first] == INF)
            {
                dist[P.first] = dist[a] + P.second;
                Q.push(P.first);
            }
        }
    }

    return 0;
}