PAT1030 Travel Plan (30)---DFS

(一)题意

题目链接:https://www.patest.cn/contests/pat-a-practise/1030

1030. Travel Plan (30)

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
(二)题解
这道题是PAT最很喜欢出的一道求最短路的变形题。解法有很多种,但鉴于我做上一道LeetCode花费了过多的经历,而且还要面对导师催论文的压力,我只写一种解法。
直接用DFS寻找从源点到目的地的所有路径中距离最短且花费最少的路径,用path记录路径。
DFS很方便可以记录路径,是借助深度deep作为路径下标。
代码如下:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define maxn 510
 4 #define For(I,A,B) for(int I = (A); I < (B); I++)
 5 int n,m,s,d;
 6 bool vis[maxn];
 7 int dist[maxn][maxn];
 8 int cost[maxn][maxn];
 9 int cur_path[maxn],path[maxn];
10 int mindis,minc,len;
11 
12 void dfs(int node,int dis,int cc,int deep)
13 {
14     cur_path[deep] = node;
15     if(node == d)
16     {
17         if(dis < mindis)
18         {
19             mindis = dis;
20             minc = cc;
21             len = deep;
22             For(i,0,deep + 1)
23                 path[i] = cur_path[i];
24         }
25         else if (dis == mindis && cc < minc)
26         {
27             mindis = dis;
28             minc = cc;
29             len = deep;
30             For(i,0,deep + 1)
31                 path[i] = cur_path[i];
32         }
33         return;
34     }
35     For(i,0,n)
36     {
37         if(!vis[i] && dist[i][node] != -1)
38         {
39             vis[i] = 1;
40             dfs(i,dis + dist[i][node],cc + cost[node][i],deep + 1);
41             vis[i] = 0;
42         }
43     }
44 }
45 int main()
46 {
47     //freopen("1030.in","r",stdin);
48     while(scanf("%d%d%d%d",&n,&m,&s,&d) != EOF)
49     {
50         int t1,t2;
51         mindis = minc = INT_MAX;
52         For(i,0,n)
53             For(j,0,n)
54             dist[i][j] = -1;
55         For(i,0,m)
56         {
57             scanf("%d%d",&t1,&t2);
58             scanf("%d%d",&dist[t1][t2],&cost[t1][t2]);
59             dist[t2][t1] = dist[t1][t2];
60             cost[t2][t1] = cost[t1][t2];
61         }
62         vis[s] = 1;
63         dfs(s,0,0,0);
64         For(i,0,len + 1)
65             cout<<path[i]<<" ";
66         cout<<mindis<<" "<<minc<<endl;
67     }
68     return 0;
69 }

相似的题目还包括PAT1018(https://www.patest.cn/contests/pat-a-practise/1018)和PAT1003(https://www.patest.cn/contests/pat-a-practise/1003


原文地址:https://www.cnblogs.com/xiaozhuyang/p/6119865.html