Pagini recente » Cod sursa (job #972114) | Cod sursa (job #612484) | Cod sursa (job #2357362) | Cod sursa (job #316050) | Cod sursa (job #1655530)
#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
ifstream fin ("sate.in");
ofstream fout ("sate.out");
const int Nmax = 30444;
int d[Nmax];
char vis[Nmax];
vector<pii> G[Nmax];
int main() {
int N, M, x, y, a, b, c;
fin >> N >> M >> x >> y;
--x, --y;
if (x > y) swap(x, y);
while(M--) {
fin >> a >> b >> c;
--a, --b;
if (a > b) swap(a, b);
G[a].push_back({b, c});
G[b].push_back({a, -c});
}
//memset(d, -1, sizeof(int)*(N+1));
vis[x] = 1;
d[x] = 0;
queue<int> Q;
Q.push(x);
while(!Q.empty()) {
a = Q.front(); Q.pop();
for(auto P: G[a]) {
if (vis[P.first])
continue;
d[P.first] = d[a] + P.second;
Q.push(P.first);
vis[P.first] = 1;
}
}
fout << d[y] << endl;
return 0;
}