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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1030 Travel Plan(甲级)

發布時間:2024/7/23 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1030 Travel Plan(甲级) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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
只使用dijkstra

#include<iostream> #include<memory.h> #include<vector> #include<algorithm> using namespace std; int n, m, st, ed; const int inf = 0x3f3f3f3f; const int maxn = 501;//n為頂點數,m為邊數,st和ed分別為起點和終點 //g為矩陣,weight為花費 //d記錄最短距離,w記錄最小花費 int g[maxn][maxn]{inf}; int weight[maxn][maxn]; int w[maxn]; int d[maxn]; bool vist[maxn]{ false }; int path[maxn];//記錄父節點 void dijistra(int s) {fill(w, w+maxn, inf);fill(d, d + maxn, inf);for (int i = 0; i < n; i++)path[i] = i;d[s] = 0;w[s] = 0;for (int i = 0; i < n; i++){int u = -1;int min = inf;for (int j = 0; j < n; j++){if (!vist[j] && d[j] < min){min = d[j];u = j;}}if (u == -1)return;vist[u] = true;for (int j = 0; j < n; j++){if (!vist[j] && g[u][j] != inf){if (d[j] > g[u][j] + d[u]){d[j] = g[u][j] + d[u];w[j] = weight[u][j] + w[u];path[j] = u;}else if (d[j] == g[u][j] + d[u]){if (w[j] > weight[u][j] + w[u]){w[j] = weight[u][j] + w[u];path[j] = u;}}}}} } void dfs(int v) {if (v == st){cout << v << " ";return;}dfs(path[v]);cout << v<<" ";return; } int main() {cin >> n >> m >> st >> ed;fill(g[0], g[0] + maxn * maxn, inf);for (int i = 0; i < m; i++){int u, v;cin >> u >> v;cin >> g[u][v];cin >> weight[u][v];g[v][u] = g[u][v];weight[v][u] = weight[u][v];}dijistra(st);dfs(ed);cout << d[ed]<<" ";cout << w[ed]; }

dijkstra+dfs

#include<iostream> #include<memory.h> #include<vector> using namespace std; int n, m, st, ed; const int inf = 0x3f3f3f3f; const int maxn = 501;//最大頂點數//n為頂點數,m為邊數,st和ed分別為起點和終點 //g為矩陣,weight為花費 //d記錄最短距離;mincost記錄最短路徑上的最小花費 int g[maxn][maxn]{inf}; int weight[maxn][maxn]; int mincost = inf; int d[maxn]; bool vist[maxn]{ false }; vector<int>path[maxn];//前驅 vector<int>temp,path_n;//臨時路徑和最優路徑 void dijistra(int s) {fill(d, d + maxn, inf);d[s] = 0;for (int i = 0; i < n; i++){int u = -1;int min = inf;for (int j = 0; j < n; j++){if (!vist[j] && d[j] < min){min = d[j];u = j;}}if (u == -1)return;vist[u] = true;for (int j = 0; j < n; j++){if (!vist[j] && g[u][j] != inf){if (d[j] > g[u][j] + d[u]){d[j] = g[u][j] + d[u];path[j].clear();path[j].push_back(u);}else if (d[j] == g[u][j] + d[u]){path[j].push_back(u); }}}} } void dfs(int v) {if (v == st){temp.push_back(v);int tempcost = 0;for (int i = temp.size() - 1; i > 0; i--){int id = temp[i];int idnext = temp[i - 1];tempcost += weight[id][idnext];}if (mincost > tempcost){mincost = tempcost;path_n = temp;}temp.pop_back();return;}temp.push_back(v);for (int i = 0; i < path[v].size(); i++){dfs(path[v][i]);}temp.pop_back(); } int main() {cin >> n >> m >> st >> ed;fill(g[0], g[0] + maxn * maxn, inf);memset(weight, 0, sizeof(weight));for (int i = 0; i < m; i++){int u, v;cin >> u >> v;cin >> g[u][v];cin >> weight[u][v];g[v][u] = g[u][v];weight[v][u] = weight[u][v];}dijistra(st);dfs(ed);for (int i =path_n.size()-1; i >= 0; i--){cout << path_n[i] << " ";}cout << d[ed]<<" "<<mincost; }

總結

以上是生活随笔為你收集整理的1030 Travel Plan(甲级)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。