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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【PAT】【spfa + dfs】1030 Travel Plan (30 分)

發(fā)布時(shí)間:2023/12/8 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【PAT】【spfa + dfs】1030 Travel Plan (30 分) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接: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

?//?解題思路:

//?經(jīng)典的最短路徑算法

//?求出所有的同等最短路徑,進(jìn)行dfs找到花費(fèi)最少的唯一一條即可

//?這里用的spfa?+?dfs,?建議沒負(fù)權(quán)都直接用dijkstra,這里心血來潮寫了spfa

#include <bits/stdc++.h> using namespace std;// 鏈?zhǔn)角跋蛐谴鎴D,其實(shí)用鄰接表或鄰接矩陣即可,同心血來潮。。。 const int maxn = 5e2 + 5; struct edge {int v, dis, next; }e[maxn * maxn];int head[maxn], size = 0; void insert(int &u, int &v, int &cos, int &dis){e[size] = (edge){v, dis, head[u]};head[u] = size++;e[size] = (edge){u, dis, head[v]};head[v] = size++; } // dfs中要直接查詢兩點(diǎn)之間邊的cost, 所以要另外直接存在一個(gè)矩陣中 int dis[maxn], ori_cost[maxn][maxn]; vector<int> pre[maxn], temppath; bool vis[maxn]; // spfa求解最短路 queue<int> q; void spfa(int s, int d) {dis[s] = 0;vis[s] = true;q.push(s);int u;while(!q.empty()){u = q.front();q.pop(), vis[u] = false;for(int v, i = head[u]; ~i; i = e[i].next){v = e[i].v;// 更新最短距離,且將pre路徑重置if ((e[i].dis + dis[u]) < dis[v]){if(!vis[v]) q.push(v), vis[v] = true;dis[v] = e[i].dis + dis[u];pre[v].clear();pre[v].push_back(u);}// 相等則也加入并列路徑,用來之后求cost最少路徑else if((e[i].dis + dis[u]) == dis[v]){pre[v].push_back(u);}}} }int n, m, s, d; // dfs在最短路徑中深搜得到最少花費(fèi)的那條路徑,用棧保存 int anscost = 1e8; stack<int> sta, ans; void dfs(int v, int tempcost){sta.push(v);if(v == s){if(tempcost < anscost){anscost = tempcost;ans = sta;}sta.pop();return;}for(int i = 0; i < pre[v].size(); i++){dfs(pre[v][i], tempcost + ori_cost[v][pre[v][i]]);}sta.pop(); }int main() {// 初始化memset(head, -1, sizeof(head));memset(dis, 0x3f, sizeof(dis));// 讀入數(shù)據(jù)cin >> n >> m >> s >> d;int u, v, cost, distance;for(int i = 0; i < m; i++){scanf("%d %d %d %d", &u, &v, &distance, &cost);insert(u, v, cost, distance);ori_cost[u][v] = ori_cost[v][u] = cost;}spfa(s, d);dfs(d, 0);// 輸出答案路徑 和 結(jié)果while(!ans.empty()){printf("%d ", ans.top());ans.pop();}printf("%d %d\n", dis[d], anscost);return 0; }

參考了柳婼的題解:https://blog.csdn.net/liuchuo/article/details/52315580?

總結(jié)

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

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