日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【讲解】1030 Travel Plan (30 分)【DFS】_41行代码Ac

發(fā)布時(shí)間:2024/2/28 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【讲解】1030 Travel Plan (30 分)【DFS】_41行代码Ac 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

立志用最少的代碼做最高效的表達(dá)


PAT甲級最優(yōu)題解——>傳送門


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


采用vector做dfs的參數(shù),同時(shí)求出最短距離和路徑。 具體邏輯請讀者去代碼中體會。


#include<bits/stdc++.h> using namespace std;int G[510][510], vis[510], cost[510][510]; //地圖、vis數(shù)組、走每條路的花費(fèi)成本 int n, m, s_c, d_c; //城市數(shù)、道路數(shù)、起點(diǎn)、終點(diǎn) vector<int>fin; //存儲最終路線 int min_dis = 0x3f3f3f3f, min_cost = 0x3f3f3f3f; //最短距離和花費(fèi) void dfs(int now_dis, int now_cost, vector<int>v) {int s = v.back(); //最后一個(gè)元素為當(dāng)前位置 if(now_dis > min_dis) return; //剪枝if(s == d_c) { //邊界條件 if(now_dis == min_dis) //距離相同則比較花費(fèi) if(now_cost > min_cost) return;min_dis = now_dis; min_cost = now_cost;fin = v;return; }for(int i = 0; i < n; i++) //逐個(gè)點(diǎn)遍歷 if(vis[i] == 0 && G[s][i]>0) { //如果沒訪問過且連通v.push_back(i); //v數(shù)組壓入該點(diǎn) vis[i] = 1; //置1表示已訪問 dfs(now_dis+G[s][i], now_cost+cost[s][i], v);vis[i] = 0; //回溯 v.pop_back();} }int main() {cin >> n >> m >> s_c >> d_c;for(int i = 0; i < m; i++) {int c1, c2, dis, val; cin >> c1 >> c2 >> dis >> val;G[c1][c2] = G[c2][c1] = dis; //構(gòu)建地圖和距離 cost[c1][c2] = cost[c2][c1] = val; //花費(fèi) }vector<int>v; v.push_back(s_c); //壓入起點(diǎn) vis[s_c] = 1;dfs(0, 0, v); //距離、花費(fèi)、路線 for(int i = 0; i < fin.size(); i++) cout << fin[i] << ' ';cout << min_dis << ' ' << min_cost << '\n';return 0; }

耗時(shí):

總結(jié)

以上是生活随笔為你收集整理的【讲解】1030 Travel Plan (30 分)【DFS】_41行代码Ac的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。