Cod sursa(job #1116012)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 22 februarie 2014 11:54:31
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include<fstream>
#include<vector>
#include<queue>

using namespace std;

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

struct muchie { int nod, cost; };

const int nmax = 30000;

bool p[nmax+1];
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;
}
void bfs( int n ) {
    queue <int> q;
    p[n] = 1;
    q.push( n );

    while ( !q.empty() ) {
        int k = q.front();
        q.pop();
        for ( int i = 0; i < (int)v[k].size(); ++ i ) {
            if ( p[ v[k][i].nod ] == 0 ) {
                p[ v[k][i].nod ] = 1;
                l[ v[k][i].nod ] = l[k] + v[k][i].cost;
                q.push( v[k][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 ) );
    }
    bfs( x );
    fout<<l[y]<<'\n';
}