Cod sursa(job #2425383)

Utilizator malina99oanea ana malina malina99 Data 24 mai 2019 19:21:52
Problema Sate Scor 60
Compilator cpp-64 Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.72 kb
#include <fstream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;

ifstream f("sate.in");
ofstream g("sate.out");
#define nmax 30100

int dr[nmax], st[nmax], cost[nmax];
int n, m, x, y;

void read_data() {
    f >> n >> m >> x >> y;
    for( int i = 1; i <= m; i++ ) {
        int a , b, c;
        f >> st[i] >> dr[i] >> cost[i];
    }
}

int viz[nmax];

void solve() {
    viz[x] = 1;
    while( !viz[y] ) {
        for( int i = 1; i <= m; i++ )
            if( viz[ st[i] ] and !viz[dr[i]] ) viz[dr[i]] = viz[st[i]] + cost[i];
            else if( !viz[st[i]] and viz[ dr[i] ] ) viz[st[i]] =   viz[dr[i]] - cost[i] ;
    }
    g << viz[y] - 1;
}

int main() {
    read_data();
    solve();
    return 0;
}