Pagini recente » Cod sursa (job #713797) | Cod sursa (job #202024) | Cod sursa (job #1510772) | Cod sursa (job #709892) | Cod sursa (job #2855040)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 3e4;
const int INF = 2e9;
ifstream fin( "sate.in" );
ofstream fout( "sate.out" );
struct Edge{
int node, cost;
};
vector <Edge> graph[NMAX];
queue <int> bfsQueue;
int dist[NMAX];
bool marked[NMAX];
int n;
void bfs( int node ) {
dist[node] = 0;
bfsQueue.push( node );
marked[node] = true;
while( !bfsQueue.empty() ) {
int qfront = bfsQueue.front();
bfsQueue.pop();
for( auto edge: graph[qfront] ) {
if( !marked[edge.node] ) {
bfsQueue.push( edge.node );
marked[edge.node] = true;
dist[edge.node] = dist[qfront] + edge.cost;
}
}
}
}
int main() {
int m, x, y, i, a, b, d;
fin >> n >> m >> x >> y;
x--;
y--;
for( i = 0; i < m; i++ ) {
fin >> a >> b >> d;
a--;
b--;
graph[a].push_back( { b, d } );
graph[b].push_back( { a, -d } );
}
bfs( x );
fout << dist[y];
return 0;
}