【 Educational Codeforces Round 51 (Rated for Div. 2) F】The Shortest Statement
【链接】 我是链接,点我呀:)
【题意】
【题解】
先处理出来任意一棵树。
然后把不是树上的边处理出来
对于每一条非树边的点(最多21*2个点)
在原图上,做dijkstra
这样就能处理出来这些非树边上的点到其他任意点的最短路了。
然后对于询问x,y
先用LCA+预处理,求出树上的最短路。
接下来考虑有非树边的情况。
显然只要枚举它经过了非树边上的点z
那么用dis[z][x]+dis[z][y]尝试更新ans就好。
只要枚举非树边上的点。
这是突破口。
【代码】
#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
using namespace std;
const int MAXN = 100000+10;
const int MAX = 17;
vector <int> son[MAXN],w[MAXN];
int n,p[MAXN][MAX+5],dep[MAXN],pre[MAX+5],m;
long long dis[MAXN];
bool vis[MAXN+10];
LL F[45][MAXN];
set<int> myset;
set<pair<LL,int> > q;
void dfs(int x,int f)
{
vis[x] = true;
dep[x] = dep[f] + 1;
p[x][0] = f;
for (int i = 1; i <= MAX; i++)
p[x][i] = p[p[x][i - 1]][i - 1];
int len = son[x].size();
for (int i = 0; i <= len - 1; i++)
{
int y = son[x][i];
if (y != f)
{
if (!vis[y]){
dis[y] = dis[x] + w[x][i];
dfs(y, x);
}else{
myset.insert(x);myset.insert(y);
}
}
}
}
long long getMinimalDistance(int t0,int t1){
int pret0 = t0,pret1 = t1;
if (dep[t0] > dep[t1]) swap(t0, t1);
for (int i = MAX; i >= 0; i--)
if (dep[t0] <= dep[t1] - pre[i])
t1 = p[t1][i];
if (t1 == t0) return dis[pret0]+dis[pret1]-2*dis[t0];
for (int i = MAX; i >= 0; i--)
{
if (p[t0][i] == p[t1][i])
continue;
t0 = p[t0][i], t1 = p[t1][i];
}
return dis[pret0]+dis[pret1]-2*dis[p[t0][0]];
}
int main()
{
#ifdef ccy
freopen("rush.txt", "r", stdin);
#endif
pre[0] = 1;
for (int i = 1; i <= MAX; i++)
pre[i] = pre[i - 1] << 1;
int T;
T = 1;
while (T--)
{
scanf("%d%d",&n,&m);
for (int i = 1; i <= n; i++) son[i].clear(),w[i].clear();
myset.clear();
for (int i = 1; i <= m; i++)
{
int x, y, z;
scanf("%d%d%d",&x,&y,&z);
son[x].push_back(y);w[x].push_back(z);
son[y].push_back(x);w[y].push_back(z);
}
dis[1] = 0;
dfs(1, 0);
int p = 0;
for (int s:myset){
p++;
rep1(i,1,MAXN-1) F[p][i] = -1;
F[p][s] = 0;
q.clear();
q.insert({0,s});
while (!q.empty()){
pair<LL,int> temp = (*q.begin());
q.erase(q.begin());
int x = temp.second;LL disx = temp.first;
if (F[p][x]<disx) continue;
rep1(i,0,(int)son[x].size()-1){
int y = son[x][i];LL cost = w[x][i];
if (F[p][y]==-1 || F[p][y]>disx+cost){
F[p][y] = disx+cost;
q.insert({F[p][y],y});
}
}
}
}
int q;
scanf("%d",&q);
rep1(i,1,q)
{
int t0, t1;
scanf("%d%d",&t0,&t1);
long long ans = getMinimalDistance(t0,t1);
rep1(j,1,p){
if (F[j][t0]!=-1 && F[j][t1]!=-1){
ans = min(ans,F[j][t0]+F[j][t1]);
}
}
printf("%lld\n",ans);
}
}
return 0;
}
【 Educational Codeforces Round 51 (Rated for Div. 2) F】The Shortest Statement的更多相关文章
- 【Educational Codeforces Round 53 (Rated for Div. 2) C】Vasya and Robot
[链接] 我是链接,点我呀:) [题意] [题解] 如果|x|+|y|>n 显然.从(0,0)根本就没法到(x,y) 但|x|+|y|<=n还不一定就能到达(x,y) 注意到,你每走一步路 ...
- 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然在没有一直往右走然后走到头再往上走一格再往左走到头之前. 肯定是一直在蛇形走位.. 这个蛇形走位的答案贡献可以预处理出来.很容易 ...
- 【Educational Codeforces Round 48 (Rated for Div. 2) D】Vasya And The Matrix
[链接] 我是链接,点我呀:) [题意] 告诉你每一行.每一列的异或和. 让你求出一个符合要求的原矩阵. [题解] 显然应该有 a1^a2^....^an = b1^b2^....^bn 也即两边同时 ...
- 【Educational Codeforces Round 41 (Rated for Div. 2) D】Pair Of Lines
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果点的个数<=3 那么直接输出有解. 否则. 假设1,2最后会在一条直线上,则把这条直线上的点都删掉. 看看剩余的点是否在同 ...
- Educational Codeforces Round 51 (Rated for Div. 2) F - The Shortest Statement 倍增LCA + 最短路
F - The Shortest Statement emmm, 比赛的时候没有想到如何利用非树边. 其实感觉很简单.. 对于一个询问答案分为两部分求: 第一部分:只经过树边,用倍增就能求出来啦. 第 ...
- Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
- 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 38 (Rated for Div. 2)】 Problem A-D 题解
[比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B Run for your prize[贪心] ...
- 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 ...
随机推荐
- 全面解析布局(Grid & Canvas &StackPanel &Wrappanel) 转
写这篇文章前,特意在百度搜索了一下,发现目前网上介绍布局的文章不多,质量也不是很高.拿grid和canvas来讲,这两个布局容器还是有许多小细节值得讲的,如果你不了解的话,开发中经常会遇到一些让人匪夷 ...
- 在Sql Server触发器中判断操作是Insert还是Update还是Delete
在Sql Server触发器中判断操作是Insert还是Update还是Delete DECLARE @IsInsert bit, @IsUpdate bit, @IsDelete ...
- /lib/dracut/hooks/shutdown/30-dm-shutdown.sh
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVQAAAMACAIAAABEqXuoAAAgAElEQVR4nOydPWjryOK3VaZM8RYpU2 ...
- E20170925-hm
arc n. 综合症状; 弧(度); 天穹; 电弧,弧光.; vi. 形成拱状物; 循弧线行进; wrap vt. 包; 缠绕; 用…包裹(或包扎.覆盖等); 掩护; n. ...
- 禁用backspace网页回退功能
<script language="JavaScript">document.onkeydown = check;function check(e) { var cod ...
- [App Store Connect帮助]一、 App Store Connect 使用入门(4)iOS 版 App Store Connect
通过 iOS 版 App Store Connect,您可以在移动设备上查看销售数据.App 元数据和顾客评论.您还可以检查 App 状态.发布您 App 的新版本并回应“Resolution Cen ...
- $P5240 Derivation$
神仙题. 第一场月赛的题目我到第二场月赛完了才写[由此可见我是真的菜 题目就是个大模拟加乘显然,幂的话需要将原函数.导函数的函数值用扩展欧拉定理展开 \(log\) 层.时间复杂度 \(O(T |S| ...
- 《Typecript 入门教程》 1、类
类 使用class + 类名 即可定义一个类,一个类中通常有3个成员:属性.构造函数.方法: 在类内部引用属性或方法事使用this调用,它表示我们访问的是类的成员. 我们使用new构造了Greeter ...
- 利用AXIS2传递JSON数据
Axis2是目前比较流行的WebService引擎.WebService被应用在很多不同的场景.例如,可以使用WebService来发布服务端 Java类的方法,以便使用不同的客户端进行调用.这样可以 ...
- Intent的调用
//Intent intent=new Intent();//intent.setClass(MainActivity.this, GPSService.class);//以上二条可以合并成如下一条 ...