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
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(甲级)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机主机内部由什么组成,计算机是由主机
- 下一篇: Freemarker静态化页面的使用