题意

给定一张无向图,对每个点$i\in S$求$\min_{j\in S} {2\times d(i,j)+a_j}$


考虑多源多汇最短路会超时,换个角度考虑每个$j$,如果$j=i$,那么答案为$a_i$,如果有更优的方案,那么为$i$到$j$的一条路径加上$a_j$,将这个过程看成两条路径,并且将$a_j$独立为一条路径,就得到最短路算法中的松弛操作,可以建立一个超级源点,连向各点,边权为$a_i$,那么从源点跑一遍最短路之后就是每个点所求答案

时间复杂度$O(n\log n)$

代码

#include <bits/stdc++.h>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
const int N = 200010;
int cnt, head[N], nxt[3 * N], to[3 * N];
LL val[3 * N];
inline void add_edge(int u, int v, LL w) {
to[cnt] = v; val[cnt] = w; nxt[cnt] = head[u]; head[u] = cnt++;
}
int vis[N];
LL dis[N];
int n, m, x, y;
LL z;
inline void dijkstra(int s) {
priority_queue<pli, vector<pli >, greater<pli > > pq;
memset(vis, 0, sizeof(vis));
memset(dis,0x3f,sizeof(dis)); dis[s] = 0;
pq.push(make_pair(dis[s], s));
while(!pq.empty()) {
pli now = pq.top(); pq.pop();
int u = now.second; if(vis[u]) continue; vis[u] = 1;
for(int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if(dis[v] > dis[u] + val[i]) {
dis[v] = dis[u] + val[i];
pq.push(make_pair(dis[v], v));
}
}
}
}
int main() {
cnt = 0; memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i) {
scanf("%d%d%I64d", &x, &y, &z);
add_edge(x, y, 2 * z); add_edge(y, x, 2 * z);
}
for(int i = 1; i <= n; ++i) {
scanf("%I64d", &z);
add_edge(0, i, z);
}
dijkstra(0);
for(int i = 1; i <= n; ++i) {
printf("%I64d%c", dis[i], i == n ? '\n' : ' ');
}
return 0;
}

【Educational Codeforces Round 38】D. Buy a Ticket 堆优化Dijkstra的更多相关文章

  1. Educational Codeforces Round 38 部分题解

    D. Buy a Ticket 分析 建一个源点,连向所有结点,边的花费为那个结点的花费,图中原有的边花费翻倍,最后跑一遍最短路即可. code #include<bits/stdc++.h&g ...

  2. Educational Codeforces Round 38 (Rated for Div. 2)

    这场打了小号 A. Word Correction time limit per test 1 second memory limit per test 256 megabytes input sta ...

  3. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  4. Educational Codeforces Round 38

    http://codeforces.com/contest/938 A:sb题 //#pragma comment(linker, "/stack:200000000") //#p ...

  5. Educational Codeforces Round 38 (Rated for Div. 2) C

    C. Constructing Tests time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化

    链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...

  7. Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)

    Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...

  8. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  9. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

随机推荐

  1. python pytest

    之前一直用unittest ,现在学习pytest 看看那个好 1. 安装 pip install -U pytest py.test --version 2. 只需要按照下面的规则: 测试文件以te ...

  2. 【Atheros】内核调试及网卡加载等问题小结

    我做的其他很多工作就比较有针对性了,不是什么大众性的问题,比如加统计代码.实现自己的速率调整算法或者加一些自己的控制什么的,就不再单独介绍了,最后呢再罗列一些小问题,供参考. 1. 加载模块(执行wi ...

  3. Android 中通过切割图片创建人物行走动画

    以前一直使用序列图片来实现动画效果,造成空间的极大浪费,所以想要尝试下切割图片来实现动画. 如图所示,是由66rpg纸娃娃系统生成的角色行走图.本程序必须实现将人物的整体图片切割后存入4x4的数组来动 ...

  4. app 之间发送文件 ios

    本文转载至 http://www.51094.com/?p=212   第一种: 发送一个正常的  pdf 文件,只要是能读取pdf 的都能得到响应 -(IBAction)openDocumentIn ...

  5. 【BZOJ4033】[HAOI2015]树上染色 树形DP

    [BZOJ4033][HAOI2015]树上染色 Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染 ...

  6. Lorenzo Von Matterhorn(STL_map的应用)

    Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Struts中类型转换踩的坑

    出现的异常: 当我输入的数据很大时候,转换后如上,这并不是我想要的, 出现问题的原因: Struts2对常用的数据类型如String.Integer.Double等都添加了转换器进行对应的转换操作. ...

  8. 15.Django添加一个功能模块的步骤(和SpringMVC类比)

    这里介绍如何在Django里新建一个模块,这个例子还是最简单的例子 通过浏览器访问 http://localhost:8000/hello/然后返回一个欢迎页 我是做java web出身的,这里用py ...

  9. PHP中ob系列函数讲解(浏览器缓存技术) (转)

    Output Control 函数可以让你自由控制脚本中数据的输出.它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况. 输出控制函数不对使用 header() 或 setcooki ...

  10. R语言图形base系统(三)

     本篇介绍R语言base系统绘制散点图.条形图.直方图.箱线图.饼图,还将简单介绍点图.核密度图.折线图. 散点图: attach(mtcars) plot(wt, mpg, main="B ...