Cod sursa(job #1115973)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 22 februarie 2014 11:39:33
Problema Sate Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<fstream>
#include<vector>

using namespace std;

ifstream fin( "sate.in" );
ofstream fout( "sate.out" );

struct muchie { int nod, cost; };

const int nmax = 30000;
int l[nmax+1];
vector <muchie> v[nmax+1];

inline muchie str_ret ( int b, int c ) {
    muchie sol;
    sol.nod = b;
    sol.cost = c;
    return sol;
}
inline int abs( int k ) {
    if ( k < 0 )
        return 0-k;
    else
        return k;
}
void solve( int n ) {
    for ( int i = 0; i < (int)v[n].size(); ++ i ) {
        if ( l[ v[n][i].nod ] == 0 ) {
            if ( v[n][i].nod > n ) {
                l[ v[n][i].nod ] = l[n] + v[n][i].cost;
            } else {
                l[ v[n][i].nod ] = l[n] - v[n][i].cost;
            }
            solve( v[n][i].nod );
        }
    }
}
int main()
{
    int n, m, x, y, a, b, c;
    fin>>n>>m>>x>>y;
    for ( int i = 0; i < m; ++ i ) {
        fin>>a>>b>>c;
        v[a].push_back( str_ret( b, c ) );
        v[b].push_back( str_ret( a, c ) );
    }
    l[1] = 1;
    solve( 1 );
    fout<<abs(l[y]-l[x])<<'\n';
}