PAT 1111. Online Map (30)
Input our current position and a destination, an online map canrecommend several paths. Now your job is to recommend two paths toyour user: one is the shortest, and the other is the fastest. It isguaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first linegives two positive integers N (2 <= N <= 500), and M, being the totalnumber of streets intersections on a map, and the number of streets, Then M lines follow, each describes a street in theformat:
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N-1) of the two ends of thestreet; one-way is 1 if the street is one-way from V1 to V2, or 0 ifnot; length is the length of the street; and time is the time taken topass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to thedestination with distance D in the format:
Distance = D: source -> v1 -> ... -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w1 -> ... -> destination
In case the shortest path is not unique, output the fastest one amongthe shortest paths, which is guaranteed to be unique. In case thefastest path is not unique, output the one that passes through thefewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print themin one line in the format:
Distance = D; Time = T: source -> u1 -> ... -> destination
原題
考え方:この問題は実は難しくなくて、面倒です.Dijkstraアルゴリズムを用いて最短経路を求めればよい.ただし、ここでの最短パスは、それぞれ最短長(同じ長さの場合は短い)と最短時間(同じ時間が経過する点が少ない)であり、この2つの量はprev配列を使用してパスを保存する必要があります.同じ長さの値に遭遇した場合は、2つのパスを作成して比較する必要があります.したがって、リカバリに注意する必要があります.
正しいコード:
vectorを使用してパスを格納し、setを使用して最小パスを取得します.構造がわかりやすい.なぜか問題があります.
Input Specification:
Each input file contains one test case. For each case, the first linegives two positive integers N (2 <= N <= 500), and M, being the totalnumber of streets intersections on a map, and the number of streets,
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N-1) of the two ends of thestreet; one-way is 1 if the street is one-way from V1 to V2, or 0 ifnot; length is the length of the street; and time is the time taken topass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to thedestination with distance D in the format:
Distance = D: source -> v1 -> ... -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w1 -> ... -> destination
In case the shortest path is not unique, output the fastest one amongthe shortest paths, which is guaranteed to be unique. In case thefastest path is not unique, output the one that passes through thefewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print themin one line in the format:
Distance = D; Time = T: source -> u1 -> ... -> destination
原題
考え方:この問題は実は難しくなくて、面倒です.Dijkstraアルゴリズムを用いて最短経路を求めればよい.ただし、ここでの最短パスは、それぞれ最短長(同じ長さの場合は短い)と最短時間(同じ時間が経過する点が少ない)であり、この2つの量はprev配列を使用してパスを保存する必要があります.同じ長さの値に遭遇した場合は、2つのパスを作成して比較する必要があります.したがって、リカバリに注意する必要があります.
int t=prev[i];
ans=get_path(prev,i);
prev[i]=v1;
auto tt=get_path(prev,i);
if(f2(tt)
正しいコード:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
vectorを使用してパスを格納し、setを使用して最小パスを取得します.構造がわかりやすい.なぜか問題があります.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include