1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <bits/stdc++.h> using namespace std;
const int MAXN = 1000; const int INF = INT_MAX;
struct edge { int to, length; edge(int to, int length) : to(to), length(length) {} };
struct point { int num, dis; point(int num, int dis) : num(num), dis(dis) {} bool operator<(const point &x) const { return dis > x.dis; } };
vector<edge> graph[MAXN]; int dis[MAXN];
void dijkstra(int s) { priority_queue<point> q; q.push({s, 0}); while (!q.empty()) { int u = q.top().num; q.pop(); for (int i = 0; i < graph[u].size(); ++i) { int v = graph[u][i].to, d = graph[u][i].length; if (dis[v] > dis[u] + d) { dis[v] = dis[u] + d; q.push({v, dis[v]}); } } } }
int main() { int n, m; cin >> n >> m; memset(graph, 0, sizeof(graph)); fill(dis, dis + n, INF); while (m--) { int from, to, length; cin >> from >> to >> length; graph[from].push_back({to, length}); graph[to].push_back({from, length}); }
int s, t; cin >> s >> t; if (dis[t] == INF) dis[t] = -1; cout << dis[t] << "\n"; return 0; }
|