【链接】 我是链接,点我呀:)

【题意】

【题解】

先处理出来任意一棵树。
然后把不是树上的边处理出来
对于每一条非树边的点(最多21*2个点)
在原图上,做dijkstra
这样就能处理出来这些非树边上的点到其他任意点的最短路了。
然后对于询问x,y
先用LCA+预处理,求出树上的最短路。
接下来考虑有非树边的情况。
显然只要枚举它经过了非树边上的点z
那么用dis[z][x]+dis[z][y]尝试更新ans就好。
只要枚举非树边上的点。
这是突破口。

【代码】

  1. #include <bits/stdc++.h>
  2. #define LL long long
  3. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  4. using namespace std;
  5. const int MAXN = 100000+10;
  6. const int MAX = 17;
  7. vector <int> son[MAXN],w[MAXN];
  8. int n,p[MAXN][MAX+5],dep[MAXN],pre[MAX+5],m;
  9. long long dis[MAXN];
  10. bool vis[MAXN+10];
  11. LL F[45][MAXN];
  12. set<int> myset;
  13. set<pair<LL,int> > q;
  14. void dfs(int x,int f)
  15. {
  16. vis[x] = true;
  17. dep[x] = dep[f] + 1;
  18. p[x][0] = f;
  19. for (int i = 1; i <= MAX; i++)
  20. p[x][i] = p[p[x][i - 1]][i - 1];
  21. int len = son[x].size();
  22. for (int i = 0; i <= len - 1; i++)
  23. {
  24. int y = son[x][i];
  25. if (y != f)
  26. {
  27. if (!vis[y]){
  28. dis[y] = dis[x] + w[x][i];
  29. dfs(y, x);
  30. }else{
  31. myset.insert(x);myset.insert(y);
  32. }
  33. }
  34. }
  35. }
  36. long long getMinimalDistance(int t0,int t1){
  37. int pret0 = t0,pret1 = t1;
  38. if (dep[t0] > dep[t1]) swap(t0, t1);
  39. for (int i = MAX; i >= 0; i--)
  40. if (dep[t0] <= dep[t1] - pre[i])
  41. t1 = p[t1][i];
  42. if (t1 == t0) return dis[pret0]+dis[pret1]-2*dis[t0];
  43. for (int i = MAX; i >= 0; i--)
  44. {
  45. if (p[t0][i] == p[t1][i])
  46. continue;
  47. t0 = p[t0][i], t1 = p[t1][i];
  48. }
  49. return dis[pret0]+dis[pret1]-2*dis[p[t0][0]];
  50. }
  51. int main()
  52. {
  53. #ifdef ccy
  54. freopen("rush.txt", "r", stdin);
  55. #endif
  56. pre[0] = 1;
  57. for (int i = 1; i <= MAX; i++)
  58. pre[i] = pre[i - 1] << 1;
  59. int T;
  60. T = 1;
  61. while (T--)
  62. {
  63. scanf("%d%d",&n,&m);
  64. for (int i = 1; i <= n; i++) son[i].clear(),w[i].clear();
  65. myset.clear();
  66. for (int i = 1; i <= m; i++)
  67. {
  68. int x, y, z;
  69. scanf("%d%d%d",&x,&y,&z);
  70. son[x].push_back(y);w[x].push_back(z);
  71. son[y].push_back(x);w[y].push_back(z);
  72. }
  73. dis[1] = 0;
  74. dfs(1, 0);
  75. int p = 0;
  76. for (int s:myset){
  77. p++;
  78. rep1(i,1,MAXN-1) F[p][i] = -1;
  79. F[p][s] = 0;
  80. q.clear();
  81. q.insert({0,s});
  82. while (!q.empty()){
  83. pair<LL,int> temp = (*q.begin());
  84. q.erase(q.begin());
  85. int x = temp.second;LL disx = temp.first;
  86. if (F[p][x]<disx) continue;
  87. rep1(i,0,(int)son[x].size()-1){
  88. int y = son[x][i];LL cost = w[x][i];
  89. if (F[p][y]==-1 || F[p][y]>disx+cost){
  90. F[p][y] = disx+cost;
  91. q.insert({F[p][y],y});
  92. }
  93. }
  94. }
  95. }
  96. int q;
  97. scanf("%d",&q);
  98. rep1(i,1,q)
  99. {
  100. int t0, t1;
  101. scanf("%d%d",&t0,&t1);
  102. long long ans = getMinimalDistance(t0,t1);
  103. rep1(j,1,p){
  104. if (F[j][t0]!=-1 && F[j][t1]!=-1){
  105. ans = min(ans,F[j][t0]+F[j][t1]);
  106. }
  107. }
  108. printf("%lld\n",ans);
  109. }
  110. }
  111. return 0;
  112. }

【 Educational Codeforces Round 51 (Rated for Div. 2) F】The Shortest Statement的更多相关文章

  1. 【Educational Codeforces Round 53 (Rated for Div. 2) C】Vasya and Robot

    [链接] 我是链接,点我呀:) [题意] [题解] 如果|x|+|y|>n 显然.从(0,0)根本就没法到(x,y) 但|x|+|y|<=n还不一定就能到达(x,y) 注意到,你每走一步路 ...

  2. 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然在没有一直往右走然后走到头再往上走一格再往左走到头之前. 肯定是一直在蛇形走位.. 这个蛇形走位的答案贡献可以预处理出来.很容易 ...

  3. 【Educational Codeforces Round 48 (Rated for Div. 2) D】Vasya And The Matrix

    [链接] 我是链接,点我呀:) [题意] 告诉你每一行.每一列的异或和. 让你求出一个符合要求的原矩阵. [题解] 显然应该有 a1^a2^....^an = b1^b2^....^bn 也即两边同时 ...

  4. 【Educational Codeforces Round 41 (Rated for Div. 2) D】Pair Of Lines

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果点的个数<=3 那么直接输出有解. 否则. 假设1,2最后会在一条直线上,则把这条直线上的点都删掉. 看看剩余的点是否在同 ...

  5. Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路

    F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...

  6. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  7. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  8. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  9. CF codeforces A. New Year Garland【Educational Codeforces Round 79 (Rated for Div. 2)】

    A. New Year Garland time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. timeout in asp.net

    Forms authentication timeout vs sessionState timeout They are different things. The Forms Authentica ...

  2. Generic Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/kwtft8ak(v=vs.140).aspx It is often useful to define interf ...

  3. 推荐微软Windows 8 Metro应用开发虚拟实验室

    Kevin Fan分享开发经验,记录开发点滴 推荐微软Windows 8 Metro应用开发虚拟实验室 2012-07-19 05:23 by jv9, 1940 阅读, 4 评论, 收藏, 编辑 微 ...

  4. 一些SQL高级函数

    一些SQL高级函数 Posted on 2010-08-08 21:34 moss_tan_jun 阅读(311) 评论(0) 编辑 收藏 长度与分析用 datalength(Char_expr) 返 ...

  5. [Codeforces Round472C] Three-level Laser

    [题目链接] https://codeforces.com/contest/957/problem/C [算法] 二分 注意精度问题 时间复杂度 :O(NlogN) [代码] #include< ...

  6. MySQL社区版是世界上最流行的开源数据库的免费

    昨天晚上搞了很久,终于搞清楚mysql的安装配置了,我真是太low了.当我在云服务器上登进Mysql时,真是高兴哈哈,咱一步一步来,彻底搞懂Mysql的安装配置. 我的安装环境: 阿里云服务器 1 2 ...

  7. maven的pom.xml文件错误

    来自:http://www.cnblogs.com/shihujiang/p/3492864.html

  8. P4166 [SCOI2007]最大土地面积

    传送门 首先,四边形的四个点肯定都在凸包上(别问我为什么我也不知道,感性理解一下好了) 那么我们可以求出凸包之后\(O(n^4)\)暴力枚举,据说在随机数据下凸包上的点只有\(O(logn)\)个可过 ...

  9. The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈

    题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...

  10. memcache缓存系统

    一.缓存系统 静态web页面: 1.在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服 ...