Cod sursa(job #1086434)

Utilizator laurionLaurentiu Ion laurion Data 18 ianuarie 2014 02:54:28
Problema Sate Scor 0
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 2.34 kb
#define nume "sate"
//#define DBG

#include<cstdio>
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<list>
#include<algorithm>

using namespace std;
using namespace __gnu_cxx;

//#define _PARSARE_
#ifdef _PARSARE_
#define DIM 8192
char ax[DIM+16];
int _idx;
template<class T>
inline void cit(T& x)
{
    x=0;
    while((ax[_idx]<'0' || ax[_idx]>'9') && (ax[_idx]!='-'))
        if(++_idx==DIM)fread(ax, 1, DIM, stdin), _idx=0;
    
    int neg=0;
    if(ax[_idx]=='-') {
        neg=1;
        if(++_idx==DIM)fread(ax, 1, DIM, stdin),_idx=0;
    }
    
    while(ax[_idx]>='0' && ax[_idx]<='9') {
        x=x*10+ax[_idx]-'0';
        if(++_idx==DIM)fread(ax,1, DIM, stdin),_idx=0;
    }
    if(neg) x=-x;
}
#else
#ifndef ONLINE_JUDGE
ifstream fin (nume ".in");
#endif
#endif //_PARSARE_
#ifndef ONLINE_JUDGE
ofstream fout(nume ".out");
#endif
#ifdef DBG
#define fout cout
#endif
//// hash-uri
//#include<ext/hash_map>
//hash_multimap<int,int> H;
//hash_map<char*,int> H;
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
#define foreach(it, v) for (typeof((v).begin()) it = (v).begin(),stop=(v).end(); it != stop; ++it)
#define foreach_r(it, v) for (typeof((v).rbegin()) it = (v).rbegin(),stop=(v).rend(); it != stop; ++it)
template<class T> inline void Swap(T& a,T& b)
{
    a^=b,b^=a,a^=b;
}

int n,m,x,y;

const int MAXN = 30000 + 10;
vector<pair<int,int> > G[MAXN];
queue<int> Q;
bool in_queue[MAXN];
int d[MAXN];

inline void bf(int start)
{
    Q.push(start);
    while(!Q.empty()) {
        int nod=Q.front();
        Q.pop();
        foreach(it,G[nod]) {
            if(!in_queue[it->first]) {
                Q.push(it->first);
                in_queue[it->first]=true;
                d[it->first] = d[nod] + it->second;
            }
        }
    }
}
int main()
{
#ifdef _PARSARE_
#ifndef ONLINE_JUDGE
    freopen(nume ".in","r",stdin);
#endif
    cit(n);
#endif
    
    fin>>n>>m>>x>>y;
    
    for(int i=0; i<m; ++i) {
        int x,y,d;
        fin>>x>>y>>d;
        G[x].push_back(make_pair(y,d));
        G[y].push_back(make_pair(x,-d));
    }
    
    if(x>y)
        swap(x,y);
    
    bf(x);
    fout<<d[y]<<'\n';
    
#ifndef DBG
#ifndef ONLINE_JUDGE
    fout.close();
#endif
#endif
    return 0;
}