您的位置:首页 > 其它

PAT - 甲级 1111. Online Map (30)(Dijkstra+DFS)

2017-11-01 17:02 405 查看
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes
a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is
the length of the street; and time is the time taken to pass 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 the destination 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 among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination
Sample Input 1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5


给定一些结点和结点之间的连线,连线有2个属性:距离和时间。

题目要求:

1.求出给定两个点a和b之间的最短路径

2.求出给定两个点a和b之间的所耗费时间最少的路径

考虑到最短路不唯一,则输出耗费时间最短的路线

考虑到耗费时间最短的路径不唯一,输出经过节点个数最少的路径即可。

解决方法:

1.Dijkstra求出最短路,并且记录路径,求解过程中同时记录耗费时间即可求出唯一的路。

2.Dijkstra求出耗费时间最少的路,记录所有可能的路径,通过DFS在所有的路径中找出经过节点最少的一条即可。

#include<iostream>
#include<cstdio>
#include<vector>
#define INF 999999
using namespace std;

int N, M;
int v1, v2, ow, l, t;
int e[510][510], w[510][510];
int dist[510], dis[510], dit[510], prel[510];
int visl[510], vist[510];
vector<int> pret[510];
vector<int> pathl, patht, temp;
int s,d;
int minn = INF;

void dfss(int v){
pathl.push_back(v);
if(v == s){
return ;
}
dfss(prel[v]);
}

void dfst(int v){
temp.push_back(v);
if(v == s){
if(temp.size() < minn){
minn = temp.size();
patht = temp;
}
temp.pop_back();
return ;
}
for(int i = 0; i < pret[v].size(); i++){
dfst(pret[v][i]);
}
temp.pop_back();
}

int main(){
freopen("input.txt", "r", stdin);
while(scanf("%d%d",&N, &M) != EOF){

fill(dis, dis + 510, INF);
fill(dit, dit + 510, INF);
fill(visl, visl + 510, 0);
fill(vist, vist + 510, 0);
fill(dist, dist + 510, 0);
fill(e[0], e[0] + 510 * 510, INF);
fill(w[0], w[0] + 510 * 510, INF);

for(int i = 0; i < M; i++){
scanf("%d%d%d%d%d",&v1, &v2, &ow, &l, &t);
e[v1][v2] = l;
w[v1][v2] = t;
if(!ow){
e[v2][v1] = l;
w[v2][v1] = t;
}
}
scanf("%d%d",&s, &d);
// 最短路
dis[s] = 0;
for(int i = 0; i < N; i++){
prel[i] = i;
}
for(int i = 0; i < N; i++){
int x = -1, minl = INF;
for(int j = 0; j < N; j++){
if(!visl[j] && dis[j] < minl){
minl = dis[j];
x = j;
}
}
if(x == -1) break;
visl[x] = 1;
for(int j = 0; j < N; j++){
if(!visl[j] && e[x][j] != INF){
if(dis[x] + e[x][j] < dis[j]){
dist[j] += w[x][j];
dis[j] = dis[x] + e[x][j];

prel[j] = x;
}
else if(dis[x] + e[x][j] == dis[j] && dist[x] + w[x][j] < dist[j]){
dist[j] = dist[x] + w[x][j];
prel[j] = x;
}
}
}
}
dfss(d);

// 最短时间
dit[s] = 0;
for(int i = 0; i < N; i++){
int x = -1, mint = INF;
for(int j = 0; j < N; j++){
if(!vist[j] && dit[j] < mint){
mint = dit[j];
x = j;
}
}
if(x == -1) break;
vist[x] = 1;
for(int j = 0; j < N; j++){
if(!vist[j] && w[x][j] != INF){
if(dit[x] + w[x][j] < dit[j]){
dit[j] = dit[x] + w[x][j];
pret[j].clear();
pret[j].push_back(x);
}else if(dit[x] + w[x][j] == dit[j]){
pret[j].push_back(x);
}
}
}
}
dfst(d);
// output
printf("Distance = %d",dis[d]);
if(patht == pathl){
printf("; Time = %d: ",dit[d]);
for(int i = pathl.size()-1; i >= 0 ; i--){
printf("%d",pathl[i]);
if(i != 0){
printf(" -> ");
}
}
}else{
printf(": ");
for(int i = pathl.size()-1; i >= 0; i--){
printf("%d",pathl[i]);
if(i != 0)
printf(" -> ");
}

printf("\nTime = %d: ",dit[d]);
for(int i = patht.size()-1; i >= 0; i--){
printf("%d",patht[i]);
if(i != 0)
printf(" -> ");
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: