博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-1986-Distance Queries
阅读量:6137 次
发布时间:2019-06-21

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

链接:

题意:

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

一个无向图求两个点的最短路。

思路:

Targjan算法,同时在记录查询的时候需要用链式结构,如果每次dfs都k次查找会T。

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long LL;const int MAXN = 4e4+10;struct Node{ int from_, to_, dist_; Node(int from, int to, int dist):from_(from), to_(to), dist_(dist){}};vector
G[MAXN];vector
Q[MAXN];int fa[MAXN], dis[MAXN];int vis[MAXN], fas[MAXN];int Res[MAXN];int n, m, l, r, v, k;int root, res;int Get_F(int x){ if (fa[x] == x) return x; fa[x] = Get_F(fa[x]); return fa[x];}void Merge(int u, int v){ int tv = Get_F(v); int tu = Get_F(u); if (tv != tu) fa[v] = u;}void Tarjan(int u){ vis[u] = 1; for (int i = 0;i < Q[u].size();i++) { Node node = Q[u][i]; if (vis[node.to_] == 1) { int lenth = dis[node.from_]+dis[node.to_]-2*dis[Get_F(node.to_)]; Res[node.dist_] = lenth; } } for (int i = 0;i < G[u].size();i++) { Node node = G[u][i]; if (!vis[node.to_]) { dis[node.to_] = dis[node.from_] + node.dist_; Tarjan(node.to_); Merge(node.from_, node.to_); } }}void init(){ for (int i = 1;i <= n;i++) { G[i].clear(); fa[i] = i; } memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(vis));}int main(){ ios::sync_with_stdio(false); cin.tie(0); int t; //cin >> t; //while (t--) while(~scanf("%d%d", &n, &m)) {// cin >> n >> m;// scanf("%d%d", &n, &m); char op; init(); for (int i = 1;i <= m;i++) {// cin >> l >> r >> v >> op; scanf("%d%d%d %c", &l, &r, &v, &op); G[l].push_back(Node(l, r, v)); G[r].push_back(Node(r, l, v)); } scanf("%d", &k); for (int i = 1;i <= k;i++) {// cin >> l >> r; scanf("%d%d", &l, &r); Q[l].push_back(Node(l, r, i)); Q[r].push_back(Node(r, l, i)); } dis[1] = 0; Tarjan(1); for (int i = 1;i <= k;i++) { printf("%d\n", Res[i]); } } return 0;}

  

 

转载于:https://www.cnblogs.com/YDDDD/p/10837711.html

你可能感兴趣的文章
建筑电气暖通给排水协作流程
查看>>
JavaScript面向对象编程深入分析(2)
查看>>
linux 编码转换
查看>>
POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
查看>>
Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月7日-1月14日)
查看>>
关于C#导出 文本文件
查看>>
使用native 查询时,对特殊字符的处理。
查看>>
maclean liu的oracle学习经历--长篇连载
查看>>
ECSHOP调用指定分类的文章列表
查看>>
分享:动态库的链接和链接选项-L,-rpath-link,-rpath
查看>>
阿里云企业邮箱 在Foxmail 7.0上POP3/IMAP协议设置方法
查看>>
Javascript一些小细节
查看>>
canvas学习总结
查看>>
Javascript的if判断
查看>>
spring cloud gateway 源码解析(3)记录请求参数及返回的json
查看>>
阿里云ECS数据盘格式化与挂载图文教程
查看>>
Flexbox响应式网页布局 - W3Schools视频02
查看>>
【手牵手】搭建前端组件库(二)
查看>>
怎么给视频添加音频或配乐
查看>>
怎么转换音乐格式
查看>>