Cod sursa(job #1878820)

Utilizator floreaadrianFlorea Adrian Paul floreaadrian Data 14 februarie 2017 14:53:07
Problema Sate Scor 100
Compilator cpp Status done
Runda becreative15 Marime 0.88 kb
#include <fstream>
#include <queue>
#include <vector>
 
using namespace std;
const int MAXN = 30005;
 
int N, M, X, Y;
vector <pair<int, int> > g[MAXN];
 
ifstream fin("sate.in");
ofstream fout("sate.out");
 
int main() {
    fin >> N >> M >> X >> Y;
    while(M --) {
        int x, y, z;
        fin >> x >> y >> z;
        g[x].push_back(make_pair(y, z));
        g[y].push_back(make_pair(x, -z));
    }
    vector <int> dp(N + 1, 1 << 30);
    dp[X] = 0;
    queue <int> q;
    q.push(X);
    while(!q.empty()) {
        int nr,node = q.front();
        nr=g[node].size();
        for(int i=0;i<nr;i++) {
            if(dp[g[node][i].first] > dp[node] + g[node][i].second) {
                dp[g[node][i].first] = dp[node] + g[node][i].second;
                q.push(g[node][i].first);
            }
        }
        q.pop();
    }
    fout << dp[Y] << '\n';
}