CF 1051F
题意:给定一张n个点,m条边的无向联通图,其中m-n<=20,共q次询问,每次询问求给定两点u,v间的最短路长度
第一眼看见这题的时候,以为有什么神奇的全图最短路算法,满心欢喜的去翻了题解,发现就四个字“树上套环”!
其实这题的提示很明显:m-n<=20!
这说明,如果我们对这个图做一次生成树,那么非树边最多只会有20条!
那么,我们在求任意两点间最短路时,可以分类讨论进行:
①:如果这两点间的最短路只经过树边,那么我们可以直接在树上预处理,利用lca(树上两点距离公式)
②:如果这两点间的最短路会经过非树边,那么由于非树边只有20条,所以产生非树边的点最多只有40个,那这样的话我们可以枚举所有有非树边的点,对全图求最短路,然后在每次询问时枚举每个有非树边的点,每找出一个有非树边的点就去求一遍最短路,最后对找出的所有结果求出最小值即可。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#define ll long long
using namespace std;
struct Edge
{
int next;
int to;
ll val;
}edge[];
bool used[];
int num[];
ll dis[][];
int que[];
struct node
{
int lx,rx;
}e[];
struct tt
{
int p;
ll v;
};
bool operator < (tt a,tt b)
{
return a.v>b.v;
}
int head[];
bool vis[];
int deep[];
int cnt=;
int n,m;
void init()
{
memset(head,-,sizeof(head));
memset(dis,0x3f,sizeof(dis));
cnt=;
}
void add(int l,int r,ll w)
{
edge[cnt].next=head[l];
edge[cnt].to=r;
edge[cnt].val=w;
head[l]=cnt++;
}
ll dep[];
int f[][];
void dfs(int x,int fx)
{
deep[x]=deep[fx]+;
f[x][]=fx;
for(int i=head[x];i!=-;i=edge[i].next)
{
int to=edge[i].to;
if(to==fx)
{
continue;
}
if(f[to][])
{
continue;
}
dep[to]=dep[x]+edge[i].val;
dfs(to,x);
}
}
void getf()
{
for(int i=;i<=;i++)
{
for(int j=;j<=n;j++)
{
f[j][i]=f[f[j][i-]][i-];
}
}
}
inline int read()
{
int f=,x=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void diji(int rt,int typ)
{
memset(vis,,sizeof(vis));
dis[rt][typ]=;
priority_queue <tt> M;
tt s;
s.p=rt;
s.v=;
M.push(s);
while(!M.empty())
{
tt uu=M.top();
M.pop();
int u=uu.p;
if(vis[u])
{
continue;
}
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int to=edge[i].to;
if(vis[to])
{
continue;
}
if(dis[to][typ]>dis[u][typ]+edge[i].val)
{
dis[to][typ]=dis[u][typ]+edge[i].val;
tt temp;
temp.p=to;
temp.v=dis[to][typ];
M.push(temp);
}
}
}
}
int LCA(int x,int y)
{
if(deep[x]>deep[y])
{
swap(x,y);
}
for(int i=;i>=;i--)
{
if(deep[f[y][i]]>=deep[x])
{
y=f[y][i];
}
}
if(x==y)
{
return x;
}
int ret;
for(int i=;i>=;i--)
{
if(f[x][i]!=f[y][i])
{
x=f[x][i];
y=f[y][i];
}else
{
ret=f[x][i];
}
}
return ret;
}
int main()
{
n=read(),m=read();
init();
for(int i=;i<=m;i++)
{
int x=read(),y=read(),z=read();
add(x,y,(ll)z);
add(y,x,(ll)z);
e[i].lx=x;
e[i].rx=y;
}
dfs(,);
getf();
for(int i=;i<=m;i++)
{
if(f[e[i].lx][]!=e[i].rx&&f[e[i].rx][]!=e[i].lx)
{
used[e[i].lx]=;
used[e[i].rx]=;
}
}
int cct=;
for(int i=;i<=n;i++)
{
if(used[i])
{
que[++cct]=i;;
diji(i,cct);
}
}
int q=read();
for(int i=;i<=q;i++)
{
int x=read(),y=read();
int f1=LCA(x,y);
ll ret=dep[x]+dep[y]-*dep[f1];
for(int j=;j<=cct;j++)
{
ret=min(ret,dis[x][j]+dis[y][j]);
}
printf("%lld\n",ret);
}
return ;
}
CF 1051F的更多相关文章
- cf 1051F 树+图
$des$给定一张 $n$ 个点 $m$ 条边的带权无向联通图,$q$ 次询问,每次询问 $u_i$ 到 $v_i$ 的最短路长度.$n,q <= 10^5, m - n <= 20$ $ ...
- Codeforces 1051E Vasya and Big Integers&1051F The Shortest Statement
1051E. Vasya and Big Integers 题意 给出三个大整数\(a,l,r\),定义\(a\)的一种合法的拆分为把\(a\)表示成若干个字符串首位相连,且每个字符串的大小在\(l, ...
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
随机推荐
- #6279. 数列分块入门 3(询问区间内小于某个值 xx 的前驱(比其小的最大元素))
题目链接:https://loj.ac/problem/6279 题目大意:中文题目 具体思路:按照上一个题的模板改就行了,但是注意在整块查找的时候的下标问题. AC代码: #include<b ...
- 【Math for ML】解析几何(Analytic Geometry)
I. 范数(Norm) 定义: 向量空间\(V\)上的范数(norm)是如下函数: \[ \begin{align} \|·\|:V→R, \notag \\ x→\|x\| \notag \end{ ...
- Oracle Instance and Database
- Python3-进程池与线程池
进程池与线程池 在刚开始学多进程或多线程时,我们迫不及待地基于多进程或多线程实现并发的套接字通信,然而这种实现方式的致命缺陷是:服务的开启的进程数或线程数都会随着并发的客户端数目地增多而增多,这会对服 ...
- Spring Boot默认Initializer(1)——ConfigurationWarningsApplicationContextInitializer
ConfigurationWarningsApplicationContextInitializer的作用是用来报告Spring容器的一些常见的错误配置的.这个类中定义了两个内部类: 1. 定义了一个 ...
- dubbo源码分析4——SPI机制_ExtensionFactory类的作用
ExtensionFactory的源码: @SPI public interface ExtensionFactory { /** * Get extension. * * @param type o ...
- HTTP协议07-通用首部字段
通用首部字段 通用首部字段是指,请求报文和响应报文双方都会使用的首部. 1)Cache-Control 通过指定首部字段Cache-Control的指令,就能操作缓存的工作机制. 指令的参数可以多选, ...
- 题解-PKUWC2018 随机算法
Problem loj2540 题意简述:给定\(n\)个点的无向图,给定求最大独立集的近似算法:随机排列\(1\cdots n\),按照该排列顺序贪心构造最大独立集(即对每个点能加入独立集就加),求 ...
- codeforces 416div.2
A CodeForces 811A Vladik and Courtesy B CodeForces 811B Vladik and Complicated Book C CodeFo ...
- nginx指定文件路径有两种方式root和alias
背景 一直没明白这个配置啥意思,反正凑合用吧,不过老凑合总不是个事,没搞明白更容易忘,别人问还答不上来.反正也很简单,就搞明白点记下来. 知识点 root实例: location ^~ /t/ { r ...