Pagini recente » Cod sursa (job #384607) | Cod sursa (job #193337) | Cod sursa (job #2680174) | Cod sursa (job #2614844) | Cod sursa (job #3182844)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
vector<pair<int,int>> graph[100001];
int dist[100001];
bool vis[100001];
int main()
{
int n,m,first,last;
fin >> n >> m >> first >> last;
if(first > last)
{
swap(first,last);
}
while(m--)
{
int x,y,z;
fin >> x >> y >> z;
if(x > y)
{
swap(x,y);
}
graph[x].push_back({y,z});
graph[y].push_back({x,-z});
}
vis[first]=1;
queue<int> Q;
Q.push(first);
while(!Q.empty())
{
int node = Q.front();
Q.pop();
for(auto i : graph[node])
{
if(!vis[i.first])
{
vis[i.first]=1;
dist[i.first]=dist[node]+i.second;
Q.push(i.first);
}
}
}
fout << dist[y];
}