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
Note
dijstra思路:
0)所需结构体
dis[ i ]表示从起点到 i 点的已知最短距离
visit[ i ]表示i点是否为已知点(或称已纳入点)
e[ i ][ j ]表示i,j两城市间的距离
vector< int > pre[ i ]表示到达i点的所有最短路径中的i的前一个结点
(pre在普通dij算法中可直接用数组,pre[ i ]表示最短路径时,i点的前一个结点)
1)
找到一个起点(将起点的dis置为0即可)
2)
找到一个最容易到达且没纳入的点u(即未知点)
3)
纳入该点u
4)
看看从该点u出发能否获得更短路径(即更新)
(注:要求visit[v] == false && e[u][v] != INF,后者好理解,前者代表不去遍历从u出发到达已纳入点的路径。不加visit判断也可以,那样就会向回寻找路径,如经过a到达b点,现在从b点出发又去判断a点,则造成了aba的回路,一定比正常到达a的距离长,所以加上判断就可跳过这种情况)
5)
如果更短,则更新pre
如果相等,则在pre中添加该点(dfs时使用)
如果不可以,则不变
至此,所有最短路径已存储于pre中
dfs思路:
0)所需结构体
minpath和mincost存储最终的最短路径和最小花费
temppath和tempcost存储当前的路径和花费
cost[ i ][ j ]表示i到j的花费
1)
dfs从终点开始进行回溯,因为起点是唯一的,回溯可省下visit数组
2)
将该点压入路径中
3)
如果是起点,则表示这条路径已经完整了,就可以开始计算他的总花费,与mincost作比较,保留花费较小的路径于minpath,然后弹出起点并return
4)
如果不是起点,则表示这条路径还不完整,则根据pre继续dfs
5)
遍历完通过这个点的路径后就弹出当前点
此时,minpath和mincost中存的就是最短路径和最小花费
code
#include <iostream>
#include <algorithm>
#include <vector>
#define INF 999999
using namespace std;
int n, m, s, d;
vector<int> pre[501];
int cost[501][501] = { 0 };
vector<int> minpath, temppath;
int mincost = 999999;
void dfs(int v) {
temppath.push_back(v);
if (v == s) {
int tempcost = 0;
for (int i = temppath.size() - 1; i > 0; i--)
{
tempcost += cost[temppath[i]][temppath[i-1]];
}
if (tempcost < mincost) {
mincost = tempcost;
minpath = temppath;
}
temppath.pop_back();
return;
}
for (int j = 0; j < pre[v].size(); j++)
{
dfs(pre[v][j]);
}
temppath.pop_back();
}
int main() {
int dis[501];
bool visit[501] = { false };
int e[501][501] = { 0 };
fill(e[0], e[0] + 501 * 501, INF);
fill(cost[0], cost[0] + 501 * 501, INF);
fill(dis, dis + 501, INF);
cin >> n >> m >> s >> d;
for (int i = 0; i < m; i++)
{
int a, b, l, c;
cin >> a >> b >> l >> c;
e[a][b] = e[b][a] = l;
cost[a][b] = cost[b][a] = c;
}
/*dijkstra*/
pre[s].push_back(s);
dis[s] = 0;//设置起点
for (int i = 0; i < n; i++)
{
int u = -1, minn = 999999;
for (int j = 0; j < n; j++)
{
if (visit[j] == false && dis[j] < minn)
{
u = j;
minn = dis[j];
}
}
if (u == -1) break;
visit[u] = true;
/*将u作为出发点,更新距离*/
for (int v = 0; v < n; v++)
{
if (visit[v] == false && e[u][v] != INF)//不加visit判断也可以,那样就会向回寻找路径,如经过a到达b点,现在从b点出发又去判断a点,则造成了aba的回路,一定比正常到达a的距离长,所以加上判断就可调过这种情况
{
if (dis[u] + e[u][v] < dis[v])
{
dis[v] = dis[u] + e[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if (dis[u] + e[u][v] == dis[v])
{
pre[v].push_back(u);
}
}
}
}
dfs(d);
for (int i = minpath.size() - 1; i >= 0; i--)
{
cout << minpath[i] << " ";
}
cout << dis[d] << " " << mincost;
return 0;
}