Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5067 Accepted Submission(s): 1410
Problem Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
Sample Input
5 3 2
1 3 2
2 4 3
5 2 3
1 4
4 5
Sample Output
Not connected
6
Hint
Hint Huge input, scanf recommended.
题意:
给出 N 个结点,M 条边,K 个询问。输出每个询问间的距离,如果不能达到的话则输出 Not connected。
思路:
LCA + 并查集。用并查集判断是否在同一棵上,在同一棵树上则用 LCA 算出距离即可。
AC:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int NMAX = 10010; const int EMAX = 10000 * 5; int n, m, c; int root[NMAX]; int ind; int fir[NMAX], next[EMAX], w[EMAX], v[EMAX]; int ans; int id[NMAX], vs[NMAX * 3], dep[NMAX * 3], dis[NMAX]; bool vis[NMAX]; int dp[NMAX * 3][50]; void init () { ind = ans = 0; memset(fir, -1, sizeof(fir)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; ++i) root[i] = i; } void add_edge (int f, int t, int val) { v[ind] = t; w[ind] = val; next[ind] = fir[f]; fir[f] = ind; ++ind; } int Find (int x) { return x == root[x] ? x : root[x] = Find(root[x]); } void merge_tree (int a, int b) { int A = Find(a); int B = Find(b); if (A != B) root[A] = B; } bool que_tree (int a, int b) { if (Find(a) == Find(b)) return true; return false; } void dfs (int x, int d) { id[x] = ans; vs[ans] = x; dep[ans++] = d; vis[x] = 1; for (int e = fir[x]; e != -1; e = next[e]) { int V = v[e]; if (!vis[V]) { dis[V] = dis[x] + w[e]; dfs(V, d + 1); dep[ans] = d; vs[ans++] = x; } } } void RMQ_init () { for (int i = 0; i < ans; ++i) dp[i][0] = i; for (int j = 1; (1 << j) <= ans; ++j) { for (int i = 0; i + (1 << j) < ans; ++i) { int a = dp[i][j - 1]; int b = dp[i + (1 << (j - 1))][j - 1]; if (dep[a] < dep[b]) dp[i][j] = a; else dp[i][j] = b; } } } int RMQ (int L, int R) { int len = 0; while ((1 << (len + 1)) <= (R - L + 1)) ++len; int a = dp[L][len]; int b = dp[R - (1 << len) + 1][len]; if (dep[a] < dep[b]) return a; return b; } int LCA (int a, int b) { int L = min(id[a], id[b]); int R = max(id[a], id[b]); int node = RMQ(L, R); return vs[node]; } int main() { while (~scanf("%d%d%d", &n, &m, &c)) { init(); while (m--) { int a, b, val; scanf("%d%d%d", &a, &b, &val); add_edge(a, b, val); add_edge(b, a, val); merge_tree(a, b); } for (int i = 1; i <= n; ++i) { if (root[i] == i) { dis[i] = 0; dfs(i, 1); } } RMQ_init(); while (c--) { int a, b; scanf("%d%d", &a, &b); if (que_tree(a, b)) { int c = LCA(a, b); printf("%d\n", dis[a] + dis[b] - 2 * dis[c]); } else puts("Not connected"); } } return 0; }
相关推荐
NATBLASTER-Establishing TCP Connections Between Hosts Behind NATs.pdf
We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer ...
在深入探讨IBM Connections 5.5性能调优的知识点之前,首先需要明确IBM Connections 5.5是一款企业社交软件,专为商务活动设计,可以用来促进企业内外部的信息交流与合作。性能调优通常是为了提高软件运行效率、增强...
标题与描述:“Lotus Connections企业级部署” Lotus Connections是一款由IBM开发的企业社交软件平台,旨在帮助企业构建内部社交网络,促进员工之间的沟通、协作和知识共享。Lotus Connections 3.0版本在企业级部署...
在Laravel框架中,"laravel-connections"通常指的是数据库连接管理。这个软件包是针对Laravel的一个扩展,专门用于帮助开发者更有效地管理和处理模型之间的关系,尤其是涉及到复杂的数据交互和用户之间的“友谊”...
### IBM Connections 4.0 新特性详解 IBM Connections 4.0是IBM公司推出的一款集成了多种企业级社交网络功能的平台,旨在帮助企业构建高效、协作的工作环境。本篇文章将详细解析IBM Connections 4.0的新特性,涵盖...
图解 Tomcat maxConnections、maxThreads、acceptCount
相比于早期的YOLO版本,YOLOv7采用了更先进的架构设计,如Mish激活函数、CrossStage Partial Connections (CSP) 和自适应锚框等技术,这些都极大地提高了模型的检测性能。Mish激活函数是非线性的,可以更好地捕获...
最近一次安全培训,需要用到安全攻防平台,结果30几个人登录上去直接爆出500错误。不知道什么原因,后来找来SSH登录用户,密码,逐步排查,发现了Nginx... 您可能感兴趣的文章:Nginx中worker connections问题的解决方法
MySQL数据库在运行过程中可能会遇到“Too many connections”的错误提示,这意味着服务器上的MySQL实例达到了其最大允许的并发连接数。此问题通常由以下两种情况引起: 1. **并发连接过多**:大量的应用程序或用户...
mysql官方告诉我们需要修改max_connections的值,那么我们怎么去修改呢?有两种方法 1、修改配置文件文件 修改/etc/my.cnf这个文件,在[mysqld]中新增max_connections=N,如果你没有这个文件请从编译源码中的support...
IBM Access Connections 3.71 经典的网络连接工具
Lenovo(IBM)出品的Access Connections是一款功能强大的网络辅助软件。 现如今毫无疑问的我们已经进入了网络互联时代,而对于笔记本这种移动设备来说就需要经常在各种不同的网络环境下使用,比如在公司需要根据...
在mysql的手册中已经对max_user_connections有一点说明,它是用来限制用户资源的,怎么限制用户资源呢?这里做了个小测试。首先产看该全局变量的值mysql> select @@max_user_connections;+————————+| @@max_...
It offers unsurpassed features designed to illuminate connections between pharmacology and practice, from patient scenarios and practice applications to coverage of lifespan considerations, patient ...
IBM的Access Connections软件是一款功能强大的网络辅助软件。现如今毫无疑问的我们已经进入了网络互联时代,而对于笔记本这种移动设备来说就需要经常在各种不同的网络环境下使用,比如在公司需要根据网络环境设置...
Telerik.NetworkConnections.Windows.dll
如果一切正常,我们可以从`getInputStream()`获取响应的流数据,并读取内容。 标签“connections”表明这个实例可能涉及到了连接池的概念。在实际应用中,为了提高效率和减少资源消耗,通常会使用连接池管理HTTP...
数据库无法连接故障的定位,Too many connections(连接过多)是数据库管理员经常面临的问题。当业务系统尝试连接数据库时,如果数据库的连接数已经达到上限,那么新的连接请求就无法建立,这时就会抛出“Too many ...