Connections between cities HDU - 2874(最短路树 lca )
题意:
给出n个点m条边的图,c次询问 求询问中两个点间的最短距离。
解析:
Floyd会T,所以用到了最短路树。。具体思想为:
设k为u和v的最近公共祖先 d[i] 为祖结点到i的最短距离 则dis[u][v] = d[u] + d[v] - 2*d[k]
用tarjan的lca求即可
把这题代码当作模板就好啦
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
const int maxm = 1e6 + ;
int n, m, cnt1, cnt2, c;
int head1[maxn], head2[maxn], f[maxn], d[maxn], vis[maxn];
int res[maxm]; struct node
{
int v, w, next;
}Node[maxn*]; struct edge
{
int v, id, next;
}Edge[maxm*]; void add1_(int u, int v, int w)
{
Node[cnt1].v = v;
Node[cnt1].w = w;
Node[cnt1].next = head1[u];
head1[u] = cnt1++;
} void add1(int u, int v, int w)
{
add1_(u, v, w);
add1_(v, u, w);
} void add2_(int u, int v, int id)
{
Edge[cnt2].v = v;
Edge[cnt2].id = id;
Edge[cnt2].next = head2[u];
head2[u] = cnt2++;
} void add2(int u, int v, int id)
{
add2_(u, v, id);
add2_(v, u, id);
} int find(int x)
{
return f[x]==x?x:(f[x] = find(f[x]));
} int lca(int u, int deep, int root)
{
f[u] = u;
d[u] = deep;
vis[u] = root; // 标记属于的树
for(int i=head1[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(vis[e.v] == -)
{
lca(e.v, deep+e.w, root);
f[e.v] = u;
}
}
for(int i=head2[u]; i!=-; i=Edge[i].next)
{
edge e = Edge[i];
if(vis[e.v] == root) //判断另一个结点是不是和u属于一个树
{
int k = find(e.v); //寻找最近公共祖先
res[e.id] = d[u] + d[e.v] - *d[k];
}
}
} void init()
{
mem(head1, -);
mem(head2, -);
mem(res, -);
mem(vis, -);
cnt1 = cnt2 = ;
} int main()
{
while(scanf("%d%d%d", &n, &m, &c) != EOF)
{
init();
rap(i, , m)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add1(u, v, w);
}
rap(i, , c)
{
int u, v;
scanf("%d%d", &u, &v);
add2(u, v, i);
}
rap(i, , n)
if(vis[i] == -)
lca(i, , i);
rap(i, , c)
{
if(res[i] == -) printf("Not connected\n");
else printf("%d\n", res[i]);
}
} return ;
}
Connections between cities HDU - 2874(最短路树 lca )的更多相关文章
- hdu 3409 最短路树+树形dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3409 参考博客:http://www.cnblogs.com/woaishizhan/p/318981 ...
- HDU 2874 Connections between cities(LCA Tarjan)
Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...
- hdu 2874 Connections between cities [LCA] (lca->rmq)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2874 Connections between cities 带权lca判是否联通
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2874 Connections between cities(st&rmq LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2874 Connections between cities (并查集+LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 2874 Connections between cities(LCA)
题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...
- HDU——2874 Connections between cities
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu-2874 Connections between cities(lca+tarjan+并查集)
题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/327 ...
随机推荐
- Redis实现之客户端
客户端 Redis服务器是典型的一对多服务器程序:一个服务器可以与多个客户端建立网络连接,每个客户端可以向服务器发送命令请求,而服务器则接收并处理客户端发送的命令请求,并向客户端返回命令回复.通过使用 ...
- [vijos1067]Warcraft III 守望者的烦恼
就是上次考得fyfy.竟然是原题... // It is made by XZZ #include<cstdio> #include<algorithm> #include&l ...
- jQuery Validate (登录页面相关验证)
$(function() { var submit = false; var superHtml = []; /** * 匹配企业帐号,以字母开头,长度在6-20之间,只能包含字符.数字和下划线. * ...
- Ajax文件上传三式
文件上传(三式) 1.urls.py文件 url(r'^upload.html$', views.upload), 2.views.py文件 import os def upload(request) ...
- Mongodb大数据语法大全
JSON和MONGODBJSON不止是一种交换数据的方式,也是一种存储数据的良好方式,实际上MONGODB并未使用JSON存储数据,而是使用由MONGODB团队开发的一种称为BSON的开放数据格式. ...
- [转载]文件系统缓存dirty_ratio与dirty_background_ra
原文地址:文件系统缓存dirty_ratio与dirty_background_ratio两个参数区别作者:vincent 这两天在调优数据库性能的过程中需要降低操作系统文件Cache对数据库性能的影 ...
- Windows单机配置Kafka环境
首先确保机器已经安装好Zookeeper,Zookeeper安装参考 Windows单机配置Zookeeper环境 然后确保Zookeeper是正常启动状态 下载Kafka http://kafka. ...
- day04 list tuple (补)
今日内容: 1. 列表 2. 列表的增删改查 3. 列表的嵌套 4. 元组和元组嵌套 5. range 列表 列表: 能装对象的对象. 有顺序的(按照我们添加的顺序保存) 在代码中使用[]表示列表. ...
- python struct详解
转载:https://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 有的时候需要用python处理二进制数据,比如,存取文件,socket操 ...
- python 中如何计算时间差...
Q:如何方便的计算两个时间的差,如两个时间相差几天,几小时等 A:使用datetime模块可以很方便的解决这个问题,举例如下: >>> import datetime>> ...