标签:
问题描述:
给定如图所示的无向连通图,假定图中所有边的权值都为1,显然,从源点A到终点T的虽短路径有多条,求不同的最短路径的数目。
权值相同的最短路径问题,则但愿点Dijkstra算法退化成广度优先搜索,假定起点为0,终点为N。
用动态规划的思想:
Code:
class NumOfShortestPath { private int[][] aja = { /*0*/{0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, /*1*/{1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0}, /*2*/{0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0}, /*3*/{0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0}, /*4*/{1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0}, /*5*/{0,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0}, /*6*/{0,0,1,0,0,1,0,1,0,0,1,0,0,0,0,0}, /*7*/{0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0}, /*8*/{0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0}, /*9*/{0,0,0,0,0,1,0,0,1,0,1,0,0,1,0,0}, /*10*/{0,0,0,0,0,0,1,0,0,1,0,1,0,0,1,0}, /*11*/{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1}, /*12*/{0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0}, /*13*/{0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0}, /*14*/{0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1}, /*15*/{0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0}, }; public int[][] getData() { return aja; } public int getNumOfShortestPahtes(int[][] edge) { int len = edge.length; int[] step = new int[len]; int[] path = new int[len]; path[0] = 1; Queue<Integer> q = new LinkedList<Integer>(); q.add(0); //将起点放入 while(!q.isEmpty()) { int element = q.remove(); for(int j=1; j<len; j++) { if(edge[element][j] == 1) { if(step[j] == 0 || step[j] > step[element] + 1) { step[j] = step[element] + 1; path[j] = path[element]; q.add(j); } else if(step[j] == step[element] + 1) path[j] += path[element]; } } } return path[len-1]; } }
标签:
原文地址:http://www.cnblogs.com/little-YTMM/p/5448652.html