Cod sursa(job #1765756)

Utilizator mariakKapros Maria mariak Data 26 septembrie 2016 22:55:00
Problema Sate Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <bits/stdc++.h>
#define Nmax 30002
#define LL long long
#define pb(x) push_back(x)
#define pii pair <int, int>
#define f first
#define s second

FILE *fin  = freopen("sate.in", "r", stdin);
FILE *fout = freopen("sate.out", "w", stdout);

using namespace std;
int N, M, X, Y;
LL Max(LL x, LL y)
{
    if(x > y)
        return x;
    return y;
}
LL Min(LL x, LL y)
{
    if(x < y)
        return x;
    return y;
}
LL D[Nmax];
vector <pii> G[Nmax];
bitset <Nmax> vis;
void read()
{
    int x, y, d;
    scanf("%d %d %d %d", &N, &M, &X, &Y);
    while(M --)
    {
        scanf("%d %d %d", &x, &y, &d);
        G[x].pb(pii(y, d));
        G[y].pb(pii(x, d));
    }
}
void DFS(int x)
{
    vis.set(x);
    for(int i = 0; i < G[x].size(); ++ i)
    {
        int y = G[x][i].f;
        int c = G[x][i].s;
        if(!vis.test(y))
        {
            if(y > x)
                D[y] = 1LL * (D[x] + c);
            else D[y] = 1LL * (D[x] - c);
            DFS(y);
        }
    }
}
void write()
{
    printf("%lld\n", Max(D[X], D[Y]) - Min(D[X], D[Y]));
}
int main()
{
    read();
    DFS(1);
    write();
    return 0;
}