PAT1030 Travel Plan (30)---DFS

(一)題意node

題目連接:https://www.patest.cn/contests/pat-a-practise/1030c++

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.ide

Input Specification: spa

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:code

City1 City2 Distance Costorm

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

Output Specification: ci

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.get

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/1003input

相關文章
相關標籤/搜索