图论题目。

打印 上一主题 下一主题

主题 559|帖子 559|积分 1677

检测环(dfs+bfs)

课程表

题目

dfs:
  1. class Solution {
  2.     //dfs来
  3.     //先创建有向图
  4.     //在判断图有没有成环
  5.     boolean isCircle=false;
  6.     boolean[] onPath;//记录一次经过的节点
  7.     boolean[] visit;//记录全过程访问过的节点
  8.     public boolean canFinish(int numCourses, int[][] prerequisites) {
  9.         //建图--> 邻接表表示
  10.         List<Integer>[] graph=new LinkedList[numCourses];
  11.         //初始化graph
  12.         for(int i=0;i<numCourses;i++){
  13.             graph[i]=new LinkedList();
  14.         }
  15.         //创建onPath,visit
  16.         onPath=new boolean[numCourses];
  17.         visit=new boolean[numCourses];
  18.         //graph的索引就是from
  19.         for(int[] arr:prerequisites){
  20.             int from=arr[1];
  21.             int to=arr[0];
  22.             graph[from].add(to);
  23.         }
  24.         //遍历图,由于图具有不连通性,每个节点都需要作为起点
  25.         for(int i=0;i<numCourses;i++){
  26.             trverse(graph,i);
  27.         }
  28.         return !isCircle;
  29.     }
  30.     //从start节点开始遍历
  31.     void trverse(List<Integer>[] graph,int start){
  32.         if(isCircle){
  33.             //已经成环了
  34.             return;
  35.         }
  36.         //在onPath路上重复出现start,说明成环了
  37.         if(onPath[start]==true){
  38.             isCircle=true;
  39.             return;
  40.         }
  41.         //start节点曾经访问过,不需要重复访问
  42.         if(visit[start]==true){
  43.             return;
  44.         }
  45.         onPath[start]=true;
  46.         visit[start]=true;
  47.         for(int neibor:graph[start]){
  48.             trverse(graph,neibor);
  49.         }
  50.         onPath[start]=false;
  51.     }
  52. }
复制代码
bfs:
  1. class Solution {
  2.     //用bfs
  3.     //思路:入度为0就进队列,同时把相邻的节点入度减1,
  4.     //入队列就说明入度为0可以学,同样出队列的次数就是可以学习的个数
  5.     //用到的:队列,存入度使得数组,图
  6.     public boolean canFinish(int numCourses, int[][] prerequisites) {
  7.         //创建图,和上面一样
  8.         List<Integer>[]graph=new LinkedList[numCourses];
  9.         for(int i=0;i<numCourses;i++){
  10.             graph[i]=new LinkedList();
  11.         }
  12.         int[]indegree=new int[numCourses];//索引就是节点,数值就是入度数量
  13.         for(int[] arr:prerequisites){
  14.             int from=arr[1];
  15.             int to=arr[0];
  16.             indegree[to]++;
  17.             graph[from].add(to);
  18.         }
  19.         //创建队列
  20.         Queue<Integer> queue=new LinkedList();
  21.         //把入度为0的节点入队列
  22.         for(int i=0;i<numCourses;i++){
  23.             if(indegree[i]==0){
  24.                 queue.add(i);
  25.             }
  26.         }
  27.         int count=0;//统计入队列(可以学习)的个数
  28.         //进队列就说明入度为1,可以学习
  29.         while(!queue.isEmpty()){
  30.             int node=queue.poll();
  31.             count++;
  32.             for(int neibor:graph[node]){
  33.                 indegree[neibor]--;
  34.                 if(indegree[neibor]==0){
  35.                     queue.add(neibor);
  36.                 }
  37.             }
  38.         }
  39.         if(count==numCourses){
  40.             return true;
  41.         }
  42.         return false;
  43.     }
  44. }
复制代码
拓扑排序(dfs+bfs)

课程表2


   我的错误代码:
原因:我的res写在了前序遍历的位置,应该写在后序遍历的位置。
  1. class Solution {
  2.     //dfs
  3.     ArrayList<Integer> res=new ArrayList();
  4.     boolean[]visit;
  5.     boolean[] onPath;
  6.     boolean isCircle=false;
  7.     public int[] findOrder(int numCourses, int[][] prerequisites) {
  8.         //创建图
  9.         List<Integer>[] graph=new LinkedList[numCourses];
  10.         for(int i=0;i<numCourses;i++){
  11.             graph[i]=new LinkedList();
  12.         }
  13.         for(int[]arr:prerequisites){
  14.             int from=arr[1];
  15.             int to=arr[0];
  16.             graph[from].add(to);
  17.         }
  18.         visit=new boolean[numCourses];
  19.         onPath=new boolean[numCourses];
  20.         for(int i=0;i<numCourses;i++){
  21.             traverse(graph,i);
  22.         }
  23.         if(isCircle){
  24.             return new int[]{};
  25.         }
  26.         int[]arr=new int[numCourses];
  27.         for(int i=0;i<numCourses;i++){
  28.             arr[i]=res.get(i);
  29.         }
  30.         return arr;
  31.     }
  32.     //以start为起点
  33.     void traverse(List<Integer>[] graph ,int start){
  34.         //已经有环了,直接返回
  35.         if(isCircle){
  36.             return;
  37.         }
  38.         //找到了环
  39.         if(onPath[start]==true){
  40.             isCircle=true;
  41.             return;
  42.         }
  43.         //start曾经访问过,防止重复
  44.         if(visit[start]==true){
  45.             return;
  46.         }
  47.         onPath[start]=true;
  48.         visit[start]=true;
  49.         res.add(start);
  50.         for(int neibor:graph[start]){
  51.             traverse(graph,neibor);
  52.         }
  53.         onPath[start]=false;
  54.     }
  55. }
复制代码

修正:这题用到拓扑排序,拓扑排序是对有向无环图(DAG)的顶点进行线性排序的一种算法,实在特别简朴,把图结构后序遍历的结果进行反转,就是拓扑排序的结果。后序遍历的这一特点很紧张,之所以拓扑排序的底子是后序遍历,是因为一个使命必须等到它依靠的所有使命都完成之后才能开始开始执行
DFS:
  1. class Solution {
  2.     //dfs
  3.     ArrayList<Integer> res=new ArrayList();
  4.     boolean[]visit;
  5.     boolean[] onPath;
  6.     boolean isCircle=false;
  7.     public int[] findOrder(int numCourses, int[][] prerequisites) {
  8.         //创建图
  9.         List<Integer>[] graph=new LinkedList[numCourses];
  10.         for(int i=0;i<numCourses;i++){
  11.             graph[i]=new LinkedList();
  12.         }
  13.         for(int[]arr:prerequisites){
  14.             int from=arr[1];
  15.             int to=arr[0];
  16.             graph[from].add(to);
  17.         }
  18.         visit=new boolean[numCourses];
  19.         onPath=new boolean[numCourses];
  20.         for(int i=0;i<numCourses;i++){
  21.             traverse(graph,i);
  22.         }
  23.         if(isCircle){
  24.             return new int[]{};
  25.         }
  26.         int[]arr=new int[numCourses];
  27.         Collections.reverse(res);
  28.         for(int i=0;i<numCourses;i++){
  29.             arr[i]=res.get(i);
  30.         }
  31.         return arr;
  32.     }
  33.     //以start为起点
  34.     void traverse(List<Integer>[] graph ,int start){
  35.         //已经有环了,直接返回
  36.         if(isCircle){
  37.             return;
  38.         }
  39.         //找到了环
  40.         if(onPath[start]==true){
  41.             isCircle=true;
  42.             return;
  43.         }
  44.         //start曾经访问过,防止重复
  45.         if(visit[start]==true){
  46.             return;
  47.         }
  48.         onPath[start]=true;
  49.         visit[start]=true;
  50.         
  51.         for(int neibor:graph[start]){
  52.             traverse(graph,neibor);
  53.         }
  54.         res.add(start);
  55.         onPath[start]=false;
  56.     }
  57. }
复制代码
BFS:
  1. class Solution {
  2.     //bfs:出队列的顺序就是拓扑排序
  3.     public int[] findOrder(int numCourses, int[][] prerequisites) {
  4.         //创建图,和上面一样
  5.         List<Integer>[]graph=new LinkedList[numCourses];
  6.         for(int i=0;i<numCourses;i++){
  7.             graph[i]=new LinkedList();
  8.         }
  9.         int[]indegree=new int[numCourses];//索引就是节点,数值就是入度数量
  10.         for(int[] arr:prerequisites){
  11.             int from=arr[1];
  12.             int to=arr[0];
  13.             indegree[to]++;
  14.             graph[from].add(to);
  15.         }
  16.         //创建res数组
  17.         int[]res=new int[numCourses];
  18.         //创建队列
  19.         Queue<Integer> queue=new LinkedList();
  20.         //把入度为0的节点入队列
  21.         for(int i=0;i<numCourses;i++){
  22.             if(indegree[i]==0){
  23.                 queue.add(i);
  24.             }
  25.         }
  26.         int count=0;//统计入队列(可以学习)的个数
  27.         int k=0;
  28.         //进队列就说明入度为1,可以学习
  29.         while(!queue.isEmpty()){
  30.             int node=queue.poll();  
  31.             count++;
  32.             res[k++]=node;        
  33.             for(int neibor:graph[node]){
  34.                 indegree[neibor]--;
  35.                 if(indegree[neibor]==0){
  36.                     queue.add(neibor);
  37.                 }
  38.             }
  39.         }
  40.         if(count==numCourses){
  41.             return res;
  42.         }
  43.         return new int[]{};
  44.     }
  45.    
  46. }
复制代码
二分图(dfs,bfs)

判断二分图


  1. class Solution {
  2.     boolean ok=true;//是二分图
  3.     //用dfs
  4.     boolean[] visit;
  5.     boolean[] color;
  6.     public boolean isBipartite(int[][] graph) {
  7.         int sz=graph.length;
  8.         color=new boolean[sz];
  9.         visit=new boolean[sz];
  10.         //遍历图
  11.         for(int i=0;i<sz;i++){
  12.             traverse(graph,i);
  13.         }
  14.         return ok;
  15.     }
  16.     //从start节点开始遍历图
  17.     void traverse(int[][]graph,int start){
  18.         //已经不是二分图了
  19.         if(!ok){
  20.             return;
  21.         }
  22.         int sz=graph.length;
  23.         //start节点访问过了
  24.         if(visit[start]){
  25.             return;
  26.         }
  27.         visit[start]=true;
  28.         for(int neibor:graph[start]){
  29.             if(!visit[neibor]){
  30.                 //start的相邻节点没有访问
  31.                 //给相邻节点染色
  32.                 color[neibor]=!color[start];
  33.                 traverse(graph,neibor);
  34.             }else{
  35.                 if(color[start]==color[neibor]){
  36.                     ok=false;
  37.                     return;
  38.                 }
  39.             }
  40.         }
  41.     }
  42. }
复制代码
  1. class Solution {
  2.     //用bfs
  3.     boolean[]color;
  4.     boolean[]visit;
  5.     boolean ok=true;
  6.     public boolean isBipartite(int[][] graph) {
  7.         int sz=graph.length;
  8.         color=new boolean[sz];
  9.         visit=new boolean[sz];
  10.         Queue<Integer> queue=new LinkedList();
  11.         //图具有不连通性
  12.         for(int i=0;i<sz;i++){     
  13.             if(visit[i]==true){
  14.                 continue;
  15.             }
  16.             queue.add(i);
  17.             visit[i]=true;
  18.             while(!queue.isEmpty()){
  19.                 int cur=queue.poll();
  20.                 //把cur的邻居节点染色,入队列
  21.                 for(int neibor:graph[cur]){
  22.                     if(!visit[neibor]){
  23.                         color[neibor]=!color[cur];
  24.                         visit[neibor]=true;
  25.                         queue.add(neibor);
  26.                     }else{
  27.                         if(color[neibor]==color[cur]){
  28.                             return false;
  29.                         }
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.         return true;
  35.     }
  36. }
复制代码
大概的二分法

题目

DFS:
  1. class Solution {
  2.     //dislike相当于给了你边的关系
  3.     //1.创建图 无向图
  4.     //2.判断这个图是不是二分图
  5.     //dfs
  6.     boolean[]color;
  7.     boolean[]visit;
  8.     boolean ok=true;
  9.     public boolean possibleBipartition(int n, int[][] dislikes) {
  10.         List<Integer>[]graph=buildGraph(n,dislikes);
  11.         color=new boolean[n+1];
  12.         visit=new boolean[n+1];
  13.         for(int i=1;i<=n;i++){
  14.             traverse(graph,i);
  15.         }
  16.         return ok;
  17.     }
  18.     //以start为起点,开始遍历
  19.     void traverse( List<Integer>[] graph,int start){
  20.         //不是二分图
  21.         if(!ok){
  22.             return;
  23.         }
  24.         //start已经访问了
  25.         if(visit[start]){
  26.             return;
  27.         }
  28.         visit[start]=true;
  29.         for(int neibor:graph[start]){
  30.             if(!visit[neibor]){
  31.                 color[neibor]=!color[start];
  32.                 traverse(graph,neibor);
  33.             }else{
  34.                 if(color[start]==color[neibor]){
  35.                     ok=false;
  36.                     return;
  37.                 }
  38.             }
  39.         }
  40.     }
  41.     List<Integer>[] buildGraph(int n,int[][]dislikes){
  42.         List<Integer>[] graph=new LinkedList[n+1];
  43.         for(int i=0;i<=n;i++){
  44.             graph[i]=new LinkedList();
  45.         }
  46.         for(int[] relation:dislikes){
  47.             int a=relation[0];
  48.             int b=relation[1];
  49.             graph[a].add(b);
  50.             graph[b].add(a);
  51.         }
  52.         return graph;
  53.     }
  54. }
复制代码
BFS:
  1. class Solution {
  2.     //dislike相当于给了你边的关系
  3.     //1.创建图 无向图
  4.     //2.判断这个图是不是二分图
  5.     //bfs
  6.     boolean[]color;
  7.     boolean[]visit;
  8.     boolean ok=true;
  9.     public boolean possibleBipartition(int n, int[][] dislikes) {
  10.         List<Integer>[]graph=buildGraph(n,dislikes);
  11.         color=new boolean[n+1];
  12.         visit=new boolean[n+1];
  13.         for(int i=1;i<=n;i++){
  14.             bfs(graph,i);
  15.         }
  16.         return ok;
  17.     }
  18.     //以start为起点,开始bfs
  19.     void bfs( List<Integer>[] graph,int start){
  20.         //不是二分图
  21.         if(!ok){
  22.             return;
  23.         }
  24.         //start已经访问了
  25.         if(visit[start]){
  26.             return;
  27.         }
  28.         visit[start]=true;
  29.         Queue<Integer> queue=new LinkedList();
  30.         queue.add(start);
  31.        while(!queue.isEmpty()){
  32.         int cur=queue.poll();
  33.         for(int neibor:graph[cur]){
  34.             if(!visit[neibor]){
  35.                 color[neibor]=!color[cur];
  36.                 visit[neibor]=true;
  37.                 queue.add(neibor);
  38.             }else{
  39.                 if(color[cur]==color[neibor]){
  40.                     ok=false;
  41.                     return;
  42.                 }
  43.             }
  44.         }
  45.        }
  46.     }
  47.     List<Integer>[] buildGraph(int n,int[][]dislikes){
  48.         List<Integer>[] graph=new LinkedList[n+1];
  49.         for(int i=0;i<=n;i++){
  50.             graph[i]=new LinkedList();
  51.         }
  52.         for(int[] relation:dislikes){
  53.             int a=relation[0];
  54.             int b=relation[1];
  55.             graph[a].add(b);
  56.             graph[b].add(a);
  57.         }
  58.         return graph;
  59.     }
  60. }
复制代码
Kruskal算法和Prim算法

   Kruskal: 将所有边按照权重从小到大排序,从权重最小的边开始遍历,如果这条边和 mst 中的其它边不会形成环,则这条边是最小生成树的一部分,将它加入 mst 集合;否则,这条边不是最小生成树的一部分,不要把它加入 mst 集合。
    Prim: Prim 算法的逻辑就是这样,每次切分都能找到最小生成树的一条边,然后又可以进行新一轮切分,直到找到最小生成树的所有边为止。
  连接所有点的最小费用

题目

  1. class Solution {
  2.     //用kruscal算法 时间复杂度是:O(M*logM) M为边的数量 这里变得数量是n(n-1)/2,n是点的数量
  3.     public int minCostConnectPoints(int[][] points) {
  4.         List<int[]> graph=new LinkedList();
  5.         int sz=points.length;
  6.         for(int i=0;i<sz;i++){
  7.             for(int j=i+1;j<sz;j++){
  8.                 int x0=points[i][0];
  9.                 int y0=points[i][1];
  10.                 int x1=points[j][0];
  11.                 int y1=points[j][1];
  12.                 int weight=Math.abs(x0-x1)+Math.abs(y1-y0);
  13.                 graph.add(new int[]{i,j,weight});
  14.             }
  15.         }
  16.        //排序 权重从小到大
  17.        Collections.sort(graph,new Comparator<int[]>() {
  18.                @Override
  19.             public int compare(int[] o1, int[] o2) {
  20.                     // TODO Auto-generated method stub
  21.                     return o1[2]-o2[2];
  22.             }
  23.             });
  24.         //
  25.         UF uf=new UF(sz);
  26.         int total=0;
  27.         for(int[]arr:graph){
  28.             int city1=arr[0];
  29.             int city2=arr[1];
  30.             int distance=arr[2];
  31.             if(uf.isConnected(city1,city2)){
  32.                 continue;
  33.             }
  34.             uf.union(city1,city2);
  35.             total+=distance;
  36.         }
  37.         return total;
  38.     }
  39.     class UF{
  40.         int count;//连通数量
  41.         int[]parent;//父节点
  42.         UF(int n){
  43.             parent=new int[n];
  44.             for(int i=0;i<n;i++){
  45.                 parent[i]=i;//自环
  46.             }
  47.             count=n;
  48.         }
  49.         boolean isConnected(int a,int b){
  50.             //找到a的父节点
  51.             int parentA=find(a);
  52.             int parentB=find(b);
  53.             if(parentA==parentB){
  54.                 return true;
  55.             }
  56.             return false;
  57.         }
  58.         //找到x的父节点
  59.         int find(int x){
  60.             if(parent[x]!=x){
  61.                 parent[x]=find(parent[x]);
  62.             }
  63.             return parent[x];
  64.         }
  65.         void union(int a,int b){
  66.             int rootA=find(a);
  67.             int rootB=find(b);
  68.             if(rootA==rootB){
  69.                 return;
  70.             }
  71.             parent[rootA]=rootB;
  72.             count--;
  73.         }
  74.         int count(){
  75.             return count;
  76.         }
  77.     }
  78. }
复制代码
  1. class Solution {
  2.     //用prim算法
  3.     boolean[] isMST;//记录那些节点是最小生成树的一部分
  4.     PriorityQueue<int[]> pq;//存储横切边[from,to,weiht]
  5.     int weightSum=0;//记录最小生成树的权重和
  6.     List<int[]>[] graph;//记录三元组{from,to,weight}表示一条边 下标就是from
  7.     public int minCostConnectPoints(int[][] points) {
  8.         int sz=points.length;
  9.         isMST=new boolean[sz];
  10.         //按照权重从小到大的顺序
  11.         pq=new PriorityQueue<int[]>(new Comparator<int[]>() {
  12.             @Override
  13.             public int compare(int[] o1, int[] o2) {
  14.                 return o1[2]-o2[2];
  15.             }
  16.         });
  17.         graph=buildGraph(points);
  18.         cut(0);//先从第一个城市开始切
  19.         isMST[0]=true;
  20.         while(!pq.isEmpty()){
  21.             int[]cur=pq.poll();
  22.             if(!isMST[cur[1]]){
  23.                 isMST[cur[1]]=true;
  24.                 weightSum+=cur[2];
  25.                 cut(cur[1]);
  26.             }
  27.         }
  28.         return weightSum;
  29.     }
  30.     //围绕x节点切一刀
  31.     void cut(int x){
  32.         //遍历x的邻居节点,
  33.         for(int[]neibor:graph[x]){
  34.             //邻居节点已经是生成树的一部分,不用重复入队列
  35.             if(isMST[neibor[1]]){
  36.                 continue;
  37.             }
  38.             pq.add(neibor);
  39.         }
  40.     }
  41.     List<int[]>[] buildGraph(int[][]points){
  42.         int sz=points.length;//城市的数量
  43.         List<int[]>[] graph=new ArrayList[sz];
  44.         for(int i=0;i<sz;i++){
  45.             graph[i]=new ArrayList();
  46.         }
  47.         for(int i=0;i<sz;i++){
  48.             for(int j=0;j<sz;j++){
  49.                 int x0=points[i][0];
  50.                 int y0=points[i][1];
  51.                 int x1=points[j][0];
  52.                 int y1=points[j][1];
  53.                 int weight=Math.abs(x0-x1)+Math.abs(y0-y1);
  54.                 graph[i].add(new int[]{i,j,weight});
  55.             }
  56.         }
  57.         return graph;
  58.     }
  59. }
复制代码
Dijkstra算法


概率最大的路径

题目

  1. class Solution {
  2.     public double maxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node) {
  3.         //创建图
  4.         List<double[]>[]graph=buildGarph(n,edges,succProb);//三元组{from,to,weight}
  5.         //创建优先级队列 {当前节点,到起点的权重} 降序
  6.         PriorityQueue<double[]> pq=new PriorityQueue<double[]>(new Comparator<double[]>() {
  7.             @Override
  8.             public int compare(double[] o1, double[] o2) {
  9.                 // TODO Auto-generated method stub
  10.                 return Double.compare(o2[1],o1[1]);
  11.             }
  12.         });
  13.         //创建备忘录,记录到期点的可能性
  14.         double[]memo=new double[n];
  15.         //起点先入队列
  16.         pq.add(new double[]{start_node,1});
  17.         memo[start_node]=1;
  18.         while(!pq.isEmpty()){
  19.             //取出节点
  20.             double[] curArr=pq.poll();
  21.             int curVal=(int)curArr[0];
  22.             double curProb=curArr[1];
  23.             //已经找到了更大的可能性(权重)
  24.             if(curVal==end_node){
  25.                 return curProb;
  26.             }
  27.             if(curProb<memo[curVal]){
  28.                 continue;
  29.             }
  30.             //去当前节点的周边看看
  31.             for(double[]neibor:graph[curVal]){
  32.                 //如果找到了更大可能性,就更新memo,并进队列
  33.                 if(neibor[2]*curProb>memo[(int)neibor[1]]){
  34.                     memo[(int)neibor[1]]=neibor[2]*curProb;
  35.                     pq.add(new double[]{neibor[1],memo[(int)neibor[1]]});
  36.                 }
  37.             }
  38.         }
  39.         return memo[end_node];
  40.     }
  41.     List<double[]>[] buildGarph(int n,int[][]edges,double[]succProb){
  42.         List<double[]>[] graph=new ArrayList[n];
  43.         for(int i=0;i<n;i++){
  44.             graph[i]=new ArrayList();
  45.         }
  46.         for(int i=0;i<edges.length;i++){
  47.             int node1=edges[i][0];
  48.             int node2=edges[i][1];
  49.             double weight=succProb[i];
  50.             graph[node1].add(new double[]{node1,node2,weight});
  51.             graph[node2].add(new double[]{node2,node1,weight});
  52.         }
  53.         return graph;
  54.     }
  55. }
复制代码
网络延时时间

题目

  1. class Solution {
  2.     //以k为起点
  3.     public int networkDelayTime(int[][] times, int n, int k) {
  4.             //{from,to,weight}
  5.             ArrayList<int[]>[] graph=new ArrayList[n+1];
  6.         for(int i=0;i<=n;i++){
  7.             graph[i]=new ArrayList();
  8.         }
  9.             //建图
  10.             for(int[] e:times) {
  11.                     int from=e[0];
  12.                     int to=e[1];
  13.                     int weight=e[2];
  14.                     graph[from].add(new int[] {from,to,weight});
  15.            
  16.             }
  17.            
  18.         int[]memo=new int[n+1];//memo[i]:i距离起点的长度
  19.         Arrays.fill(memo, Integer.MAX_VALUE);
  20.         memo[k]=0;
  21.         //pq存的是{当前节点,距离起点的长度}
  22.         PriorityQueue<int[]>pq=new PriorityQueue<int[]>(new Comparator<int[]>() {
  23.                 @Override
  24.                 public int compare(int[] o1, int[] o2) {
  25.                         // TODO Auto-generated method stub
  26.                         return o1[1]-o2[1];
  27.                 }
  28.                 });
  29.         pq.add(new int[] {k,0});
  30.         memo[k]=0;
  31.         while(!pq.isEmpty()) {
  32.                 int[]cur=pq.poll();
  33.                 int curVal=cur[0];
  34.                 int curDis=cur[1];
  35.                 //如果当前的节点距离起点的距离较大,就不在继续往下
  36.                 if(curDis>memo[curVal]) {
  37.                         continue;
  38.                 }
  39.                
  40.                 for(int[]neibor:graph[curVal]) {
  41.                         int neiborVal=neibor[1];
  42.                         int neiborDis=curDis+neibor[2];
  43.                         //当前的权重更小就更新
  44.                         if(neiborDis<memo[neiborVal]) {
  45.                                 //更新memo
  46.                                 memo[neiborVal]=neiborDis;
  47.                                 //把小化的数据入队列
  48.                                 pq.add(new int[] {neiborVal,neiborDis});
  49.                         }
  50.                 }
  51.         }
  52.         //在memo中找最大值返回
  53.         int res=memo[1];
  54.         for(int i=2;i<=n;i++) {
  55.                 res=Math.max(res, memo[i]);
  56.         }
  57.         
  58.         return (res==Integer.MAX_VALUE)?-1:res;
  59.     }
  60. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

星球的眼睛

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表