Cod sursa(job #1789864)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 27 octombrie 2016 16:29:59
Problema Sate Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <cstdio>
#include <queue>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector <pair<int, int>> v[30005];
queue <int> q;
int dp[30005];
bool used[30005];

int main(){
    freopen("sate.in", "r", stdin);
    freopen("sate.out", "w", stdout);
    int n,m,i,j,x,y,s,f,d;
    scanf("%d %d %d %d", &n, &m, &s, &f);
    if(s > f){
        swap(s, f);
    }
    for(i = 1;i <= m;i++){
        scanf("%d %d %d", &x, &y, &d);
        v[x].push_back({y, d});
        v[y].push_back({x, d});
    }
    used[1] = 1;
    q.push(1);
    while(q.empty() == false){
        x = q.front();
        q.pop();
        for(auto it : v[x]){
            if(used[it.first] == 0){
                if(x > it.first){
                    dp[it.first] = dp[x] - it.second;
                }else{
                    dp[it.first] = dp[x] + it.second;
                }
                used[it.first] = 1;
                q.push(it.first);
            }
        }
    }
    printf("%d", dp[f] - dp[s]);
    return 0;
}