Cod sursa(job #1336145)

Utilizator harababurelPuscas Sergiu harababurel Data 6 februarie 2015 19:58:00
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#define nmax 30005
#define inf (1<<30)
#define weight first
#define id second
using namespace std;


bool seen[nmax];
int n, m, x, y, w, source, target, best[nmax];
vector <pair <int, int>> v[nmax];

void explore(int x, int soFar) {
    seen[x] = true;

    for(auto y:v[x]) {
        if(best[y.id] == 0) best[y.id] = inf;
        best[y.id] = min(best[y.id], soFar + y.weight);

        if(!seen[y.id]) explore(y.id, best[y.id]);
    }
}

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

    f>>n>>m>>source>>target;
    for(int i=1; i<=m; i++) {
        f>>x>>y>>w;
        v[x].push_back(make_pair(w, y));
        v[y].push_back(make_pair(-w, x));
    }

    explore(source, 0);

    /*
    for(int i=1; i<=n; i++)
        cout<<best[i]<<" ";
    cout<<"\n";
    */
    g<<best[target]<<"\n";

    return 0;
}