Pagini recente » Cod sursa (job #3175841) | Cod sursa (job #2406719) | Cod sursa (job #401693) | Cod sursa (job #1668642) | Cod sursa (job #3219496)
#include <iostream>
#include <fstream>
#include <queue>
#define INF 1000000
#define NMAX 30000
#define MMAX 100024
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
vector<pair<int,int>> graph[NMAX+1];
int n,m,x,y;
bool vis[NMAX+1];
int dist[NMAX+1];
int main()
{
fin >> n >> m >> x >> y;
if(x > y)
{
swap(x,y);
}
while(m--)
{
int from,to,d;
fin >> from >> to >> d;
if(from > to)
{
swap(from,to);
}
graph[from].push_back({to,d});
graph[to].push_back({from,-d});
}
queue<int> Q;
Q.push(x);
vis[x]=1;
dist[x]=0;
int res=0;
while(!Q.empty())
{
int from = Q.front();
Q.pop();
for(auto i : graph[from])
{
int to = i.first, c = i.second;
if(!vis[to])
{
vis[to]=1;
dist[to] = dist[from] + c;
if(to==y)
{
fout << dist[y];
return 0;
}
Q.push(to);
}
}
}
}