堆优化dijkstra, 一个点都没过
  • 板块P1359 租用游艇
  • 楼主zzt244
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/30 11:38
  • 上次更新2024/3/30 14:29:24
查看原帖
堆优化dijkstra, 一个点都没过
1163432
zzt244楼主2024/3/30 11:38
#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 205;

int n;
vector<PII> p[N];

int dijkstra() {
	vector<int> dist(n, 0x3f3f3f3f);
	dist[1] = 0;
	priority_queue<PII, vector<PII>, greater<PII>> q;
	q.emplace(0, 1);
	while (!q.empty()) {
		int ver = q.top().second, dis = q.top().first;
		q.pop();

		if (dis > dist[ver]) continue;

		for (auto x : p[ver]) {
			int u = x.first;
			int w = x.second;
			if (dist[u] > dist[ver] + w) {
				dist[u] = dist[ver] + w;
				q.emplace(dist[u], u);
			}
		}
	}
	return dist[n];
}

void solve() {
	cin >> n;
	for (int i = 1; i <= n; i ++ ) {
		for (int j = i + 1; j <= n; j ++ ) {
			int x;
			cin >> x;
			p[i].push_back({j, x});
		}
	}
	cout << dijkstra() << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int t = 1;
	//cin >> t;
	while (t -- ) {
		solve();
	}
	return 0;
}
2024/3/30 11:38
加载中...