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);}}
}

转载于:https://www.cnblogs.com/qscqesze/p/5136261.html

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 100650B Countdown (离线)

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

  3. [蓝桥杯][2018年第九届真题]版本分支(离线LCA模板)

    题目描述 小明负责维护公司一个奇怪的项目.这个项目的代码一直在不断分支(branch)但是从未发生过合并(merge). 现在这个项目的代码一共有N个版本,编号1~N,其中1号版本是最初的版本. 除了 ...

  4. Codeforces Gym 101173 CERC 16 D BZOJ 4790 Dancing Disks

    Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks 强烈安利这道构造题目,非常有意思. 这里用到的思想是归并排序! 多路归并排序 ...

  5. Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)

    Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven) 题目来源: Codeforces 题意: 给出一些比赛, ...

  6. [Codeforces Gym 101651/100725B] Banal Tickets

    Codeforces Gym 100725 题解: 先分两种情况, 积为000与积非0" role="presentation" style="position ...

  7. Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)

    题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, -, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...

  8. Gym 101142G Gangsters in Central City【思维+Lca】

    Gangsters in Central City 题意: 给一棵树,叶子节点为房子,q次操作,节点1为根节点(蓄水池)向房子供水 每次操作有两种类型,+ x 强盗占领了编号为x的房子,- x强盗离开 ...

  9. codeforces gym 101142G Gangsters in Central City

    简略题意:一棵树,每个节点有一个局面,根是水源,边是水管.初始每个居民都有水喝. 操作有两种: '+ v', v处的居民楼被强盗占领. '- v', v处的强盗走了. 对于每个询问,你需要切断一些水管 ...

  10. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

最新文章

  1. Python 技术篇-用PIL库修改图片透明度实例演示,改变png图片色道为RGBA、RGB
  2. fileUpload 文件上传
  3. 微软职位内部推荐-Software Engineer II-Web app
  4. 简述对linux系统的认识,对Linux的认识
  5. TeamViewer中一些按钮的功能
  6. Mac版IntelliJ IDEA上手的一些必要设置
  7. html 两个表合并,SQL中将两个表合并成一个新表
  8. 1.MySql驱动的jar包下载
  9. 【Python】使用Labelme标注自己的数据集并由json生成Ground Truth
  10. android 获取录音时长_Android、iOS录音时音量大小计算
  11. 运用人类「从众心理」!掌握简单心理学成为说服高手
  12. 微信支付商户平台:商户简称
  13. 工商总局:将对网店卖家身份进行全面普查
  14. 树莓派 Linux VS code 远程断点调试 .net 6.0 保姆级教程
  15. java.util包
  16. java.util 语言_java中的import java.util是什么意思
  17. java的regex_java regex 简单使用
  18. 乱七八糟:迟到的入职两年个人总结
  19. [html代码] 几种美丽的分割线
  20. 如何开展性能测试工作

热门文章

  1. substring用法,between...and用法 trim标签的用法 模糊查询
  2. WIN系统仿MAC任务栏工具分享——RocketDock
  3. 【语义分割系列:八】Segmentation 数据集 介绍下载论文
  4. Dynamics CRM 365零基础入门学习(一)Dynamics介绍以及开发工具配置
  5. 树莓派CM4官方底板的双路摄像头使用
  6. 黑马程序员——java 泛型
  7. 基于单片机的水壶自动加热系统_基于单片机的智能热水壶设计 -
  8. SpringBoot应用监控(带邮件警报)
  9. Region Proposal by Guided Anchoring论文解读
  10. 分享一个Github逆天级别的彩蛋