【Educational Codeforces Round 38】D. Buy a Ticket 堆优化Dijkstra
题意
给定一张无向图,对每个点$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的更多相关文章
- Educational Codeforces Round 38 部分题解
D. Buy a Ticket 分析 建一个源点,连向所有结点,边的花费为那个结点的花费,图中原有的边花费翻倍,最后跑一遍最短路即可. code #include<bits/stdc++.h&g ...
- 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 ...
- 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解
[比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B Run for your prize[贪心] ...
- Educational Codeforces Round 38
http://codeforces.com/contest/938 A:sb题 //#pragma comment(linker, "/stack:200000000") //#p ...
- 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 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...
- Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
随机推荐
- rebound是facebook的开源动画库
网址:http://www.jcodecraeer.com/a/opensource/2015/0121/2338.html 介绍: rebound是facebook的开源动画库.可以认为这个动画库是 ...
- Android Studio 默认的快捷键
参考资料: 1.http://stormzhang.com/devtools/2014/12/09/android-studio-tutorial3/ Action Mac OSX Win/Linux ...
- openresty 定时器
[1]nginx定时器应用 (1)文件目录结构 (2)nginx.conf配置 lua_package_path "/usr/local/lib/ubcsrvd/lualib/?.lua;; ...
- ipmitool常用命令
然后查看ip等信息,使用如下命令: ipmitool lan print 远程访问 可使用如下命令尝试: ipmitool -I lanplus -H 10.108.125.227 -U Admini ...
- 几种session存储方式比较
原文: http://blog.sina.com.cn/s/blog_495697e6010143tj.html 集群中session安全和同步是个最大的问题,下面是我收集到的几种session同步的 ...
- 苹果input点击页面稍微变大的问题
今天在群里看到有人问input标签点击以后在ios下页面会变大一点的问题 说实话我是没有遇到过后来解决了我看了一下代码 我明白了 不是我没有遇到过是因为我写的比较规范 所以没出现那样的问题 嘿嘿. ...
- Solr6.5与mysql集成建立索引
首先在solrconfig.xml(我的是保存在/usr/local/tomcat/solrhome/mycore/conf/下)的<requestHandler name="/sel ...
- Boxes and Candies(贪心)
Boxes and Candies Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Ther ...
- output value . Sigmoid neurons are similar to perceptrons, but modified so that small changes in their weights and bias cause only a small change in their output.
http://neuralnetworksanddeeplearning.com/chap1.html . Sigmoid neurons are similar to perceptrons, bu ...
- js函数的caller属性
funcName.caller : 返回一个对函数的引用, 该函数调用了当前函数 function test() { if (test.caller) { var a = test.caller.to ...