补一补之前听课时候的题。

考虑使用dij算法求最短路,因为边权存不下,所以考虑用主席树维护二进制位,因为每一次都只会在一个位置进行修改,所以可以暴力进位,这样均摊复杂度是对的。

《算法导论》给了证明:对于一个有$k$位的二进制计数器,假设每一次都从第0位$+1$,那么我们发现执行$n$次加法之后,发现第零位会变$\left \lfloor \frac{n}{1} \right \rfloor$次,第一位会变$\left \lfloor \frac{n}{2} \right \rfloor$次...而第$n$位会变$\left \lfloor \frac{n}{2^n} \right \rfloor$次,这样子所有的操作次数就相当于$\sum_{i = 0}^{k - 1}\left \lfloor \frac{i}{2^i} \right \rfloor < n * \sum_{i = 0}^{\infty}\frac{1}{2^i} = 2n$,所以最坏情况下的时间为$O(n)$,每一次操作的均摊时间复杂度为$O(n) / n = O(1)$。

这样子只需要借助主席树写一个时间为$O(log)$的$cmp$函数就可以用堆优化dijkskra了,遇到进位就在线段树上暴力搞一搞,反正复杂度是对的。

数太大了直接用题目中给的$seed = 2, Mod = 1e9 + 7$的哈希哈希一下就好了。

时间复杂度$O(nlog^2n)$。

感觉就像是对着大佬的题解抄了一遍。

Code:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. const int N = 1e5 + ;
  8. const int P = 1e9 + ;
  9.  
  10. int n, m, st, ed, lim = , bin[N << ];
  11. int tot = , head[N], pre[N];
  12.  
  13. struct Edge {
  14. int to, nxt, val;
  15. } e[N << ];
  16.  
  17. inline void add(int from, int to, int val) {
  18. e[++tot].to = to;
  19. e[tot].val = val;
  20. e[tot].nxt = head[from];
  21. head[from] = tot;
  22. }
  23.  
  24. inline void read(int &X) {
  25. X = ; char ch = ; int op = ;
  26. for(; ch > ''|| ch < ''; ch = getchar())
  27. if(ch == '-') op = -;
  28. for(; ch >= '' && ch <= ''; ch = getchar())
  29. X = (X << ) + (X << ) + ch - ;
  30. X *= op;
  31. }
  32.  
  33. inline void chkMax(int &x, int y) {
  34. if(y > x) x = y;
  35. }
  36.  
  37. namespace PSegT {
  38. struct SegNode {
  39. int lc, rc, w;
  40. } s[N * ];
  41.  
  42. int nodeCnt, root[N];
  43.  
  44. #define mid ((l + r) >> 1)
  45.  
  46. bool cmp(int x, int l, int r, int y) {
  47. if(l == r) return s[x].w > s[y].w;
  48. if(s[s[x].rc].w == s[s[y].rc].w) return cmp(s[x].lc, l, mid, s[y].lc);
  49. else return cmp(s[x].rc, mid + , r, s[y].rc);
  50. }
  51.  
  52. bool modify(int &x, int l, int r, int pos, int y) {
  53. s[x = ++nodeCnt] = s[y];
  54. if(l == r) {
  55. s[x].w = s[y].w ^ ;
  56. return s[y].w;
  57. }
  58.  
  59. int res;
  60. if(pos > mid) res = modify(s[x].rc, mid + , r, pos, s[y].rc);
  61. else {
  62. res = modify(s[x].lc, l, mid, pos, s[y].lc);
  63. if(res) res = modify(s[x].rc, mid + , r, mid + , s[y].rc);
  64. }
  65.  
  66. s[x].w = (1LL * s[s[x].rc].w * bin[mid - l + ] % P + s[s[x].lc].w) % P;
  67. return res;
  68. }
  69.  
  70. } using namespace PSegT;
  71.  
  72. struct Node {
  73. int x, rt;
  74.  
  75. bool operator < (const Node &oth) const {
  76. return cmp(rt, , lim, oth.rt);
  77. }
  78.  
  79. };
  80. priority_queue <Node> Q;
  81.  
  82. void dfs(int x, int dep) {
  83. if(x == st) {
  84. printf("%d\n%d ", dep, x);
  85. return;
  86. }
  87.  
  88. dfs(pre[x], dep + );
  89. printf("%d ", x);
  90. }
  91.  
  92. inline void out() {
  93. printf("%d\n", s[root[ed]].w);
  94. dfs(ed, );
  95. printf("\n");
  96. exit();
  97. }
  98.  
  99. int main() {
  100. read(n), read(m);
  101. for(int x, y, v, i = ; i <= m; i++) {
  102. read(x), read(y), read(v);
  103. add(x, y, v), add(y, x, v);
  104. chkMax(lim, v);
  105. }
  106. lim += ;
  107. read(st), read(ed);
  108.  
  109. for(int i = bin[] = ; i <= lim; i++)
  110. bin[i] = 1LL * bin[i - ] * % P;
  111.  
  112. nodeCnt = ;
  113. Q.push((Node) {st, root[st]});
  114. for(; !Q.empty(); ) {
  115. Node now = Q.top(); Q.pop();
  116. if(now.rt != root[now.x]) continue;
  117. if(now.x == ed) out();
  118. for(int i = head[now.x]; i; i = e[i].nxt) {
  119. int y = e[i].to, v;
  120. modify(v, , lim, e[i].val, root[now.x]);
  121. if(!root[y] || cmp(root[y], , lim, v)) {
  122. root[y] = v;
  123. Q.push((Node) {y, root[y]});
  124. pre[y] = now.x;
  125. }
  126. }
  127. }
  128.  
  129. puts("-1");
  130. return ;
  131. }

CF 464E The Classic Problem的更多相关文章

  1. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

  2. CodeForces 464E The Classic Problem | 呆克斯歘 主席树维护高精度

    题意描述 有一个\(n\)点\(m\)边的无向图,第\(i\)条边的边权是\(2^{a_i}\).求点\(s\)到点\(t\)的最短路长度(对\(10^9 + 7\)取模). 题解 思路很简单--用主 ...

  3. Codeforces 464E. The Classic Problem

    题目大意 给定一张$n$个点, $m$条边的无向图,求$S$ 到$T$的最短路,其中边权都是$2^k$的形式$n,m,k<=10^5$,结果对$10^9+7$取模 题解 大佬好厉害 跑一边dij ...

  4. Codeforces 464E The Classic Problem (最短路 + 主席树 + hash)

    题意及思路 这个题加深了我对主席树的理解,是个好题.每次更新某个点的距离时,是以之前对这个点的插入操作形成的线段树为基础,在O(logn)的时间中造出了一颗新的线段树,相比直接创建n颗线段树更省时间. ...

  5. Codeforces 464E The Classic Problem(主席树+最短路+哈希,神仙题)

    题目链接 题意:给出一张 \(n\) 个点 \(m\) 条边的无向图,第 \(i\) 条边连接 \(u_i,v_i\),边权为 \(2^{w_i}\),求 \(s\) 到 \(t\) 的最短路. \( ...

  6. Codeforces 464E #265 (Div. 1) E. The Classic Problem 主席树+Hash

    E. The Classic Problem http://codeforces.com/problemset/problem/464/E 题意:给你一张无向带权图,求S-T的最短路,并输出路径.边权 ...

  7. 把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend

    //把一个序列转换成严格递增序列的最小花费 CF E - Sonya and Problem Wihtout a Legend //dp[i][j]:把第i个数转成第j小的数,最小花费 //此题与po ...

  8. cf 633B A trivial problem

    Mr. Santa asks all the great programmers of the world to solve a trivial problem. He gives them an i ...

  9. 【dp/贪心】CF 780 (Div. 3), problem: (C) Get an Even String

    Problem - C - Codeforces 难度: 1300 input 6 aabbdabdccc zyx aaababbb aabbcc oaoaaaoo bmefbmuyw output ...

随机推荐

  1. Arc083_F Collecting Balls

    传送门 题目大意 给定$N$,在$(1,0),(2,0)......(N,0)$和$(0,1),(0,2)...(0,N)$上都有$1$个机器人,同时给定$2N$个坐标$(x,y),x,y\in[1, ...

  2. 【java规则引擎】简单规则的rete网络示意图

    一个Fact通过Session添加到规则网络中,如何进行规则匹配的大致过程如下 (1)通过根结点对象从EntryPointNode的Map集合中找到相应的EntryPointNode对象 (2)Ent ...

  3. BZOJ4861 [Beijing2017]魔法咒语

    题意 Chandra 是一个魔法天才.从一岁时接受火之教会洗礼之后, Chandra 就显示出对火元素无与伦比的亲和力,轻而易举地学会种种晦涩难解的法术.这也多亏 Chandra 有着常人难以企及的语 ...

  4. es5中foreach的用法

    HTML代码: <p id="result"></p> JS代码: var eleResult = document.getElementById(&quo ...

  5. 如何隐藏掉Nginx的版本号

    最近新学习了一个命令curl,里面有一个参数-I可以查看到网站使用的是哪种服务器,比如: zhangxiaoliudeMacBook-Pro-2:~ zhangxiaoliu$ curl -I htt ...

  6. 蘑菇街 IM 项目 TeamTalk

    源码 https://github.com/mogujie/TeamTalk 试用 http://tt.mogu.io/

  7. GWT实现平滑移动图片效果

    在一些网站的首页上,顶部总会存在一些平滑移动的图片,一般用来投放广告或者业务介绍.多个图片只在一个区域展示,仅通过一些方法来不停的移动这个区域的图片来达到展示多个图片的目的.如果是普通的网页,使用Jq ...

  8. 蓝桥杯 历届试题 PREV-3 带分数

     历届试题 带分数   时间限制:1.0s   内存限制:256.0MB 问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3 ...

  9. Python内置函数:read()

    文章转载于:http://blog.csdn.net/sxingming/article/details/51337768(博主:快递小哥) 1> >>> f=open(r&q ...

  10. MongoTemplate聚合操作

    Aggregation简单来说,就是提供数据统计.分析.分类的方法,这与mapreduce有异曲同工之处,只不过mongodb做了更多的封装与优化,让数据操作更加便捷和易用.Aggregation操作 ...