Cod sursa(job #1163240)

Utilizator nimicLeoveanu Mihaita Alexandru nimic Data 1 aprilie 2014 11:36:03
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<cstdio>
#include<vector>
#include<queue>
#include<fstream>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");

struct muchie { int nod, cost; };

const int nmax = 30009;
vector <muchie> v[nmax];
int n, m, x, y; 
int vdist[nmax];
bool poz[nmax];

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;
    poz[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 ( poz[ v[k][i].nod ] == 0 ) {
				poz[ v[k][i].nod ] = 1;
				vdist[ v[k][i].nod ] = vdist[k] + v[k][i].cost;
				q.push( v[k][i].nod );
            }
        }
    }
}

int main(){

	in>>n>>m>>x>>y;
	for(int i = 0; i<m; ++i)
	{
		int a, b, cost;
		in>>a>>b>>cost;

		v[a].push_back( str_ret( b, cost ) );
        v[b].push_back( str_ret( a, -cost ) );
	}

	bfs(x);

	out<<vdist[y];
	return 0;
}