当前位置: 首页 > news >正文

代码随想录-算法训练营-番外(图论01:图论理论基础,所有可到达的路径)

day01 图论part01 
今日任务:图论理论基础/所有可到达的路径
代码随想录图论视频部分还没更新
https://programmercarl.com/kamacoder/图论理论基础.html#图的基本概念

day01

所有可达路径

邻接矩阵

 import java.util.Scanner;import java.util.List;import java.util.ArrayList;​public class Main{static List<List<Integer>> result = new ArrayList<>();static List<Integer> path = new ArrayList<>();public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int[][] graph = new int[n + 1][n + 1];for(int i = 0; i < m; i++){graph[sc.nextInt()][sc.nextInt()] = 1;}path.add(1); //先加一个节点dfs(graph, 1, n); if (result.isEmpty()) System.out.println(-1);for(List<Integer> pa : result){for (int i = 0; i < pa.size() - 1; i++) {System.out.print(pa.get(i) + " ");}System.out.println(pa.get(pa.size() - 1));} }private static void dfs(int[][] graph, int x, int n){//n就是结束节点if(x == n){result.add(new ArrayList(path));return;}for(int i = 1; i <= n; i++){if (graph[x][i] == 1) { path.add(i); dfs(graph, i, n); path.remove(path.size() - 1); }}return;}}

邻接表

 //感觉graph不用LinkedList而是直接用ArrayList也可以,因为这个场景下不涉及什么增删改而基本都是访问import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Scanner;​public class Main {static List<List<Integer>> result = new ArrayList<>(); // 收集符合条件的路径static List<Integer> path = new ArrayList<>(); // 1节点到终点的路径​public static void dfs(List<LinkedList<Integer>> graph, int x, int n) {if (x == n) { // 找到符合条件的一条路径result.add(new ArrayList<>(path));return;}for (int i : graph.get(x)) { // 找到 x指向的节点path.add(i); // 遍历到的节点加入到路径中来dfs(graph, i, n); // 进入下一层递归path.remove(path.size() - 1); // 回溯,撤销本节点}}​public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();​// 节点编号从1到n,所以申请 n+1 这么大的数组List<LinkedList<Integer>> graph = new ArrayList<>();for (int i = 0; i <= n; i++) {graph.add(new LinkedList<>());}​while (m-- > 0) {int s = scanner.nextInt();int t = scanner.nextInt();// 使用邻接表表示 s -> t 是相连的graph.get(s).add(t);}​path.add(1); // 无论什么路径已经是从1节点出发dfs(graph, 1, n); // 开始遍历​// 输出结果if (result.isEmpty()) System.out.println(-1);for (List<Integer> pa : result) {for (int i = 0; i < pa.size() - 1; i++) {System.out.print(pa.get(i) + " ");}System.out.println(pa.get(pa.size() - 1));}}}

感谢大佬分享:

代码随想录算法训练营第五十天|Day50 图论_本关任务:创建邻接表存储的无向图,并输出图的邻接表。-CSDN博客

http://www.lryc.cn/news/503827.html

相关文章:

  • 【JAVA】Java项目实战—Java EE项目:企业资源规划(ERP)系统
  • springboot配置过滤器解决html资源路径和接口路径冲突问题
  • 在IDE中使用Git
  • 【AIGC进阶-ChatGPT提示词副业解析】反向心理学在沟通中的运用:激将法的艺术
  • JeecgBoot passwordChange 任意用户密码重置漏洞复现
  • 【智体OS】官方上新发布智体机器人:使用rtrobot智体应用远程控制平衡车机器人
  • Blazor(.razor)+VUE+elementUI适合一起用吗
  • SpringBoot左脚进门之Maven管理家
  • 188-下翻便携式6U CPCI工控机箱
  • Ubuntu 挂载目录
  • 基于IEEE 802.1Qci的时间敏感网络(TSN)主干架构安全分析及异常检测系统设计
  • 2024年食堂采购系统源码技术趋势:如何开发智能的供应链管理APP
  • zotero安装教程(包括茉莉花插件)
  • webpack4 - 配置文件分离(详细教程)
  • MongoDB 分片
  • PHP加载MySQL扩展
  • 期末复习-计算机网络篇SCAU
  • 使用LLM进行股价预测(附代码)
  • 分支限界笔记
  • PHP Cookie
  • Java后端面试场景题汇总
  • 【量化中的复权数据详解】
  • YOLO简史
  • 低通滤波器,高通滤波器,公式
  • 深入了解IPv6——光猫相关设定:DNS来源、DHCPv6服务、前缀来源等
  • 前端国际化实战:从需求到落地的完整实践
  • React的状态管理库-Redux
  • 【Android学习】RxJava
  • Pycharm访问MySQL数据库·上
  • 【CUDA】CUBLAS