首页 > 其他分享 >题解:AT_abc362_d [ABC362D] Shortest Path 3

题解:AT_abc362_d [ABC362D] Shortest Path 3

时间:2024-07-14 16:08:10浏览次数:19  
标签:head ch read 题解 ll Path 点权 Shortest dis

一句话题意:给定一个带点权的有权无向连通图,求点 1 到所有其它点的最短路径。

首先,只有 1 一个起点,所以是单源最短路,又因为最大是 \(2 \times 10^5\),所以是优先队列(堆)优化过后的 Dijkstra。

所以,我们只需要解决点权的问题就好了。一种显而易见的想法是把与这条边的边权加上起终点的点权,因为走这条边肯定是要过这两个点的。但这样有个问题:样例都过不了(或许是我写挂了?反正只过了样例 2)!为啥呢?

你从 1 走到 2,再从 2 走到 3,你这点 2 的点权不就算重了?

那该咋办呢?

其实你不用改边权,对 Dijkstra 做一点小改动即可。什么小改动呢?每次走边的时候加上终点的点权,这样子就方便算、判断了。注意不能加起点的,因为起点是固定的,你算它跟没算一样。

比如还是从 1 到 2 再到 3,你每条边再加上点 2、3 的点权,就不会出事了。

但注意此时点 1 的点权还没算,输出是加上就好。


ACCode:

// Problem: D - Shortest Path 3
// Contest: AtCoder - Toyota Programming Contest 2024#7(AtCoder Beginner Contest 362)
// URL: https://atcoder.jp/contests/abc362/tasks/abc362_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

/*Code by Leo2011*/
#include <bits/stdc++.h>

#define log printf
#define EPS 1e-8
#define INF 0x3f3f3f3f3f3f3f3f
#define FOR(i, l, r) for (ll(i) = (l); (i) <= (r); ++(i))
#define IOS                      \
	ios::sync_with_stdio(false); \
	cin.tie(nullptr);            \
	cout.tie(nullptr);

using namespace std;

typedef __int128 i128;
typedef long long ll;
typedef pair<ll, ll> PII;

const ll N = 1e6 + 10;
ll m, n, s, x, y, z, a[N], w[N], to[N], idx, dis[N], nxt[N], head[N];

struct line {
	ll u, d;
	bool operator<(const line &cmp) const { return d > cmp.d; }
};
priority_queue<line> q;

template <typename T>

inline T read() {
	T sum = 0, fl = 1;
	char ch = getchar();
	for (; !isdigit(ch); ch = getchar())
		if (ch == '-') fl = -1;
	for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
	return sum * fl;
}

template <typename T>

inline void write(T x) {
	if (x < 0) putchar('-'), write<T>(-x);
	static T sta[35];
	ll top = 0;
	do { sta[top++] = x % 10, x /= 10; } while (x);
	while (top) putchar(sta[--top] + 48);
}

inline void init() {
	memset(head, -1, sizeof(head));
	memset(dis, 0x3f, sizeof(dis));
}

inline void addEdge(ll u, ll v, ll q) {
	w[idx] = q;
	to[idx] = v;
	nxt[idx] = head[u];
	head[u] = idx;
	idx++;
}

void dijkstra(ll s) {
	dis[s] = 0;
	q.push({s, 0});
	while (!q.empty()) {
		line f = q.top();
		q.pop();
		ll u = f.u;
		if (f.d > dis[u]) continue;
		for (ll i = head[u]; i != -1; i = nxt[i]) {
			ll v = to[i];
			if (dis[u] + w[i] + a[v] < dis[v]) {
				dis[v] = dis[u] + w[i] + a[v];  // 把要去的点的点权算上
				q.push({v, dis[v]});
			}
		}
	}
}

int main() {
	init();
	n = read<ll>(), m = read<ll>(), s = 1;
	FOR(i, 1, n) a[i] = read<ll>();
	FOR(i, 1, m) {
		x = read<ll>(), y = read<ll>(), z = read<ll>();
		addEdge(x, y, z), addEdge(y, x, z);
	}
	dijkstra(s);
	FOR(i, 2, n)
	write<ll>(dis[i] + a[1]), putchar(' ');  // 除了起点都有了,所以要加上起点
	return 0;
}

AC 记录~

理解万岁!

标签:head,ch,read,题解,ll,Path,点权,Shortest,dis
From: https://www.cnblogs.com/leo2011/p/18301668

相关文章

  • 题解:CodeForces 346A Alice and Bob[博弈/数论]
    CodeForces346AA.AliceandBobtimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputItissoboringinthesummerholiday,isn'tit?SoAliceandBobhaveinventedanewgametoplay.Therulesa......
  • 题解:CodeForces 843A Sorting by Subsequences[模拟/排序]
    CodeForces843AA.SortingbySubsequencestimelimitpertest:1secondmemorylimitpertest:256megabytesinputstandardinputoutputstandardoutputYouaregivenasequence\(a_1, a_2, ..., a_n\)consistingofdifferentintegers.Itisrequiredtos......
  • 题解:CodeForces 618C Constellation[贪心/模拟]
    CodeForces618CC.Constellationtimelimitpertest:2secondsmemorylimitpertest:256megabytesinputstandardinputoutputstandardoutputCatNokuhasobtainedamapofthenightsky.Onthismap,hefoundaconstellationwithnstarsnumberedfrom......
  • 题解:CodeForces 835 D Palindromic characteristics[区间dp/马拉车Manacher]
    CodeForces835DD.Palindromiccharacteristicstimelimitpertest:3secondsmemorylimitpertest:256megabytes*inputstandardinputoutputstandardoutputPalindromiccharacteristicsofstring\(s\)withlength\(|s|\)isasequenceof\(|s|\)in......
  • 题解:CodeForces 1019 A Elections[贪心/三分]
    CodeForces1019AA.Electionstimelimitpertest:2secondsmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputAsyouknow,majorityofstudentsandteachersofSummerInformaticsSchoolliveinBerlandforthemostparto......
  • [CF1941E] Rudolf and k Bridges 的题解
    题目大意在第\((i,j)\)个格子修建一个桥墩需要\(a_{i,j}+1\)的花费而且要求\((i,0)\)与\((i,m)\)必须修建桥墩并且桥墩之间的距离不得大于\(d\)。现在需要求见\(k\)个连续的桥,求最小代价。其中\(1\lek\len\le100,3\lem\le2\cdot10,1\led\lem\)。思路因为......
  • [ABC206E] Divide Both 的题解
    题目大意求出从\(l\)至\(r\)中满足以下条件的\((x,y)\)的个数。\(\gcd(x,y)\ne1\)且\(\gcd(x,y)\nex\)且\(\gcd(x,y)\ney\)。其中\(1\lel\ler\le10^6\)。思路正难则反,所以可以求出所有互质或者是相互倍数的\((x,y)\)的对数,在将其减去所有的方案数就是......
  • [CF1178D] Prime Graph 的题解
    题目大意构造一个简单无向图,是所有的有度的点的度都是质数而且总共的边的数量的个数是质数。思路因为需要让所有的入度都为质数,所以我们可以找到两个相邻的质数\(2,3\),因为这样即使增加了一条边那么这个节点的度也是质数。先将这个图构成一个巨大的环,接着如果所有的边数并不......
  • [CF1538F] Interesting Function 的题解
    题目大意给定两个正整数\(l,r\),将\(l\)不断加\(1\)直到\(l=r\),求出这一过程中\(l\)发生变化的位数总数。\(1\lel<r\le10^9\)。思路假设从\(l\)处理到\(r\)变化的次数为\(f(l,r)\)。因为直接求解出\(f(l,r)\)十分困难,所以可以通过求出\(f(0,l)\)......
  • 题解:CodeForces 1511 C Yet Another Card Deck[暴力/模拟]
    CodeForces1511CC.YetAnotherCardDeckDescriptionYouhaveacarddeckof\(n\)cards,numberedfromtoptobottom,i. e.thetopcardhasindex\(1\)andbottomcard —index\(n\).Eachcardhasitscolor:the\(i\)-thcardhascolor\(a_i\......