How Many PathsAre There
TimeLimit: 2000/1000 MS (Java/Others) Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 1820 Accepted Submission(s): 646
ProblemDescription
oooccc1 is a Software Engineerwho has to ride to the work place every Monday through Friday. For a longperiod, he went to office with the shortest path because he loves to sleeplate…Time goes by, he find that he should have 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? Tiredat all the tasks he got, he got no time to carry it out. As a best friend ofhis, you’re going to help him!
Since oooccc1 is now getting up earlier, he is glad to take thosepaths, which are a little longer than the shortest one. To be precisely, youare going to find all the second shortest paths.
You would be given a directed graph G, together with the startpoint S which stands for oooccc’1 his house and target point E presents hisoffice. And there is no cycle in the graph. Your task is to tell him how longare these paths and how many there are.
Input
There are some cases. Proceed till the endof file.
The first line of each case is three integers N, M, S, E (3 <= N <= 50, 0<= S , E <N)
N stands for the nodes in that graph, M stands for the number of edges, Sstands 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 startpoint, and y stands for another point, w stands for the length between x and y.
All the nodes are marked from 0 to N-1.
Output
For each case,please output the length andcount for those second shortest paths in one line. Separate them with a singlespace.
Sample Input
3 3 0 2
0 2 5
0 1 4
1 2 2
SampleOutput
6 1
Author
ZSTU
Source
HDU2009-12 Programming Contest
Recommend
lcy
算法分析:
题意:次短路径和次短路径数量
哎,竟然要用dij,以为凭借spfa和floyed就可以打便ACM了呢,dij求次短路径有它本身的优势,思路具体如下:
设dis[i][1]为到i的最短路径,dis[i][2]为到i的次短路径,cnt[i][1]为到i的最短路径条数,cnt[i][2]为到i的次短路径条数。若找到一个点x,有四种情况:
(1) dis[x][1]+w(x,v)<dis[v][1],(找到一条比”最短路“更加短的路)
若本来的dis[v][0]存在(不等于INF),则要先使dis[v][2]=dis[v][1],cnt[v][2]=cnt[v][1],然后更新dis[v][1]和cnt[v][1];
(2)dis[x][1]+w(x.v)==dis[v][1],(找到最短路)
则更新cnt[v][1];
(3)dis[v][1]<dis[x][1]+w(x,v)<dis[v][2],(找到一条比”最短“长,比”次短“短的路,更新”次短“),则更新dis[v][2]和cnt[v][2];
(4)dis[x][1]+w(x,v)==dis[v][2](找到次短路),
则更新cnt[v][2]
代码实现: