Find the shortest path with n (1 < n <= 100) nodes in the direction graph, node 1 to node n, and the number of shortest path.
Input
The first line has 2 integers n and m (0 < M < 3000), and the next m row has three integers per line u,v,w nodes u to v have a side (w<100000) with a right of W.
Output
The output is only one row, the shortest path between node 1 to node N and its number (separated by spaces), and output-1 0 if there is no path between 1 and N.
Sample Input
3 3
1 2 10
2 3 15
1 3 25
Sample Output
25 2
Analysis: There may be multiple paths between two adjacent points in the subject, but each road has the same length, which is not stated in the question.
Idea: Do two times Dijkstra can, for the first time to find out the point to the shortest distance from the source point. The second time is used to count the shortest number of bars. There are two types of statistics: one for the shortest-circuit between the middle point, and the other for the shortest path between the middle vertex.
AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <vector> #include <map> #include <algorithm>using namespace Std;int cost[101][101],path [101] [101],low[101],dis[101],num[101]={};//low and dis arrays are two times Dijkstra of the current I point to the source point of the shortest path bool vis[101]; Path holds two points between the number of route bars const int maxn=10000001; Num save point to Source point number of shortest paths int main () {int n,m,i,j,k; memset (vis,false,sizeof (VIS)); scanf ("%d%d", &n,&m); for (i=0;i<101;++i) {//Initialize cost matrix for (J=0;J<101;++J) {cost[i][j]=maxn; }} for (i=0;i<m;++i) {//read data int a,b,c; scanf ("%d%d%d", &a,&b,&c); Cost[a][b]=c; path[a][b]++; } vis[1]=true; The first Dijkstra the shortest road for (i=2;i<=n;++i) low[i]=cost[1][i]; for (i=2;i<=n;++i) {int minn=maxn+1; for (j=1;j<n;++j) {if (!vis[j] && low[j]<minn) {minn=low[j];k=j; }} vis[k]=true; for (J=1;J<=N;++J) {if (!vis[j]) {low[j]=min (low[j],low[k]+cost[k][j]); }}} memset (Vis,false,sizeof (VIS)); Vis[1]=true; The second Dijkstra to find the shortest strip number, the shortest short-circuit divided into two categories, a kind of direct, a class with the help of the middle point for (I=2;i<=n;++i) dis[i]=cost[1][i]; for (I=2;i<=n;++i)//First Class Direct if (Dis[i]==low[i]) num[i]=path[1][i]; for (i=2;i<=n;++i) {int minn=maxn+1; for (J=1;J<=N;++J) {if (!vis[j] && dis[j]<minn) {minn=dis[j];k=j; }} vis[k]=true; for (J=1;J<=N;++J) {if (!vis[j]) {dis[j]=min (dis[j],dis[k]+cost[k][j]); } if (Dis[k]+cost[k][j]==low[j])//Statistics Class II, the number of paths reached through the middle point num[j]+=num[k]*path[k][j]; }} if (LOW[N]>=MAXN) printf ("-1 0\n "); else printf ("%d%d\n", low[n],num[n]); return 0;}
eoj1818 Dijkstra to find the shortest path and its number