题意简述:n个点m条边的无向图,有点权,有边权, 对于每一个点计算,d(i,j)表示点i到点j的最短路

题解:边权扩大二倍,建立源点,然后源点向每一个点x连接一条权值为a[x]的边,然后跑最短路即可

  1. #include<bits/stdc++.h>
  2.  
  3. #define forn(i, n) for (int i = 0; i < int(n); i++)
  4. #define fore(i, s, t) for (int i = s; i < (int)t; i++)
  5. #define fi first
  6. #define se second
  7. #define all(x) x.begin(),x.end()
  8. #define pf2(x,y) printf("%d %d\n",x,y)
  9. #define pf(x) printf("%d\n",x)
  10. #define each(x) for(auto it:x) cout<<it<<endl;
  11. #define pi pair<int,int>
  12.  
  13. using namespace std;
  14.  
  15. char inline nc(){
  16. static char buf[100000],*p1=buf,*p2=buf;
  17. return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  18. }
  19.  
  20. template <typename T>
  21. bool rd(T& v){
  22. static char ch;
  23. while(ch!=EOF&&!isdigit(ch)) ch=nc();
  24. if(ch==EOF) return false;
  25. for(v=0;isdigit(ch);ch=nc())
  26. v=v*10+ch-'0';
  27. return true;
  28. }
  29.  
  30. template <typename T>
  31. void o(T p){
  32. static int stk[70],tp;
  33. if(p==0) {
  34. putchar('0');return ;
  35. }
  36. if(p<0) {
  37. p=-p;putchar('-');
  38. }
  39. while(p) stk[++tp]=p%10,p/=10;
  40. while(tp) putchar(stk[tp--]+'0');
  41. }
  42.  
  43. typedef long long ll;
  44.  
  45. const int maxn=2e5+5;
  46. const int maxm=6e5+5;
  47. const int inf=1e9;
  48.  
  49. int head[maxn],ver[maxm],nex[maxm],tot;
  50.  
  51. ll wi[maxm];
  52.  
  53. int n,m;
  54.  
  55. void inline AddEdge(int x,int y,ll z){
  56. ver[++tot]=y,wi[tot]=z,nex[tot]=head[x],head[x]=tot;
  57. }
  58.  
  59. ll dis[maxn];
  60.  
  61. bool vis[maxn];
  62.  
  63. void dijkstra(){
  64. memset(dis,0x3f,sizeof(dis));
  65. dis[n+1]=0;
  66. multiset<pair<ll,int>> q;
  67. q.insert({0,n+1});
  68. while(q.size()){
  69. int x=q.begin()->se;q.erase(q.begin());
  70. if(vis[x]) continue;
  71. vis[x]=1;
  72. for(int i=head[x];i;i=nex[i]){
  73. int y=ver[i];
  74. if(dis[y]>dis[x]+wi[i]){
  75. dis[y]=dis[x]+wi[i];
  76. q.insert({dis[y],y});
  77. }
  78. }
  79. }
  80. }
  81. int main(){
  82. ios_base::sync_with_stdio(false);
  83. cin>>n>>m;
  84. for(int i=0;i<m;i++){
  85. int x,y;ll z;
  86. cin>>x>>y>>z;
  87. AddEdge(x,y,2*z);
  88. AddEdge(y,x,2*z);
  89. }
  90. for(int i=0;i<n;i++){
  91. ll x;
  92. cin>>x;
  93. AddEdge(n+1,i+1,x);
  94. }
  95. dijkstra();
  96. for(int i=1;i<=n;i++)
  97. cout<<dis[i]<<' ';
  98. cout<<"\n";
  99. }

  

cf938D的更多相关文章

  1. CF938D Buy a Ticket

    这个题都想不出来,感觉

  2. CF938D Buy a Ticket dijkstra

    考试T1,建一个反图跑一个最短路就好了~ code: #include <bits/stdc++.h> #define ll long long #define N 200002 #def ...

  3. cf自训4.10

    cf933A dp题 一开始看错是连续子序列了,然后样例刚好能过.. 然后正解没想出来,网上看了题解:感觉正解是枚举2开始的位置,然后再枚举翻转的区间,pos左右两侧分别求出贡献最大的那个区间,左右两 ...

  4. NOIP前刷题记录

    因为本蒻实在太蒻了...对于即将到来的NOIP2018ssfd,所以下决心要把自己近期做过的题目(衡量标准为洛谷蓝题难度或以上)整理一下,归归类,简单地写一下思路,就当作自己复习了吧qwq 本随笔持续 ...

  5. NOIP刷题

    搜索 [NOIP2013]华容道 最短路+带剪枝的搜索,是一个思维难度比较大的题目. CF1064D Labyrinth 考虑贪心,用双向队列bfs [NOIP2017]宝藏 剪枝搜索出奇迹 题解:h ...

随机推荐

  1. qt creator源码全方面分析(0)

    本人主攻C++和Qt. 上两天刚研究完Qt install framework(IFW)应用程序安装框架. google没发现有正儿八经的官方文档的翻译,我就进行了翻译哈!! 系列文章具体见:http ...

  2. CTF--HTTP服务--路径遍历(拿到www-data用户权限)

    开门见山 1. 扫描靶机ip,发现PCS 172.18.4.20 2. 用nmap扫描靶机开放服务及版本 3. 再扫描靶机的全部信息 4. 用nikto工具探测http服务敏感信息 5. 用dirb工 ...

  3. day05【数组】

    day05[数组] 1.数组 概念:是一种容器,可以同时存放多个数据. 特点: 数组是一种引用数据类型 数组当中的多个数据,类型必须统一 数组的长度在程序的运行期间不可改变 初始化:在内存当中创建一个 ...

  4. Leetcode 题目整理-1

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  5. acwing 243. 一个简单的整数问题2 树状数组 线段树

    地址 https://www.acwing.com/problem/content/description/244/ 给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“C l ...

  6. react中 如何使用图片

    //render 第一种方法:先const一个对象,把需要应用图片的dom上的style写入对象中, 然后在return()中使用style关键字赋值为预先定义的那个style对象 const bgG ...

  7. requestAnimationFrame 与 seeTimeout 的区别

    requestAnimationFrame 随着浏览器的刷新而执行. let a =  () =>{          doSomething() ;         window.reques ...

  8. JVM性能优化系列-(3) 虚拟机执行子系统

    3. 虚拟机执行子系统 3.1 Java跨平台的基础 Java刚诞生的宣传口号:一次编写,到处运行(Write Once, Run Anywhere),其中字节码是构成平台无关的基石,也是语言无关性的 ...

  9. TypeError:Can't instantiate abstract class Ultraman with abstract methods sttack 报错

    报错Can't instantiate abstract class Ultraman with abstract methods sttack 通过非常仔细的排查,发现错误如下: 1.单词拼写错误是 ...

  10. 使用小书匠及markdown here编辑博客和微信公众号

    1. 使用小书匠连接Evernote并发布笔记到博客园 1.1 小书匠初探 我平时的信息收集的主要方法是采用Pocket+Evernote. 简单来说: 如果访问到非常有用,而且是必须要立刻记录的内容 ...