P1144-最短路计数

布满荆棘的人生 2023-06-01 08:55 116阅读 0赞
  1. 1 #include <bits/stdc++.h>
  2. 2 #define pb push_back
  3. 3 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
  4. 4 #define INF 1000000003
  5. 5 #define ll long long
  6. 6
  7. 7 using namespace std;
  8. 8
  9. 9 const int maxn = 2000003;
  10. 10 inline ll read()
  11. 11 {
  12. 12 ll ans = 0;
  13. 13 char ch = getchar(), last = ' ';
  14. 14 while(!isdigit(ch)) last = ch, ch = getchar();
  15. 15 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  16. 16 if(last == '-') ans = -ans;
  17. 17 return ans;
  18. 18 }
  19. 19 inline void write(ll x)
  20. 20 {
  21. 21 if(x < 0) x = -x, putchar('-');
  22. 22 if(x >= 10) write(x / 10);
  23. 23 putchar(x % 10 + '0');
  24. 24 }
  25. 25 struct edge
  26. 26 {
  27. 27 ll to;
  28. 28 ll cost;
  29. 29 };
  30. 30 vector<edge> G[maxn];
  31. 31
  32. 32 ll V,E,B;
  33. 33
  34. 34 typedef pair<ll,ll> P;//first 是最短距离,second 是顶点编号
  35. 35 ll d[maxn];
  36. 36 ll t[maxn];
  37. 37 ll ans[maxn];
  38. 38 void shortest_path(int s)
  39. 39 {
  40. 40 priority_queue<P,vector<P>,greater<P>> que;
  41. 41
  42. 42 _for(i,1,V+1)
  43. 43 d[i] = INF;
  44. 44 d[s] = 0;
  45. 45 ans[s] = 1;
  46. 46 que.push(P{
  47. 0,s});
  48. 47
  49. 48 while(!que.empty())
  50. 49 {
  51. 50 P p = que.top();que.pop();
  52. 51 int v = p.second;
  53. 52 if(d[v] < p.first) continue;
  54. 53
  55. 54 _for(i,0,G[v].size())
  56. 55 {
  57. 56 edge e = G[v][i];
  58. 57 if(d[e.to] > d[v] + e.cost)
  59. 58 {
  60. 59 d[e.to] = d[v] + e.cost;
  61. 60 que.push(P{d[e.to],e.to});
  62. 61 }
  63. 62 if(d[e.to] == d[v] + e.cost)
  64. 63 {
  65. 64 ans[e.to] += ans[v];
  66. 65 ans[e.to] %= 100003;
  67. 66 }
  68. 67 }
  69. 68 }
  70. 69 }
  71. 70
  72. 71 int main()
  73. 72 {
  74. 73 V = read(),E = read();
  75. 74
  76. 75 _for(i,0,E)
  77. 76 {
  78. 77 ll s,t,c;
  79. 78 s = read(),t = read(),c = 1;
  80. 79 // scanf("%d %d %d",&s,&t,&c);
  81. 80 G[s].push_back(edge{t,c});
  82. 81 G[t].push_back(edge{s,c});
  83. 82 }
  84. 83 shortest_path(1);
  85. 84 _for(i,1,V+1)
  86. 85 printf("%d\n",ans[i]);
  87. 86 return 0;
  88. 87 }

转载于:https://www.cnblogs.com/Asurudo/p/11518449.html

发表评论

表情:
评论列表 (有 0 条评论,116人围观)

还没有评论,来说两句吧...

相关阅读