Cod sursa(job #2027642)

Utilizator PondorastiAlex Turcanu Pondorasti Data 26 septembrie 2017 14:55:30
Problema Sate Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 30000;
int n, m, x, y, a, b, c, ans;
vector<pair<int, int>> g[NMAX + 5];
queue<pair<int, int>> q;
int Solve() {
    q.push(make_pair(x, 0));
    while (!q.empty()) {
        int node = q.front().first;
        int cost = q.front().second;
        q.pop();
        vector<pair<int, int>>::iterator it;
        for(it = g[node].begin(); it != g[node].end(); ++it) {
            pair<int, int> sat = *it;
            if(node > sat.first)
                sat.second = cost - sat.second;
            else
                sat.second = cost + sat.second;
            if(sat.first == y) {
                return sat.second;
            } else {
                q.push(sat);
            }
        }
    }
    return 0;
}
int main() {
    ifstream cin("sate.in");
    ofstream cout("sate.out");
    cin >> n >> m >> x >> y;
    for(int i = 1; i <= m; ++i) {
        cin >> a >> b >> c;
        g[a].push_back(make_pair(b, c));
        g[b].push_back(make_pair(a, c));
    }
    cout << Solve() << "\n";
    return 0;
}