跳到正文

题解

AT_abc340_d 题解

Vitamin_B约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年2月11日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟

版本

  1. v1当前

dijkstra 板子题。这题每个点就连两个边,然后就没有然后了。

代码

C++
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const ll inf = 1e18;
int n, m, a, b, c;
ll d[200005];
vector <pii> v[200005];
struct node {
	ll dis;
	int u;
	bool operator < (const node& cmp) const {
		return dis > cmp.dis;
	}
} ;
priority_queue <node> q;
ll dijkstra () {
	fill (d + 2, d + n + 1, inf);
	q.push ({0, 1});
	while (! q.empty ()) {
		node t = q.top ();
		q.pop ();
		int u = t.u;
		if (t.dis > d[u])
			continue ;
		for (pii i : v[u]) {
			int v = i.first;
			if (d[u] + i.second < d[v]) {
				d[v] = d[u] + i.second;
				q.push ({d[v], v});
			}
		}
	}
	return d[n];
}
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> n;
	for (int i = 1; i < n; ++ i) {
		cin >> a >> b >> c;
		v[i].push_back ({i + 1, a});
		v[i].push_back ({c, b});
	}
	cout << dijkstra ();
	return 0;
}
原文