博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Gym 100015C City Driving 离线LCA
阅读量:7078 次
发布时间:2019-06-28

本文共 2544 字,大约阅读时间需要 8 分钟。

City Driving

题目连接:

Description

You recently started frequenting San Francisco in your free time and realized that driving in the city is a

huge pain. There are only N locations in the city that interest you, though, and you have decidedtotry
to improve your driving experience. Since you lack a GPS and cannot remember too many di!erent routes,
you wrote down the directions and how long it takes to get between N di!erent pairs of locations (the same
in both directions), ensuring that using only these paths you can get from any location to any other one.
Now you are planning your trip for the weekend and you need to figureoutthefastestwaytogetbetween
Q pairs of locations in the city using only the routes you have written down.

Input

The input consists of multiple test cases. The first line contains a single integer N,3 ! N ! 100,000, the

number of locations of interest and the number of routes you wrotedown.Thenext N lines each contain
three integers u, v,and w (1 ! w ! 1,000), indicating that you have directions from location u to location v
and vice-versa (0-indexed) which take w time. The following line contains a single integer Q,1 ! Q ! 10,000,
the number of pairs of locations you need to compute the travel timefor. Thenext Q lines each contain
two integers u and v, indicating that you should find the minimum time to get from location u to location

v. The input terminates with a line with N = 0. For example:

Output

For each test case, print out Q lines, one for each pair of locations u and v you are finding the fastest routes

for. Each line should simply contain the minimum time it takes to travel from location u to location v.For
example, the correct output for the sample input above would be:

Sample Input

7

0 1 2

0 2 3

1 3 2

2 3 8

2 4 3

3 5 1

1 6 7

3

4 5

0 6

1 2

0

Sample Output

11

9

5

Hint

题意

给你一个n环n边的图,问你两点之间的最短路

题解:

随便找一个环,然后在这个环上随便去掉一条边,然后就比较这个树上的距离小,还是经过这条边的饿距离小

比如你去掉的边是a,b,边权是w,你查询u,v

那么你比较dis(u,v),dis(u,a)+w+dis(b,v),dis(u,b)+w+dis(a,u)就好了

找环上的边,我推荐用并查集

代码

#include
using namespace std;#define maxn 100005struct node{ int x,y;};vector
G[maxn];int dp[18][maxn*2],dis[maxn],parent[maxn],vis[maxn],pos[maxn],res[maxn];int n,m,c,num,cnt,si;int qx=0,qy=0,qv=0;void init(){ qx = qy = qv = 0; cnt = num = si = 0; memset(dp,0,sizeof(dp)); memset(dis,0,sizeof(dis)); memset(vis,0,sizeof(vis)); memset(res,0,sizeof(res)); memset(pos,0,sizeof(pos)); for(int i=0;i
=0;j--) { k=(1<<(i-1)); dp[i][j]=dp[i-1][j]; if(k+j

转载地址:http://vldml.baihongyu.com/

你可能感兴趣的文章
驾考宝典排行榜之爬虫接口解决方案
查看>>
linux日志(常用命令)
查看>>
history
查看>>
Leetcode: Arranging Coins
查看>>
HttpUtil 【判断网络连接的封装类】
查看>>
【转】TCP分段与IP分片
查看>>
iOS 多线程 NSOperation、NSOperationQueue
查看>>
delphi执行查询语句时的进度条怎么做
查看>>
CF 335A(Banana-贪心-priority_queue是大根堆)
查看>>
python的memcache使用如果对key设置了一个int型
查看>>
Leetcode: Longest Substring with At Most Two Distinct Characters
查看>>
173. Binary Search Tree Iterator
查看>>
[python基础知识]python内置函数map/reduce/filter
查看>>
基因家族收缩和扩张分析 & Selective loss pathway & 泛基因组
查看>>
HDU2089 ------不要62(数位dp)
查看>>
hdu4756 Install Air Conditioning(MST + 树形DP)
查看>>
windows7安装redis过程
查看>>
css 多行文本溢出省略号显示
查看>>
每日源码分析 - lodash(slice.js)
查看>>
antd的sider与router4结合, 后退刷新,菜单高亮
查看>>