Background
Tony, a clever boy, whose dream is travelling around world, has no sense of direction.The summer vacation is coming and Tony decides to see the world outside.But his the length of the vacation is limited, while he has a lot of homework needs to be done.So Tony wants to find a nearest place that he wants to visit.Sadly, there is no railway station in his hometown and he needs to arrive in other town adjoining first.
Input && Output
Input
There is three integer in the first line:
the first integer T represents the number of path;
the second integer S represents the number of town adjoining to Tony’s hometown;
the third integer D represents the number of the place which Tony want to visit.
Then there are T lines datum and each line has three integer:
the first integer and the second represent the identity of two places;
the third integer represents the time which Tony need to travel between the two places.
The T+2 line has S integer which represent the identity of the towns adjoining to Tony’s hometown.
The T+3 line has D integer which represent the place where Tony wants to go.
Output
The shortest time Tony will to spend to arrive any place he wants to visit.
Sample
Input
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10
Output
9
Dijkstra
We need:
- an array Dj to store the shortest path weight to every node;
- an array vis to mark the node we have expanded;
- an array of two dimensions to store the weight of each path.
Attentions:
- each array needs to be initialized;
- there maybe two paths of different weight between two adjoined places;
- the weight b to a is the same as a to b;
- we create an super source and the weight between super source and the node can be source is 0.
Core:
Dj[x] = min{Dj[x], Dj[inx]+m[inx][x]}
|
|