[关闭]
@Junlier 2018-08-21T09:18:47.000000Z 字数 1150 阅读 2187

Dijkstra+堆优化

图论综合
洛谷题目传送门

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<cstring>
  6. #include<iomanip>
  7. #include<algorithm>
  8. #include<ctime>
  9. #include<queue>
  10. #include<stack>
  11. #include<vector>
  12. #define rg register
  13. #define il inline
  14. #define lst long long
  15. #define ldb long double
  16. #define N 10050
  17. #define M 500050
  18. using namespace std;
  19. const int Inf=2147483647;
  20. int n,m,S,cnt;
  21. struct EDGE{
  22. int to,nxt,v;
  23. }ljl[M];
  24. int hd[N];
  25. lst dis[N],vis[N];
  26. struct cmp{
  27. bool operator()(int x,int y)
  28. {
  29. return dis[x]>dis[y];
  30. }
  31. };
  32. priority_queue <int,vector<int>,cmp> Q;
  33. il int read()
  34. {
  35. rg int s=0,m=0;rg char ch=getchar();
  36. while(ch<'0'||ch>'9'){if(ch=='-')m=1;ch=getchar();}
  37. while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
  38. return m?-s:s;
  39. }
  40. il void Dijkstra()
  41. {
  42. for(rg int i=1;i<=n;++i)dis[i]=Inf;
  43. while(!Q.empty())Q.pop();
  44. Q.push(S),dis[S]=0;
  45. while(!Q.empty())
  46. {
  47. rg int now=Q.top();
  48. Q.pop();
  49. if(!vis[now])
  50. {
  51. vis[now]=1;
  52. for(rg int i=hd[now];i;i=ljl[i].nxt)
  53. {
  54. rg int qw=ljl[i].to;
  55. dis[qw]=min(dis[qw],dis[now]+ljl[i].v);
  56. Q.push(qw);
  57. }
  58. }
  59. }
  60. }
  61. int main()
  62. {
  63. n=read(),m=read(),S=read();
  64. for(rg int i=1;i<=m;++i)
  65. {
  66. rg int p=read(),q=read(),o=read();
  67. ljl[++cnt]=(EDGE){q,hd[p],o},hd[p]=cnt;
  68. }
  69. Dijkstra();
  70. for(rg int i=1;i<=n;++i)
  71. printf("%lld ",dis[i]);
  72. puts("");
  73. return 0;
  74. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注