Write a program below |
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INF = 1000000000;
int main() {
int n, m ,s, f;
cin >> n>>m>>s>>f;
vector < vector < pair<int, int> > > g(n+1);
for (int i = 0; i < m; i++)
{
int x, y, z;
cin >> x >> y >> z;
g[x].push_back(make_pair(y, z));
}
vector<int> d(n+1, INF);
d[s] = 0;
priority_queue < pair<int, int> > q;
q.push(make_pair(0, s));
while (!q.empty()) {
int v = q.top().second, cur_d = -q.top().first;
q.pop();
if (cur_d > d[v]) continue;
for (size_t j = 0; j < g[v].size(); ++j) {
int to = g[v][j].first,
len = g[v][j].second;
if (d[v] + len < d[to]) {
d[to] = d[v] + len;
q.push(make_pair(-d[to], to));
}
}
}
|
|
}
|