题意

给定一张无向图,对每个点$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)$

代码

  1. #include <bits/stdc++.h>
  2. #define inf 0x7f7f7f7f
  3. using namespace std;
  4. typedef long long LL;
  5. typedef pair<LL, int> pli;
  6. const int N = 200010;
  7. int cnt, head[N], nxt[3 * N], to[3 * N];
  8. LL val[3 * N];
  9. inline void add_edge(int u, int v, LL w) {
  10. to[cnt] = v; val[cnt] = w; nxt[cnt] = head[u]; head[u] = cnt++;
  11. }
  12. int vis[N];
  13. LL dis[N];
  14. int n, m, x, y;
  15. LL z;
  16. inline void dijkstra(int s) {
  17. priority_queue<pli, vector<pli >, greater<pli > > pq;
  18. memset(vis, 0, sizeof(vis));
  19. memset(dis,0x3f,sizeof(dis)); dis[s] = 0;
  20. pq.push(make_pair(dis[s], s));
  21. while(!pq.empty()) {
  22. pli now = pq.top(); pq.pop();
  23. int u = now.second; if(vis[u]) continue; vis[u] = 1;
  24. for(int i = head[u]; ~i; i = nxt[i]) {
  25. int v = to[i];
  26. if(dis[v] > dis[u] + val[i]) {
  27. dis[v] = dis[u] + val[i];
  28. pq.push(make_pair(dis[v], v));
  29. }
  30. }
  31. }
  32. }
  33. int main() {
  34. cnt = 0; memset(head, -1, sizeof(head));
  35. scanf("%d%d", &n, &m);
  36. for(int i = 1; i <= m; ++i) {
  37. scanf("%d%d%I64d", &x, &y, &z);
  38. add_edge(x, y, 2 * z); add_edge(y, x, 2 * z);
  39. }
  40. for(int i = 1; i <= n; ++i) {
  41. scanf("%I64d", &z);
  42. add_edge(0, i, z);
  43. }
  44. dijkstra(0);
  45. for(int i = 1; i <= n; ++i) {
  46. printf("%I64d%c", dis[i], i == n ? '\n' : ' ');
  47. }
  48. return 0;
  49. }

【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. rebound是facebook的开源动画库

    网址:http://www.jcodecraeer.com/a/opensource/2015/0121/2338.html 介绍: rebound是facebook的开源动画库.可以认为这个动画库是 ...

  2. Android Studio 默认的快捷键

    参考资料: 1.http://stormzhang.com/devtools/2014/12/09/android-studio-tutorial3/ Action Mac OSX Win/Linux ...

  3. openresty 定时器

    [1]nginx定时器应用 (1)文件目录结构 (2)nginx.conf配置 lua_package_path "/usr/local/lib/ubcsrvd/lualib/?.lua;; ...

  4. ipmitool常用命令

    然后查看ip等信息,使用如下命令: ipmitool lan print 远程访问 可使用如下命令尝试: ipmitool -I lanplus -H 10.108.125.227 -U Admini ...

  5. 几种session存储方式比较

    原文: http://blog.sina.com.cn/s/blog_495697e6010143tj.html 集群中session安全和同步是个最大的问题,下面是我收集到的几种session同步的 ...

  6. 苹果input点击页面稍微变大的问题

    今天在群里看到有人问input标签点击以后在ios下页面会变大一点的问题  说实话我是没有遇到过后来解决了我看了一下代码 我明白了 不是我没有遇到过是因为我写的比较规范 所以没出现那样的问题  嘿嘿. ...

  7. Solr6.5与mysql集成建立索引

    首先在solrconfig.xml(我的是保存在/usr/local/tomcat/solrhome/mycore/conf/下)的<requestHandler name="/sel ...

  8. Boxes and Candies(贪心)

    Boxes and Candies Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Ther ...

  9. 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 ...

  10. js函数的caller属性

    funcName.caller : 返回一个对函数的引用, 该函数调用了当前函数 function test() { if (test.caller) { var a = test.caller.to ...