Pagini recente » Cod sursa (job #397581) | Cod sursa (job #2573105) | Cod sursa (job #1912730) | Cod sursa (job #2976900) | Cod sursa (job #2812089)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAXN = 30001;
const int INF = 0x3f3f3f3f;
int n, m, x, y, a, b, c;
int dp[MAXN], viz[MAXN];
vector<pair<int,int>> G[MAXN];
static inline void dfs(int x) {
viz[x] = 1;
for(auto node : G[x])
if(!viz[node.first]) {
if(node.first > x)
dp[node.first] = min(dp[node.first], dp[x] + node.second);
else
dp[node.first] = min(dp[node.first], dp[x] - node.second);
dfs(node.first);
}
}
int main() {
fin >> n >> m >> x >> y;
if(x > y) swap(x, y);
memset(dp, INF, sizeof(dp));
dp[x] = 0;
while(m--) {
fin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
if(a == x)
dp[b] = c;
}
dfs(x);
fout << dp[y] << '\n';
return 0;
}