Codeforces Round 933 (Div. 3) --- G. Rudolf and Subway --- 题解

news/2024/5/12 9:20:47

G. Rudolf and Subway:

题目大意:

        

思路解析:

        这道题很容易看出是一个最短路的图论问题,但是Java普通最短路常数有点高会被卡。

        因为他是地铁线路,线路一定是一直连着的,不会中间断开,那我们可以从一个颜色线路上的站点到的任意其他站点,那我们就可以将整个图分为多个颜色块。从颜色块加速状态转移的过程,转移就变为了如果我们当前在一个站点上,那就选择一个这个站点能去的线路,如果当前在线路上,就选择当前在这个线路的那个地点停下。

 代码实现:

        

import java.util.*;
import java.io.*;public class Main {static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int t = Integer.parseInt(br.readLine());while (t-- > 0) {solve(br);}w.flush();w.close();}static void solve(BufferedReader br) throws IOException {StringTokenizer st = new StringTokenizer(br.readLine());int n = Integer.parseInt(st.nextToken());int m = Integer.parseInt(st.nextToken());Vector<Integer> a = new Vector<>();HashMap<Integer, Integer> map = new HashMap<>();int[][] edges = new int[m][3];for (int i = 0; i < m; i++) {st = new StringTokenizer(br.readLine());edges[i][0] = Integer.parseInt(st.nextToken()) - 1;edges[i][1] = Integer.parseInt(st.nextToken()) - 1;edges[i][2] = Integer.parseInt(st.nextToken());a.add(edges[i][2]);}int k = 0;for (int i = 0; i < a.size(); i++) {int num = a.get(i);if (!map.containsKey(num)){map.put(num, n + k + 1);k++;}}st = new StringTokenizer(br.readLine());int b = Integer.parseInt(st.nextToken()) - 1;int e = Integer.parseInt(st.nextToken()) - 1;LinkedList<Integer>[] list = new LinkedList[n + k + 1];for (int i = 0; i < n + k + 1; i++) {list[i] = new LinkedList<>();}for (int i = 0; i < m; i++) {int u = edges[i][0];int v = edges[i][1];int c = map.get(edges[i][2]);list[u].add(c);list[v].add(c);list[c].add(u);list[c].add(v);}LinkedList<Integer> q = new LinkedList<>();q.add(b);int[] dp = new int[n+k+1];Arrays.fill(dp, (int) 1e9);dp[b] = 0;while (!q.isEmpty()){int x = q.poll();if (x == e) break;for (Integer y : list[x]) {if (dp[y] > dp[x] + 1){dp[y] = dp[x] + 1;q.addLast(y);}}}System.out.println(dp[e] / 2);}static class Pair<T1, T2> {T1 first;T2 second;public Pair(T1 first, T2 second) {this.first = first;this.second = second;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (!(o instanceof Pair)) return false;Pair<?, ?> pair = (Pair<?, ?>) o;return Objects.equals(first, pair.first) &&Objects.equals(second, pair.second);}@Overridepublic int hashCode() {return Objects.hash(first, second);}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/10441.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

漏洞复现-蓝凌LandrayOA系列

蓝凌OA系列 蓝凌poc-yaml-landray-oa-rcepoc-yaml-landray-oa-custom-jsp-filereadlandray-uploadzhihuidiaodupingtai-upload-file-uploadlandray-sysZonePersonInfo-info-leaklandrayoa-2022-rcelandray-login-bypasslandray-login-bypass-rcelandray-getLoginSessionId-logi…

Excel F4键的作用

目录 一. 单元格相对/绝对引用转换二. 重复上一步操作 一. 单元格相对/绝对引用转换 ⏹ 使用F4键 如下图所示&#xff0c;B1单元格引用了A1单元格的内容。此时是使用相对引用&#xff0c;可以按下键盘上的F4键进行相对引用和绝对引用的转换。 二. 重复上一步操作 ⏹添加或删除…

Vue.js+SpringBoot开发天然气工程业务管理系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块三、使用角色3.1 施工人员3.2 管理员 四、数据库设计4.1 用户表4.2 分公司表4.3 角色表4.4 数据字典表4.5 工程项目表4.6 使用材料表4.7 使用材料领用表4.8 整体E-R图 五、系统展示六、核心代码6.1 查询工程项目6.2 工程物资…

【教程】uni-app iOS打包解决profile文件与私钥证书不匹配问题

摘要 当在uni-app中进行iOS打包时&#xff0c;有时会遇到profile文件与私钥证书不匹配的问题。本文将介绍如何解决这一问题&#xff0c;以及相关的技术细节和操作步骤。 引言 在uni-app开发过程中&#xff0c;iOS打包是一个常见的操作。然而&#xff0c;有时会出现profile文…

【Redis】Redis 缓存重点解析

Redis 缓存重点解析 推荐文章&#xff1a;【Redis】Redis的特性和应用场景 数据类型 持久化 数据淘汰 事务 多机部署-CSDN博客 1. 我看你的项目都用到了 Redis&#xff0c;你在最近的项目的哪些场景下用到了 Redis 呢&#xff1f; 一定要结合业务场景来回答问题&#x…

UE5 C++ TPS开发 学习记录(九

p20 首先我们现在有一个多人游戏的系统类MultiplayerSessionsSubsystem 在这个系统内提供了很多会话系统的接口SessionInterface 当现在我们有一些SessionInterfaceDelegates的委托,这个委托的来源是SessionInterface,所以我们使用的委托可以接收到来自SessionInterface的消息(…