Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement
今天又在群里看到一个同学问$n$个$n$条边,怎么查询两点直接最短路。看来这种题还挺常见的。
为什么最终答案要从42个点的最短路(到$x,y$)之和,还有$x,y$到$LCA(x,y)$的距离里面取呢?
就是如果走非树边,那么一定要走42个点中的一个,不走树边,就是LCA求了。
我写的太蠢了,还写生成树,看大家都是LCA中的dfs直接标记下就行了。
验证了算法的正确,我又试了试把每条非树边只加一个点,也是AC的,其实想了想,确实正确。
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = 1e5 + ;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct edge{
int to;
ll cost;
friend bool operator < (edge A,edge B){
return A.cost > B.cost;
}
};
int u[maxn], v[maxn], w[maxn], vis[maxn];
vector<int> flag;
ll d[][maxn];
vector<edge> g[maxn];
bool done[maxn];
void dijkstra(ll d[], vector<edge> g[], bool done[], int s){
fill(d,d + maxn, INF);
fill(done, done + maxn, false);
d[s] = ;
priority_queue<edge> q;
q.push((edge){s,});
while(!q.empty()){
edge cur = q.top(); q.pop();
int v = cur.to;
if(done[v]) continue;
done[v] = true;
for(int i = ; i<g[v].size(); i++){
edge e = g[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to] = d[v]+e.cost;
q.push((edge){e.to,d[e.to]});
}
}
}
}
///加边
int cnt, h[maxn];
struct node
{
int to, pre, v;
} e[maxn << ];
void init()
{
cnt = ;
memset(h, , sizeof(h));
}
void add(int from, int to, int v)
{
cnt++;
e[cnt].pre = h[from]; ///5-->3-->1-->0
e[cnt].to = to;
e[cnt].v = v;
h[from] = cnt;
}
///LCA
ll dist[maxn];
int dep[maxn];
int anc[maxn][]; ///2分的父亲节点
void dfs(int u, int fa)
{
for(int i = h[u]; i; i = e[i].pre)
{
int v = e[i].to;
if(v == fa) continue;
dist[v] = dist[u] + e[i].v;
dep[v] = dep[u] + ;
anc[v][] = u;
dfs(v, u);
}
}
void LCA_init(int n)
{
for(int j = ; ( << j) < n; j++)
for(int i = ; i <= n; i++) if(anc[i][j-])
anc[i][j] = anc[anc[i][j-]][j-];
}
int LCA(int u, int v)
{
int log;
if(dep[u] < dep[v]) swap(u, v);
for(log = ; ( << log) < dep[u]; log++);
for(int i = log; i >= ; i--)
if(dep[u] - ( << i) >= dep[v]) u = anc[u][i];
if(u == v) return u;
for(int i = log; i >= ; i--)
if(anc[u][i] && anc[u][i] != anc[v][i])
u = anc[u][i], v = anc[v][i];
return anc[u][];
} int pre[maxn];
int Find(int x)
{
if(x == pre[x]) return x;
else return pre[x] = Find(pre[x]);
}
int main()
{
int n, m; scanf("%d %d", &n, &m);
for(int i = ; i <= m; i++)
{
scanf("%d %d %d", &u[i], &v[i], &w[i]);
g[u[i]].push_back({v[i], w[i]});
g[v[i]].push_back({u[i], w[i]});
}
///先找生成树
for(int i = ; i <= n; i++) pre[i] = i;
for(int i = ; i <= m; i++)
{
int x = Find(u[i]);
int y = Find(v[i]);
if(x != y)
{
pre[x] = y;
}
else flag.push_back(u[i]), flag.push_back(v[i]), vis[i] = ;
}
///对至多42个点跑最短路
sort(flag.begin(), flag.end());
flag.erase(unique(flag.begin(), flag.end()), flag.end());
for(int i = ; i < flag.size(); i++)
{
dijkstra(d[i], g, done, flag[i]);
}
///跑LCA
init();
for(int i = ; i <= m; i++)
{
if(!vis[i]) add(u[i], v[i], w[i]), add(v[i], u[i], w[i]);
}
dist[] = ;
dfs(, );
LCA_init(n);
int Q; scanf("%d", &Q);
///查询
while(Q--)
{
int x, y; scanf("%d %d", &x, &y);
ll ans = dist[x] + dist[y] - 2LL * dist[LCA(x, y)];
for(int i = ; i < flag.size(); i++)
{
ans = min(ans, d[i][x] + d[i][y]);
}
printf("%lld\n", ans);
}
return ;
}
Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement的更多相关文章
- 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) F - The Shortest Statement 倍增LCA + 最短路
F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...
- Educational Codeforces Round 51 (Rated for Div. 2)
做了四个题.. A. Vasya And Password 直接特判即可,,为啥泥萌都说难写,,,, 这个子串实际上是忽悠人的,因为每次改一个字符就可以 我靠我居然被hack了???? %……& ...
- 【 Educational Codeforces Round 51 (Rated for Div. 2) F】The Shortest Statement
[链接] 我是链接,点我呀:) [题意] [题解] 先处理出来任意一棵树. 然后把不是树上的边处理出来 对于每一条非树边的点(最多21*2个点) 在原图上,做dijkstra 这样就能处理出来这些非树 ...
- 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 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
随机推荐
- 理解GloVe模型(Global vectors for word representation)
理解GloVe模型 概述 模型目标:进行词的向量化表示,使得向量之间尽可能多地蕴含语义和语法的信息.输入:语料库输出:词向量方法概述:首先基于语料库构建词的共现矩阵,然后基于共现矩阵和GloVe模型学 ...
- Flask——基础知识
Flask应用程序 一个简单的Flask应用程序 # 导入flask程序 from flask import Flask # 初始化flask对象 app = Flask(__name__) # 装饰 ...
- MySQL中数组的存储
1. MySQL中以字符串的形式存储数组 MySQL中无数组类型,通常将数组元素按某个字符分割以字符串形式存储 1.1. 求数组中元素的个数 方法:按指定符号分割字符串,返回分割后的元素个数.方法很简 ...
- golang 实现冒泡排序
package main import ( "fmt" ) func main(){ a := [...] int{2,5,9,6,8} fmt.Println(a) num := ...
- linux 04 CentOS安装
今天在Vmware上安装了CentOS6.5系统,下午首先把书上的安装过程看了一遍,实际进行操作时有些步骤不一样,经过查资料成功安装,说一下收获.选择自定义安装虚拟机,首先创建空白虚拟机,稍后编辑虚拟 ...
- python--进程内容补充
一. 进程的其他方法 进程id, 进程名字, 查看进程是否活着(is_alive()), terminate()发送结束进程的信号 import time import os from multipr ...
- 使用selenium和phantomJS浏览器获取网页内容的小演示
# 使用selenium和phantomJS浏览器获取网页内容的小演示 # 导入包 from selenium import webdriver # 使用selenium库里的webdriver方法调 ...
- (原)剑指offer之位运算
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: count为1的位数,初始为零 每次右移一为,与1做与运算,结果不为零说明最后一位为1 c++代码如下 in ...
- PAT Basic 1017
1017 A除以B(20 分) 本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立. 输入格式: 输入在一 ...
- 爬虫cookie
Cookie Cookie 是指某些网站服务器为了辨别用户身份和进行Session跟踪,而储存在用户浏览器上的文本文件,Cookie可以保持登录信息到用户下次与服务器的会话. Cookie原理 HTT ...