Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路
emmm, 比赛的时候没有想到如何利用非树边。
其实感觉很简单。。
对于一个询问答案分为两部分求:
第一部分:只经过树边,用倍增就能求出来啦。
第二部分:经过至少一条非树边, 如果经过一个树边那么必定经过其两个端点,暴力的求出这些端点为起始点的最短路。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, m, tot, cnt, head[N], f[N][], d[N], depth[N];
LL cost[N][], dis[][N];
bool vis[N]; struct Edge {
int from, to, w, nx;
} edge[N << ]; void add(int u, int v, int w) {
edge[tot].from = u;
edge[tot].to = v;
edge[tot].w = w;
edge[tot].nx = head[u];
head[u] = tot++;
} void Dij(int S, LL d[N]) {
memset(d, INF, N * sizeof(LL));
priority_queue<PLI, vector<PLI>, greater<PLI> > que;
d[S] = ; que.push(mk(, S)); while(!que.empty()) {
int u = que.top().se;
LL dis = que.top().fi; que.pop();
if(dis > d[u]) continue;
for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].to, w = edge[i].w;
if(dis + w < d[v]) {
d[v] = dis + w;
que.push(mk(d[v], v));
}
}
}
} void dfs(int u, int fa, int w, int deep) {
vis[u] = true; depth[u] = deep;
f[u][] = fa; cost[u][] = w;
for(int j = ; j < ; j++) {
f[u][j] = f[f[u][j-]][j-];
cost[u][j] = cost[f[u][j-]][j-] + cost[u][j-];
}
for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].to;
if(v == fa) continue;
else if(vis[v]) {
d[cnt++] = u; d[cnt++] = v;
} else dfs(v, u, edge[i].w, deep + );
}
} int getLCA (int u, int v){
if(depth[u] < depth[v]) swap(u, v);
for(int j = ; j >= ; j--)
if(depth[u] - depth[v] >= ( << j))
u = f[u][j]; for(int j = ; j >= ; j--)
if(f[u][j] != f[v][j])
u = f[u][j], v = f[v][j];
return u == v ? u : f[u][];
} LL getDis(int u, int v) {
LL ans = ;
for(int j = ; j >= ; j--)
if(depth[u] - depth[v] >= ( << j))
ans += cost[u][j], u = f[u][j];
return ans;
} int main() {
memset(head, -, sizeof(head));
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w); add(v, u, w);
}
dfs(, , , );
sort(d, d + cnt);
cnt = unique(d, d + cnt) - d; for(int i = ; i < cnt; i++) Dij(d[i], dis[i]); int q; scanf("%d", &q);
while(q--) {
int u, v, lca;
scanf("%d%d", &u, &v);
lca = getLCA(u, v);
LL ans = getDis(u, lca) + getDis(v, lca);
for(int i = ; i < cnt; i++) ans = min(ans, dis[i][u] + dis[i][v]);
printf("%lld\n", ans);
}
return ;
} /*
*/
Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路的更多相关文章
- 【 Educational Codeforces Round 51 (Rated for Div. 2) F】The Shortest Statement
[链接] 我是链接,点我呀:) [题意] [题解] 先处理出来任意一棵树. 然后把不是树上的边处理出来 对于每一条非树边的点(最多21*2个点) 在原图上,做dijkstra 这样就能处理出来这些非树 ...
- 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 ...
- Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- Educational Codeforces Round 51 (Rated for Div. 2)
做了四个题.. A. Vasya And Password 直接特判即可,,为啥泥萌都说难写,,,, 这个子串实际上是忽悠人的,因为每次改一个字符就可以 我靠我居然被hack了???? %……& ...
- Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement
题目链接:The Shortest Statement 今天又在群里看到一个同学问$n$个$n$条边,怎么查询两点直接最短路.看来这种题还挺常见的. 为什么最终答案要从42个点的最短路(到$x,y$) ...
- CodeForces Educational Codeforces Round 51 (Rated for Div. 2)
A:Vasya And Password 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen(&q ...
- The Shortest Statement(Educational Codeforces Round 51 (Rated for Div.2)+最短路+LCA+最小生成树)
题目链接 传送门 题面 题意 给你一张有\(n\)个点\(m\)条边的联通图(其中\(m\leq n+20)\),\(q\)次查询,每次询问\(u\)与\(v\)之间的最短路. 思路 由于边数最多只比 ...
- Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)
题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...
随机推荐
- centos7 配置 yum 安装的 jdk
yum 安装的 java,jdk 路径默认是 /usr/lib/jvm/java-* 我们修改 .bash_profile 文件加上下面几行: export JAVA_HOME=/usr/lib/jv ...
- 图片虚拟目录--即图片保存在window硬盘上面
这个是图片保存在电脑的硬盘上面的图片上传设置,既不是在web工程中,也不是在专门的图片服务器中,下面是配置方法: r 这里的Document base 我们这里设置为F:\images 如果在浏览器访 ...
- STL源码分析-traits
http://note.youdao.com/noteshare?id=b5fd9f936cd133af3790a8b0e9c35b8a
- Python-- Redis Set
一.无序集合 Set操作,Set集合就是不允许重复的列表 1.1 sadd(name, values) # name对应的集合中添加元素 1.2 smembers(name) # 获取name对应的集 ...
- 前端PHP入门-021-重点日期函数之日期验证函数
checkdate可以判断一个输出的日期是否有效. 在实际的工作中,我们需要经常用于检测常用于用户提交表单的数据验证. 函数的语法格式如下: bool checkdate ( int month,in ...
- 树dp...吧 ZOJ 3949
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...
- Fire Net(深度优先搜索)
ZOJ Problem Set - 1002 Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we ha ...
- Linux网络知识
在思科上面模拟一下数据包的传递过程:一般上网使用的协议是tcp 交换机是一个2层的设备,它和Ip地址是没有关系的. 交换机上主要处理的是硬件地址(MAC),它只能分析到硬件地址,再到IP地址它就不管了 ...
- Flex布局(伸缩盒布局)
Flexible Box是什么?Flexible意为可伸缩的,Box意为盒子,可以理解为一种新式的盒模型——伸缩盒模型.由CSS3规范提出,这是在原有的大家非常熟悉的block, inline-blo ...
- js和jquery中的遍历对象和数组(forEach,map,each)
arr[].forEach(function(value,index,array){ //do something }) 参数:value数组中的当前项,index当前项的索引,array原始数组: ...