题目链接  HDU5636

n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边。

m个询问,每次询问求两个点之前的最短路。

我们把这三条边的6个点两两算最短路,

然后询问的时候用这6个点的距离来更新答案就可以了。

(不过听说好像有更好的方法,先占个坑)

时间复杂度$O(216m)$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const LL mod = 1e9 + 7; int T;
int n, m;
int a[10];
int b[10][10];
int d;
LL ans, cnt; int main(){ scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &m);
rep(i, 1, 6) scanf("%d", a + i);
ans = 0;
cnt = 0; rep(i, 1, 6) rep(j, 1, 6) b[i][j] = abs(a[i] - a[j]); b[1][2] = b[2][1] = 1;
b[3][4] = b[4][3] = 1;
b[5][6] = b[6][5] = 1; rep(k, 1, 6) rep(i, 1, 6) rep(j, 1, 6)
b[i][j] = min(b[i][j], b[i][k] + b[k][j]); for (; m--; ){
scanf("%d%d", &a[7], &a[8]);
d = abs(a[7] - a[8]);
rep(i, 1, 6) rep(j, 1, 6)
d = min(d, abs(a[i] - a[7]) + abs(a[j] - a[8]) + b[i][j]); (ans += (++cnt) * (LL)d) %= mod;
}
printf("%lld\n", ans);
} return 0;
}

HDU 5636 Shortest Path(Floyd)的更多相关文章

  1. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  2. HDU-3631 Shortest Path (floyd)

    Description When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in Informa ...

  3. HDU 5636 Shortest Path 暴力

    Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...

  4. Shortest Path(hdu5636)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  5. HDU 5636 Shortest Path(Floyed,枚举)

    Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...

  6. HDU 5636 Shortest Path

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 题解: 1.暴力枚举: #include<cmath> #include<c ...

  7. HDU 5636 Shortest Path 分治+搜索剪枝

    题意:bc round 74 分析(官方题解): 你可以选择分类讨论, 但是估计可能会写漏一些地方. 只要抽出新增边的端点作为关键点, 建立一个新图, 然后跑一遍floyd就好了. 复杂度大概O(6^ ...

  8. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. HDU ACM 1869 六度分离(Floyd)

    六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. C基础:关于预处理宏定义命令

    为了程序的通用性,可以使用#define预处理宏定义命令,它的具体作用,就是方便程序段的定义和修改. 1.关于预定义替代 #define Conn(x,y) x##y#define ToChar(x) ...

  2. CPP-基础:windows api 多线程---互斥量、信号量、临界值、事件区别

    http://blog.csdn.net/wangsifu2009/article/details/6728155 四种进程或线程同步互斥的控制方法:1.临界区:通过对多线程的串行化来访问公共资源或一 ...

  3. Bootstrap历练实例:基本输入框组

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  4. UVa-227-谜题

    这题的话,我们读入的时候,可以用scanf单个读入字符,也可以用getchar函数来读入. scanf scanf读入串字符的时候,遇到空格.回车和TAB等空白字符就会停止读入,但是如果读入单个字符就 ...

  5. C++实现简易单向链表

    #include <iostream> #include <stdlib.h> #include <stdbool.h> using namespace std; ...

  6. Cookies和Session的区别和理解

    Cookies和Session的区别和理解 cookie机制 Cookies是服务器在本地机器上存储的小段文本并随每一个请求发送至同一个服务器.IETF RFC 2965 HTTP State Man ...

  7. 自定义View画一条线

    #import "PublishContextView.h" @implementation PublishContextView -(void)drawRect:(CGRect) ...

  8. (转)python之禅

    凡是用过 Python的人,基本上都知道在交互式解释器中输入 import this 就会显示 Tim Peters 的 The Zen of Python,但它那偈语般的语句有点令人费解,所以我想分 ...

  9. cf950e Data Center Maintenance

    若推迟 \(u\) 必推迟 \(v\),则连边 <\(u,v\)>. 求强联通分量后缩点,答案显然是出度为 \(0\) 且 size 最小的 scc. #include <iostr ...

  10. BRVAH(让RecyclerView变得更高效) (3)

    本文来自网易云社区 作者:吴思博 3 实现列表加载动画效果    3.1默认动画 我们只需将自建的 adapter 继承它对应满足需求的 Adapter,然后在 Activity 中实例化,通过ope ...