跳到正文

题解

AT_abc362_d 题解

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

文章信息

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

版本

  1. v1当前

思路

双倍经验。

显然,经过一条 u→vu\to v 且边权为 ww 的边后,代价一定是 w+avw+a_v。而初始就经过 11 号点,所以 dis1=a1dis_1=a_1,然后跑一遍 dijkstra 模板就行了。注意此题是双向边。

代码

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