Pagini recente » Cod sursa (job #253321) | Cod sursa (job #2624851) | Cod sursa (job #2230436) | Cod sursa (job #1328448) | Cod sursa (job #1470398)
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int Nmax = 30005;
const int INF = 1e9;
struct conexion{
int go, len;
};
int g[Nmax];
deque < int > stak;
vector < conexion > G[Nmax];
void bfs(){
int node;
while(!stak.empty()){
node = stak.front();
stak.pop_front();
for(int i = 0; i < G[node].size(); i++){
if(G[node][i].len + g[node] < g[G[node][i].go]){
g[G[node][i].go] = G[node][i].len + g[node];
stak.push_back(G[node][i].go);
}
}
}
}
int main(){
int n, m, x, y, a, b, d;
conexion A;
fin >> n >> m >> x >> y;
for(int i = 1; i <= m; i++){
fin >> a >> b >> d;
A.go = b; A.len = d;
G[a].push_back(A);
A.go = a; A.len = -d;
G[b].push_back(A);
}
for(int i = 1; i <= n; i++){
g[i] = INF;
}
g[x] = 0;
stak.push_back(x);
bfs();
fout << g[y];
return 0;
}