Codeforces.1051F.The Shortest Statement(最短路Dijkstra)
先随便建一棵树。
如果两个点(u,v)不经过非树边,它们的dis可以直接算。
如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案。
所以枚举每条非树边的两个端点,求一遍这两个点到所有点的最短路。非树边最多21条,所以要求一遍最短路的点最多42个。
另外对于一条边的两个点只求一个就好了。因为要用这条非树边的话它们两个都要经过。
//779ms 28900KB
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define mp std::make_pair
#define pr std::pair<LL,int>
//#define gc() getchar()
#define MAXIN 250000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e5+5;
int Enum,H[N],nxt[N<<1],to[N<<1],len[N<<1],fa[N],dep[N],sz[N],son[N],top[N],sk[N];
LL dist[N],dis[23][N];
bool upd[N],nottree[N];
std::priority_queue<pr> q;
char IN[MAXIN],*SS=IN,*TT=IN;
#define AE(u,v,w) to[++Enum]=v,nxt[Enum]=H[u],H[u]=Enum,len[Enum]=w,to[++Enum]=u,nxt[Enum]=H[v],H[v]=Enum,len[Enum]=w
inline int read()
{
int now=0; register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline int LCA(int u,int v)
{
while(top[u]!=top[v]) dep[top[u]]>dep[top[v]]?u=fa[top[u]]:v=fa[top[v]];
return dep[u]>dep[v]?v:u;
}
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void DFS1(int x)
{
int mx=0; sz[x]=1;
for(int v,i=H[x]; i; i=nxt[i])
if(!nottree[i] && (v=to[i])!=fa[x])
{
fa[v]=x, dep[v]=dep[x]+1, dist[v]=dist[x]+len[i], DFS1(v), sz[x]+=sz[v];
if(sz[v]>mx) mx=sz[v], son[x]=v;
}
}
void DFS2(int x,int tp)
{
top[x]=tp;
if(son[x])
{
DFS2(son[x],tp);
for(int i=H[x]; i; i=nxt[i])
if(!nottree[i] && to[i]!=fa[x] && to[i]!=son[x]) DFS2(to[i],to[i]);
}
}
void Dijkstra(LL *dis,int s,int n)
{
static bool vis[N];
memset(vis,0,sizeof vis);
memset(dis,0x3f,sizeof(LL)*(n+1));//dis是指针!
dis[s]=0, q.push(mp(0,s));
while(!q.empty())
{
int x=q.top().second; q.pop();
if(vis[x]) continue;
vis[x]=1;
for(int i=H[x],v; i; i=nxt[i])
if(dis[v=to[i]]>dis[x]+len[i]) q.push(mp(-(dis[v]=dis[x]+len[i]),v));
}
}
int main()
{
int n=read(), m=read(); Enum=1;
for(int i=1; i<=n; ++i) fa[i]=i;
int tote=0,cnt=0;
for(int i=1,r1,r2,u,v,w; i<=m; ++i)
{
r1=Find(u=read()), r2=Find(v=read()), w=read();
AE(u,v,w);
if(r1==r2) nottree[Enum]=1, nottree[Enum^1]=1, sk[++tote]=Enum;
else fa[r1]=r2;
}
fa[1]=1, DFS1(1), DFS2(1,1);
for(int i=1; i<=tote; ++i)
{
int e=sk[i];
if(!upd[to[e]]) upd[to[e]]=1, Dijkstra(dis[++cnt],to[e],n);
// if(!upd[to[e^1]]) upd[to[e^1]]=1, Dijkstra(dis[++cnt],to[e^1],n);
}
for(int Q=read(),u,v; Q--; )
{
u=read(),v=read();
LL ans=dist[u]+dist[v]-(dist[LCA(u,v)]<<1ll);
for(int i=1; i<=cnt; ++i)
ans=std::min(ans,dis[i][u]+dis[i][v]);
printf("%I64d\n",ans);
}
return 0;
}
Codeforces.1051F.The Shortest Statement(最短路Dijkstra)的更多相关文章
- codeforces 1051F The Shortest Statement
题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...
- 2018.09.24 codeforces 1051F. The Shortest Statement(dijkstra+lca)
传送门 这真是一道一言难尽的题. 首先比赛的时候居然没想出来正解. 其次赛后调试一直调不出来最后发现是depth传错了. 其实这是一道简单题啊. 对于树边直接lca求距离. 由于非树边最多21条. 因 ...
- [Codeforces 1051F] The Shortest Statement 解题报告(树+最短路)
题目链接: https://codeforces.com/contest/1051/problem/F 题目大意: 给出一张$n$个点,$m$条边的带权无向图,多次询问,每次给出$u,v$,要求输出$ ...
- Codeforces 1051E Vasya and Big Integers&1051F The Shortest Statement
1051E. Vasya and Big Integers 题意 给出三个大整数\(a,l,r\),定义\(a\)的一种合法的拆分为把\(a\)表示成若干个字符串首位相连,且每个字符串的大小在\(l, ...
- Codeforces.567E.President and Roads(最短路 Dijkstra)
题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...
- Codeforces Gym101502 I.Move Between Numbers-最短路(Dijkstra优先队列版和数组版)
I. Move Between Numbers time limit per test 2.0 s memory limit per test 256 MB input standard inpu ...
- Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...
- cf1051F. The Shortest Statement(最短路)
题意 题目链接 题意:给出一张无向图,每次询问两点之间的最短路,满足$m - n <= 20$ $n, m, q \leqslant 10^5$ Sol 非常好的一道题. 首先建出一个dfs树. ...
- The Shortest Statement CodeForces - 1051F(待测试)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...
随机推荐
- 【转载】apache log配置 按日期写日志
指定apache日志每天生成一个文件 Linux系统配置方法 在apache的配置文件httpd.conf中找到 代码如下1 ErrorLog logs/error_log CustomLog log ...
- caffe中使用python定义新的层
转载链接:http://withwsf.github.io/2016/04/14/Caffe-with-Python-Layer/ Caffe通过Boost中的Boost.Python模块来支持使用P ...
- MySQL各个版本区别及问题总结
参考:http://www.admin10000.com/document/62.html 一.简介 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个 ...
- git安装及基础用法
1.安装GitGit-2.9.3-64-bit.exe 2.打开Git Bash,设置用户名,Email $ git config --global user.name "Your Name ...
- React-Native 之 网络请求 fetch
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- 解决Idea运行testng套件无testoutput文件夹问题
说明:testNG的工程我是使用eclipse创建的,直接导入到idea中,运行test时不会生产test-output,只能在idea的控制台中查看运行结果,然后到处报告,经过不懈的百度终于找到怎么 ...
- JetBrains 授权服务器(License Server URLS)
分享几个已经部署好的在线验证服务器:http://idea.iteblog.com/key.php http://idea.imsxm.com/ http://103.207.69.64:1017 h ...
- java 语言的主要特点
java 语言主要特点如下: 1:简单 2:面向对象 3:分布性 4:可移植性 5:安全性 6:健壮性 二:java 主要术语 三:java 核心是面向对象程序设计OOP 四:封装 五:多态 六:继承 ...
- PHP 发送HTTP请求的几种方式
1.curl仍然是最好的HTTP库,没有之一. 可以解决任何复杂的应用场景中的HTTP 请求2. 文件流式的HTTP请求比较适合处理简单的HTTP POST/GET请求,但不适用于复杂的HTTP请求3 ...
- poj2828 伸展树模拟
用伸展树模拟插队比线段树快乐3倍.. 但是pojT了.别的oj可以过,直接贴代码. 每次更新时,找到第pos个人,splay到根,然后作为新root的左子树即可 #include<iostrea ...