字典序?建树时从小枚举,用\(Dijkstra\)的血泪建好树,\(size\)大小决定贡献

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define QWQ
#ifdef QWQ
#define D_e_Line printf("\n---------------\n")
#define D_e(x) cout << (#x) << " : " << x << "\n"
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
#endif
struct ios {
template<typename ATP> inline ios& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <='9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
return *this;
}
}io;
using namespace std;
template<typename ATP> inline ATP Max(ATP a, ATP b) {
return a > b ? a : b;
}
template<typename ATP> inline ATP Min(ATP a, ATP b) {
return a < b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
return a < 0 ? -a : a;
}
#include <queue>
#include <vector> const int N = 1e4 + 7;
const int M = 5e4 + 7; #define int long long
int n, m, T;
int SIZ[N], dis[N]; struct Edge {
int nxt, pre, w;
} e[M << 1];
int head[N], cntEdge;
inline void add(int u, int v, int w) {
e[++cntEdge] = (Edge){ head[u], v, w}, head[u] = cntEdge;
}
struct nod {
int x, w;
bool operator < (const nod &com) const {
return w > com.w;
}
};
priority_queue<nod> q;
inline void Dijkstra(int st) {
Fill(dis, 0x3f);
dis[st] = 0;
q.push((nod){ st, 0});
while(!q.empty()){
int u = q.top().x, w = q.top().w;
q.pop();
if(w != dis[u]) continue;
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(dis[v] > dis[u] + e[i].w){
dis[v] = dis[u] + e[i].w;
q.push((nod){ v, dis[v]});
}
}
}
} int ans;
vector<int> G[N];
bool vis[N];
inline void Build() {
R(u,1,n){
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(dis[v] == dis[u] + e[i].w && !vis[v]){
vis[v] = 1;
G[u].push_back(v);
}
}
}
} inline int DFS(int u) {
int siz = SIZ[u];
for(vector<int>::iterator v = G[u].begin(); v != G[u].end(); ++v){
// if(*v == fa) continue;
siz += DFS(*v);
}
ans = Max(ans, siz * (dis[u] - T));
return siz;
}
#undef int
int main() {
#define int long long
// FileOpen();
io >> n >> m >> T;
R(i,1,n){
io >> SIZ[i];
}
R(i,1,m){
int u, v, w;
io >> u >> v >> w;
add(u, v, w);
add(v, u, w);
} Dijkstra(1); Build();
DFS(1); printf("%lld", ans); return 0;
}

LuoguP5201 [USACO19JAN]Shortcut(最短路树)的更多相关文章

  1. Luogu P5201 [USACO19JAN]Shortcut 最短路树???

    最短路树...开眼界了...之前想也没想过.... 先跑出来1到每个点最短路,然后建树时要标记点的入度,否则会多连边...然后深搜时更新新答案就是 #include<cstdio> #in ...

  2. LG5201 「USACO2019JAN」Shortcut 最短路树

    \(\mathrm{Shortcut}\) 问题描述 LG5201 题解 最短路树. 显然奶牛的路径就是从\(1\)走到各个草地,于是从\(1\)跑最短路,构建最短路树. 为了保证字典序,从\(1\) ...

  3. hdu 3409 最短路树+树形dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3409 参考博客:http://www.cnblogs.com/woaishizhan/p/318981 ...

  4. LA4080/UVa1416 Warfare And Logistics 最短路树

    题目大意: 求图中两两点对最短距离之和 允许你删除一条边,让你最大化删除这个边之后的图中两两点对最短距离之和. 暴力:每次枚举删除哪条边,以每个点为源点做一次最短路,复杂度\(O(NM^2logN)\ ...

  5. BZOJ1975[Sdoi2010]魔法猪学院——可持久化可并堆+最短路树

    题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世界是由元素构成的:元素与 ...

  6. BZOJ4356Ceoi2014 Wall——堆优化dijkstra+最短路树

    题目描述 给出一个N*M的网格图,有一些方格里面存在城市,其中首都位于网格图的左上角.你可以沿着网络的边界走,要求你走的路线是一个环并且所有城市都要被你走出来的环圈起来,即想从方格图的外面走到任意一个 ...

  7. 51nod 1443 路径和树(最短路树)

    题目链接:路径和树 题意:给定无向带权连通图,求从u开始边权和最小的最短路树,输出最小边权和. 题解:构造出最短路树,把存留下来的边权全部加起来.(跑dijkstra的时候松弛加上$ < $变成 ...

  8. Berland and the Shortest Paths CodeForces - 1005F(最短路树)

    最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...

  9. LA 4080 战争和物流(最短路树)

    https://vjudge.net/problem/UVALive-4080 题意:给出一个n个结点m条边的无向图,每条边上有一个正权.令c等于每对结点的最短路长度之和.不连通的两点的最短路长度视为 ...

随机推荐

  1. 安装Squid到CentOS(YUM)

    运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:无 硬件要求:无 安装过程 1.关闭防火墙和SeLinux [root@localhost ~] ...

  2. TypeError: this.getOptions is not a function

    我在vue ui界面中安装版本依赖包后报这个错误 less-loader/sass-loader安装的版本过高 解决办法 删除原有的版本依赖包,安装更低版本的依赖包. 如 @6.0.1为选择安装的版本 ...

  3. 【JSOI2016】最佳团体

    思路:二分答案+动态规划(结合dfs序) 类型:选/不选:最大比值 代码: #include<stdio.h> #include<bits/stdc++.h> using na ...

  4. C++:小包包的玩具

    小包包的玩具 时间限制 : 1.000 sec        内存限制 : 128 MB 题目描述: 小包包最讨厌的是整理他自己的玩具,为此,他制造了一个伟大的发明:玩具传送门!利用这个传送门,他可以 ...

  5. Web自动化定位方法以及常用便捷操作

    很遗憾现在才开始给大家逐步分享自动化教程,原本计划着将现有的接口以及app.pc网页端进行自动化处理后再逐步给大家好好分享一下,由于当前实在没必要自动化操作了,所以临时用脑海中的知识再为大家继续更一篇 ...

  6. cool-admin vite-vue3 打包部署 nginx代理设置

    location /api {rewrite ^/api/(.*)$ /$1 break;proxy_pass http://xxx.com;}location /socket.io {rewrite ...

  7. DevStream 成为 CNCF Sandbox 项目啦!- 锣鼓喧天、鞭炮齐鸣、红旗招展、忘词了。

    开局两张图,内容全靠"编" 来,有图有真相! DevStream ️ CNCF DevStream joins CNCF Sandbox CNCF Cloud Native Int ...

  8. 一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推

    迎面走来了你的面试官,身穿格子衫,挺着啤酒肚,发际线严重后移的中年男子. 手拿泡着枸杞的保温杯,胳膊夹着MacBook,MacBook上还贴着公司标语:"加班使我快乐". 面试官: ...

  9. RPA应用场景-信用卡交易争议后续流程

    RPA应用场景-信用卡交易争议后续流程 场景概述 信用卡交易争议后续流程 所涉系统名称 客服系统,邮件 人工操作(时间/次) 4小时 所涉人工数量20操作频率 不定时 场景流程 1.RPA自动接收客户 ...

  10. UiPath文本操作Get Text的介绍和使用

    一.Get Text操作的介绍 从指定的UI元素提取文本值 二.Get Text在UiPath中的使用 1. 打开设计器,在设计库中新建一个Sequence,为序列命名及设置Sequence存放的路径 ...