Pagini recente » Cod sursa (job #713900) | Cod sursa (job #963847) | Cod sursa (job #170599) | Cod sursa (job #995057) | Cod sursa (job #957145)
Cod sursa(job #957145)
#include <fstream>
#include <list>
#include <vector>
#include <queue>
#define MAX 300002
#define fin "sate.in"
#define fout "sate.out"
using namespace std;
typedef pair<int,int> node;
int N,M,S,D;
list<node> edges[MAX];
vector<int> dist( MAX, 0 );
queue<int> q;
vector<bool> visited(MAX,0);
int main(){
int x, y, cost;
ifstream in(fin);
ofstream out(fout);
in >> N >> M >> S >> D;
for( int i = 1; i <= M; i++ ){
in >> x >> y >> cost;
edges[x].push_back( node(y, cost));
edges[y].push_back( node(x, -cost));
}
visited[S] = 1;
q.push(S);
while( !q.empty() && !visited[D] ){
int u = q.front();
q.pop();
for( auto it = edges[u].begin(); it != edges[u].end(); it++ ){
pair<int,int> nod = *it;
if( visited[ nod.first] == 0 ){
visited[nod.first] = 1;
dist[nod.first] = dist[u] + nod.second;
q.push(nod.first);
}
}
if( dist[D] != 0 )
break;
}
out<< dist[D];
in.close();
out.close();
return 0;
}