Pagini recente » Profil pestcontrol206 | Cod sursa (job #3252169)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("sate.in");
ofstream fout("sate.out");
const int Max = 3e4 + 1;
const int Inf = 1 << 30;
vector<pair<int,int>> graph[Max];
vector<int> d(Max, Inf);
void bfs(int node)
{
queue<pair<int,int>> q;
q.push({0, node});
d[node] = 0;
while(!q.empty())
{
node = q.front().second;
q.pop();
for(auto i: graph[node])
{
int neighbour = i.first;
int cost = i.second;
if(d[node] + cost < d[neighbour])
{
d[neighbour] = d[node] + cost;
q.push({d[neighbour], neighbour});
}
}
}
}
int main()
{
int n, m, s, f, x, y, z;
fin >> n >> m >> s >> f;
for(int i = 1; i <= m; ++i)
{
fin >> x >> y >> z;
graph[x].push_back({y, z});
graph[y].push_back({x, -z});
}
bfs(s);
fout << d[f];
fin.close();
fout.close();
return 0;
}