36求条
查看原帖
36求条
1438293
LiuStar2233楼主2025/1/18 14:24
// P4568 [JLOI2011] 飞行路线
#include <bits/stdc++.h>
#define MAXN (int)10e4*5+2
using namespace std;

struct Edge { int to, cost; };
vector<Edge> adj[MAXN];
int n, m, k, s, t;
int dis[MAXN];
bool vis[MAXN];

void Dijkstra() {
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
	fill(dis, dis + MAXN, INT_MAX);
	dis[s] = 0;
	pq.push({0, s});

	while (!pq.empty()) {
		int u = pq.top().second;
		pq.pop();
		if (vis[u]) continue;
		vis[u] = true;

		for (auto &edge : adj[u]) {
			int v = edge.to;
			int cost = edge.cost;
			if (dis[u] + cost < dis[v]) {
				dis[v] = dis[u] + cost;
				pq.push({dis[v], v});
			}
		}
	}
}

int main() {
	cin >> n >> m >> k >> s >> t;
	for (int i = 0; i < m; i++) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		adj[a].push_back({b, c});
		adj[b].push_back({a, c});
	}
	Dijkstra();
	cout << dis[t];

	return 0;
}

2025/1/18 14:24
加载中...