City Driving

题目连接:

http://codeforces.com/gym/100015/attachments

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<bits/stdc++.h>
using namespace std;
#define maxn 100005
struct node
{
int x,y;
};
vector<node>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<maxn;i++)
G[i].clear();
}
int Find(int i)
{
if(i!=parent[i])
parent[i]=Find(parent[i]);
return parent[i];
}
void Union(int i,int j)
{
int x,y;
x=Find(i);
y=Find(j);
if(x!=y)
parent[x]=y;
} void dfs3(int u,int dist)
{
int i,j;
vis[u]=1;
dis[u]=dist;
pos[u]=cnt;
res[si]=u;
dp[0][cnt++]=si++;
for(i=0;i<G[u].size();i++)
{
j=G[u][i].x;
if(!vis[j])
{
dfs3(j,dist+G[u][i].y);
dp[0][cnt++]=dp[0][pos[u]];
}
}
}
void rmq()
{
int i,j,k;
for(i=1;(1<<i)<=n;i++)
for(j=n-1;j>=0;j--)
{
k=(1<<(i-1));
dp[i][j]=dp[i-1][j];
if(k+j<n)
dp[i][j]=min(dp[i][j],dp[i-1][j+k]);
}
}
int cal(int i,int j)
{
int k;
if(i<j)swap(i,j);
k=0;
while((1<<k)<=(i-j+1))
k++;
k--;
k=min(dp[k][j],dp[k][i-(1<<k)+1]);
return res[k];
}
int Dis(int u,int v)
{
int k = cal(pos[u],pos[v]);
return dis[u]+dis[v]-dis[k]*2;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
init();
for(int i=0;i<=n;i++)
parent[i]=i;
for(int i=1;i<=n;i++)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
x++,y++;
int p = Find(x),q = Find(y);
if(p==q){
qx = x,qy = y,qv = z;
continue;
}
Union(x,y);
G[x].push_back((node){y,z});
G[y].push_back((node){x,z});
}
for(int i=1;i<=n;i++)
if(!vis[i])
dfs3(i,0);
n=n*2-1;
rmq();
int q;
scanf("%d",&q);
while(q--)
{
int x,y;scanf("%d%d",&x,&y);
x++,y++;
int res = Dis(x,y);
res = min(res,Dis(x,qx)+Dis(y,qy)+qv);
res = min(res,Dis(x,qy)+Dis(y,qx)+qv);
printf("%d\n",res);
}
}
}

Codeforces Gym 100015C City Driving 离线LCA的更多相关文章

  1. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  2. codeforces gym #101161E - ACM Tax(lca+主席树)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 ...

  3. Codeforces Gym 100650B Countdown (离线)

    题目链接:http://codeforces.com/gym/100650 根据给出的树和d,求出一些结点,这些结点形成子树的第d层结点数应该尽量多,具体要求可以参考题目. dfs一个结点前保存询问深 ...

  4. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  5. HDU 5044 离线LCA算法

    昨天写了HDU 3966 ,本来这道题是很好解得,结果我想用离线LCA 耍一把,结果发现离线LCA 没理解透,错了好多遍,终得AC ,这题比起 HDU 3966要简单,因为他不用动态查询.但是我还是错 ...

  6. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  7. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  8. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  9. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

随机推荐

  1. JVM 性能调优实战之:一次系统性能瓶颈的寻找过程

    玩过性能优化的朋友都清楚,性能优化的关键并不在于怎么进行优化,而在于怎么找到当前系统的性能瓶颈.性能优化分为好几个层次,比如系统层次.算法层次.代码层次…JVM 的性能优化被认为是底层优化,门槛较高, ...

  2. 为网站添加网址图标favicon.ico

    今天终于有时间把domety的图标设计好,并显示在了网站地址前面.如果你还不知道怎么把自己的图标放到网站上,今天DDBug就和你分享一下实现方法. 制作图标 首先是准备一张ico图标,你可以从网上搜索 ...

  3. 记录一次cefsharp1输入法在win7下异常解决定位

    最近几天都被基于cefSharp封装的浏览器控件搞疯了!对于cefSharp基本满足当前所做项目的需求,但是有一个问题一直困扰我,那就是系统中偶尔会出现输入法不能转换到中文.而且这个问题似乎没有什么规 ...

  4. Macbook pro内存升级

    http://support.apple.com/kb/HT1270?viewlocale=zh_CN&locale=zh_CN#link1 https://support.apple.com ...

  5. Oracle 取两个表中数据的交集并集差异集合

    Oracle 取两个表中数据的交集 关键字: Oracle 取两个表中数据的交集 INTERSECT Oracle 作为一个大型的关系数据库,日常应用中往往需要提取两个表的交集数据 例如现有如下表,要 ...

  6. Epic - Seed Number

    Find the seed of a number. Eg : 1716 = 143*1*4*3 =1716 so 143 is the seed of 1716. find all possible ...

  7. 动手动脑之查看String.equals()方法的实现代码及解释

    动手动脑 请查看String.equals()方法的实现代码,注意学习其实现方法. 第一个是false,后三个是true. package stringtest; public class Strin ...

  8. VS常用技巧

    VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 C ...

  9. 从四大音乐APP首页设计对比分析产品方向

    原帖:http://www.ui.cn/detail/63201.html 本文章中作者例举四个音乐APP应用:虾米.网易.百度.QQ首页 1. 推荐内容:作者将四个首页界面划分出官方推荐与个性化推荐 ...

  10. Cloudera Manager5安装总结遇到问题及解决办法

    安装过程中,由于网络终端,导致下面问题: 问题1:安装停止在获取安装锁/tmp/scm_prepare_node.tYlmPfrT using SSH_CLIENT to get the SCM ho ...