How many Paths is thereTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1339 Accepted Submission (s): 468
Problem Description OOOCCC1 is a software Engineer who have to ride to the work place every Monday through Friday. For a long period, he went to office with the shortest path because he loves to sleep late ... Time goes by, he find that he should has some changes as you could see, always riding with the same path is boring.
One day, Oooccc1 got an idea! Why could I take another path? Tired at all the tasks he got, he got no time to carry it out. As a best friend of He, you ' re going-to-help him!
Since OOOCCC1 is now getting up earlier, he's glad to take those paths, which is a little longer than the shortest one. To is precisely, you is going to find all the second shortest paths.
You would is given a directed graph G, together with the start point S which stands for OOOCCC ' 1 he house and target Poin T E presents his office. And there is no cycle in the graph. Your task is to tell him how long was these paths and how many there was.
Inputthere is some cases. Proceed till the end of file.
The first line of all case is three integers n, M, S, E (3 <= N <=, 0 <= S, e <n)
N stands for the nodes in then graph, M stands for the number of edges, S-stands for the start point, and E-stands for the End point.
Then M lines follows to describe the Edges:x y W. x stands for the start point, and Y stands for another point, W stands For the length between x and Y.
All the nodes is marked from 0 to N-1.
Outputfor each case,please output of the length and count for those second shortest paths on one line. Separate them with a single space.
Sample Input
3 3 0 20 2 50 1 41 2 2
Sample Output
6 1
Test Instructions Description:
a direction graph of n points and m edges is given. The number of short and minor short circuits is obtained.
Problem Solving Ideas:
We can ask for the shortest circuit, but how do we ask for a short circuit? We can change the mincost array with the shortest short-circuit length and the DP array with the shortest number of records into two-dimensional, 1 for the shortest, and 2 for the secondary short. If there is a shorter path than the shortest path, the least-shorted information is assigned to the secondary short-circuit, and then the information is changed.
In addition we will encounter the following four scenarios when updating the mincost array:
1) The length of a road from the current point of arrival to I is shorter than the shortest path to the I point: To assign a short-circuit to the least-shorted information, and assign the information of this road to the quickest way;
2) The length of a route from the current point of arrival to I is consistent with the shortest length of the point of arrival: The number of types that arrive at the current point and a minimum length is added to the number of the least-shorted species to reach I, i.e. dp[i][1]+=dp[now.pos][now.ismin], Where Now.pos represents the current point, now.ismin indicates whether it is the shortest path;
3) The length of a path from the current point of arrival to I and the shortest short-circuit length to the I point, but at the same time shorter than the short: update the information of the secondary short-circuit;
4) The length of a road from the current point of arrival to I is consistent with the length of the longest road reaching I point: similar to 2), but only dp[i][1] is changed to dp[i][2].
Reference code:
#include <stack> #include <queue> #include <cmath> #include <cstdio> #include <cstring># Include<iostream> #include <algorithm>using namespace std;const double eps=1e-6;const int inf=0x3f3f3f3f; const int maxn=52;struct point{int pos,cost, ismin; BOOL operator < (const point &p) const//remove the shortest-most-shorted dot in the collection from the priority queue {if (p.cost!=cost) return p.cost& Lt;cost; Return p.pos<pos; } point (int pos,int cost,int ismin) {this->pos=pos; this->cost=cost; this->ismin=ismin; }};int n,m,s,e,mincost[maxn][3],dp[maxn][3],edge[maxn][maxn];bool used[maxn][3];void SPFA () {memset (Mincost,INF, sizeof (Mincost)); Memset (Dp,0,sizeof (DP)); memset (used,false,sizeof (used)); priority_queue<point>q;//priority Queue mincost[s][1]=0; Dp[s][1]=1; Q.push (Point (s,0,1)); while (!q.empty ()) {point now=q.top (); Q.pop (); if (Used[now.pos][now.ismin]) continue; Used[now.poS][now.ismin]=true; for (int i=0; i<n; i++) {if (edge[now.pos][i]==inf) continue; if (!used[i][1]&&now.cost+edge[now.pos][i]<mincost[i][1])//The length of a route from the current point of arrival to I is shorter than the shortest path to the I point { mincost[i][2]=mincost[i][1];//Update secondary short circuit dp[i][2]=dp[i][1]; Q.push (Point (i,mincost[i][2],2)); mincost[i][1]=now.cost+edge[now.pos][i];//update Shortest way dp[i][1]=dp[now.pos][now.ismin]; Q.push (Point (i,mincost[i][1],1)); } else if (!used[i][1]&&now.cost+edge[now.pos][i]==mincost[i][1])//The length of a route from the current point of arrival to I is consistent with the shortest length to the I point {Dp[i][1]+=dp[now.pos][now.ismin]; } else if (!used[i][2]&&now.cost+edge[now.pos][i]<mincost[i][2])//The length of a route from the current point of arrival to I and the shortest path to the I point, but at the same time shorter than the short {Mincost[i][2]=now.cost+edge[now.pos][i]; Dp[i][2]=dp[now.pos][now.ismin]; Q.push (Point (i,mincost[i][2],2)); } else if (!used[i][2]&&now.cost+edge[now.pos][i]==mincost[i][2])//The length of a path from the current point of arrival to I is consistent with the length of the longest path to the I point {Dp[i][2]+=dp[now.pos][now.ismin]; }}}}int Main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%d%d %d%d ", &n,&m,&s,&e)!=eof) {memset (edge)); for (int i=1; i<=m; i++) {int a,b,c; scanf ("%d%d%d", &a,&b,&c); edge[a][b]=c;//Graph} SPFA (); printf ("%d%d\n", mincost[e][2],dp[e][2]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 3191 How many Paths is there (SPFA)