【题解】JZOJ6578 / 洛谷P5201[USACO2019Jan]Shortcut G

洛谷 P5201 [USACO19JAN] Shortcut G

题意

在一个带权无向连通图上,每个点有 a i a_i ai 只奶牛,奶牛会走最短路径到 1 1 1,如果有多条路径,选择字典序最小的,定义移动总时间为所有奶牛走到 1 1 1 的时间之和。你可以修建一条从任意一点到 1 1 1 的边权为 t t t 的边,奶牛只有在平时走到 1 1 1 的路上看到这条边才会走。求最多能减少多少移动总时间。

题解

题目保证了对于每个点都有唯一的路径走到 1 1 1,那么可以建出一棵树,根节点为 1 1 1。

然后统计一下子树中奶牛数量总和,对于每个点尝试建时间为 t t t 的新边,可以 O ( 1 ) O(1) O(1) 求出减少的移动总时间。设 j j j 为 i i i 的子树中的节点,则减少的时间为 ( d i s i − t ) ∑ j a j (dis_i-t)\sum\limits_{j}a_j (disi−t)j∑aj。

时间复杂度 O ( n ) O(n) O(n)。

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 50005;
int n, m, t, a[N], vis[N];
LL dis[N], cnt[N], ans = 0;
int cn1 = 0, fi1[N], nx1[N << 1], to1[N << 1], va1[N << 1], cn2 = 0, fi2[N], nx2[N << 1], to2[N << 1];
void ad1(int u, int v, int w) {
	cn1++, nx1[cn1] = fi1[u], fi1[u] = cn1, to1[cn1] = v, va1[cn1] = w; 
	cn1++, nx1[cn1] = fi1[v], fi1[v] = cn1, to1[cn1] = u, va1[cn1] = w;
}
void ad2(int u, int v) { cn2++, nx2[cn2] = fi2[u], fi2[u] = cn2, to2[cn2] = v; }
struct node {
	int r;
	LL dis;
	bool operator < (const node &T) const { return dis > T.dis; }
};
priority_queue<node> pq;
void dij(int r) {
	memset(dis, 0x3f, sizeof(dis)), dis[r] = 0;
	pq.push((node){r, 0});
	while (!pq.empty()) {
		node h = pq.top();
		pq.pop();
		if (vis[h.r]) continue;
		vis[h.r] = 1;
		for (int i = fi1[h.r]; i; i = nx1[i])
			if (dis[to1[i]] > dis[h.r] + va1[i])
				dis[to1[i]] = dis[h.r] + va1[i], pq.push((node){to1[i], dis[to1[i]]});
	}
}
void dfs(int r) {
	cnt[r] = a[r];
	for (int i = fi2[r]; i; i = nx2[i]) dfs(to2[i]);
	for (int i = fi2[r]; i; i = nx2[i]) cnt[r] += cnt[to2[i]];
	if (t < dis[r]) ans = max(ans, (dis[r] - t) * cnt[r]);
}
int main() {
	scanf("%d%d%d", &n, &m, &t);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1, u, v, w; i <= m; i++) scanf("%d%d%d", &u, &v, &w), ad1(u, v, w);
	dij(1);
	for (int i = 2; i <= n; i++) {
		int x = n;
		for (int j = fi1[i]; j; j = nx1[j])
			if (dis[i] == dis[to1[j]] + va1[j])
				x = min(x, to1[j]);
		ad2(x, i);
	}
	dfs(1);
	printf("%lld", ans);
	return 0;
}
相关推荐
曦月逸霜17 分钟前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法
海的诗篇_1 小时前
移除元素-JavaScript【算法学习day.04】
javascript·学习·算法
自动驾驶小卡2 小时前
A*算法实现原理以及实现步骤(C++)
算法
Unpredictable2222 小时前
【VINS-Mono算法深度解析:边缘化策略、初始化与关键技术】
c++·笔记·算法·ubuntu·计算机视觉
编程绿豆侠2 小时前
力扣HOT100之多维动态规划:1143. 最长公共子序列
算法·leetcode·动态规划
珂朵莉MM2 小时前
2021 RoboCom 世界机器人开发者大赛-高职组(初赛)解题报告 | 珂学家
java·开发语言·人工智能·算法·职场和发展·机器人
fail_to_code3 小时前
递归法的递归函数何时需要返回值
算法
C137的本贾尼3 小时前
(每日一道算法题)二叉树剪枝
算法·机器学习·剪枝
BUG收容所所长4 小时前
栈的奇妙世界:从冰棒到算法的华丽转身
前端·javascript·算法