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> ...
随机推荐
- Mac 安装多个python环境
1.安装Homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ ...
- mysql忘记root密码的处理方式
1.停用mysql服务 service mysqld stop 2.修改my.cnf 利用vim命令打开mysql配置文件my.cnf 添加skip-grant-tables,添加完成后,执行w ...
- java.util.Map
map时key/value形式存储信息的,键可以为对象null public static void main(String[] args) { Map<String, String> m ...
- 【mongo】登陆报错
今天登陆mongo时出现了错误 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt fa ...
- web----框架基础
Web框架本质: 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 真实开发中的python web程序来说,一般会分为两部分:服务器 ...
- python+selenium六:等待相关
显式等待(sleep): 固定的等待(死等),不管页面有没有加载完,都等设置的时间过了再做下一步操作 隐式等待 全局生效,只写一次即可(仅当前页面),缺点:如果页面一直转圈,如:js出错将等待到所设置 ...
- Java列表、数组、字符串
列表(list) list中添加,获取,删除元素 添加方法是:.add(e): 获取方法是:.get(index): 删除方法是:.remove(index), 按照索引删除: .remove(Obj ...
- 网页异步加载之AJAX理解
AJAX AJAX介绍 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 是一种用于创建快速动态网页的技术 AJAX ...
- 步步为营-11-List<T>泛型的简单练习
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 向集合中添加自定义类型--建议在自定义类型的时候要重写equals方法
package com.bjpowernode.t01list; import java.util.ArrayList; /* * 向集合中添加自定义类型 */public class TestLis ...