Pagini recente » Cod sursa (job #1351066) | Cod sursa (job #2063963) | Cod sursa (job #2918987) | Cod sursa (job #2303303) | Cod sursa (job #3272451)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y, sol, d_x, d_y;
vector< pair<int, int> > G[30005];
queue< pair<int, int> > q;
bool viz[30005];
void Dfs(int x)
{
viz[x] = 1;
for(auto e : G[x])
if(viz[e.first] == 0)
Dfs(e.first);
}
void Bfs(int a)
{
int nod, dist, cost;
q.push({a, 0});
viz[a] = 1;
if(a == x) d_x = 0;
while(!q.empty())
{
auto f = q.front();
q.pop();
a = f.first;
dist = f.second;
if(viz[x] == 1 && viz[y] == 1)
{
fout << d_y - d_x << "\n";
fout.close();
exit(0);
}
for(auto e : G[a])
{
nod = e.first;
cost = e.second;
if(viz[nod] == 0)
{
if(nod < a)
{
q.push({nod, dist - cost});
if(nod == x)
d_x = dist - cost;
if(nod == y)
d_y = dist - cost;
viz[nod] = 1;
}
else{
q.push({nod, dist + cost});
if(nod == x)
d_x = dist + cost;
if(nod == y)
d_y = dist + cost;
viz[nod] = 1;
}
}
}
}
}
int main()
{
int i, j, c;
fin >> n >> m >> x >> y;
while(m)
{
fin >> i >> j >> c;
G[i].push_back({j, c});
G[j].push_back({i, c});
m--;
}
Dfs(x);
for(i = 1;i <= n && viz[i] == 0;i++)
;
for(j = 1;j <= n;j++)
viz[j] = 0;
Bfs(i);
return 0;
}