Pagini recente » Solutii Autumn Warmup, Runda 2 | Cod sursa (job #240379) | Cod sursa (job #324152) | Cod sursa (job #888112) | Cod sursa (job #2717182)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 30000+5
#define oo 2e9
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
struct NodeStruct {
int node;
long long cost;
bool operator < (const NodeStruct other) const {
return cost > other.cost;
}
};
int n, m, x, y, a, b, c;
long long visited[NMAX];
vector <NodeStruct> G[NMAX];
priority_queue <NodeStruct> pq;
long long BFS(int start, int stop)
{
for(int i = 1; i <= n; ++ i)
visited[i] = oo;
pq.push({start, 0});
while(!pq.empty()) {
int cnode = pq.top().node;
int ccost = pq.top().cost;
pq.pop();
if(visited[cnode] == oo)
visited[cnode] = ccost;
for(auto it : G[cnode]) {
if(visited[it.node] == oo)
if(it.node > cnode)
pq.push({it.node, ccost + it.cost});
else
pq.push({it.node, ccost - it.cost});
}
}
return visited[stop];
}
int main()
{
fin >> n >> m >> x >> y;
for(int i = 1; i <= m; i++) {
fin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
BFS(x, y);
fout << visited[y] << '\n';
return 0;
}