Pagini recente » Cod sursa (job #3227767) | Cod sursa (job #1792111) | Cod sursa (job #2048165) | Cod sursa (job #907779) | Cod sursa (job #3182843)
#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);
if(i.first==last)
{
fout << dist[i.first];
return 0;
}
}
}
}
}