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
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXV = 510;
const int INF = 1000000000;
int G[MAXV][MAXV],cost[MAXV][MAXV],n,m,st,ed;
int c[MAXV],pre[MAXV],d[MAXV];
bool vis[MAXV] ={false};
void Dijkstra(int s)
{
fill(c, c + MAXV, INF);
fill(d, d + MAXV, INF);
for (int i = 0; i < n; i++)
{
pre[i] = i;
}
d[s] = 0;//起点到自身的距离为0
c[s] = 0;//起点到自身的花费为0
for (int i = 0; i < n; i++)
{//循环n次
int u = -1, MIN = INF;
for (int j = 0; j < n; j++)
{
if (vis[j] == false && d[j] < MIN) {
u = j;
MIN = d[j];
}
}
if (u == -1) return;//说明其他的顶点都不与起点相连通
vis[u] = true;
for (int v = 0; v < n; v++) {
if (vis[v] == false && G[u][v] != INF)
{
if (d[u] + G[u][v] < d[v]) {
d[v] = d[u] + G[u][v];
c[v] = c[u] + cost[u][v];
pre[v] = u;
}
else if(d[u]+G[u][v]==d[v])
{
if (c[u] + cost[u][v] < c[v])
{
c[v] = c[u] + cost[u][v];
pre[v] = u;
}
}
}
}
}
}
void DFS(int v)//打印路径 v为终点坐标
{
if (v == st)
{
printf("%d ", v);
return;
}
DFS(pre[v]);
printf("%d ", v);
}
int main()
{
scanf("%d%d%d%d", &n, &m, &st, &ed);
fill(G[0], G[0] + MAXV * MAXV, INF);
fill(cost[0], cost[0] + MAXV * MAXV, INF);
int u, v;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &u, &v);
scanf("%d%d", &G[u][v], &cost[u][v]);
G[v][u] = G[u][v];
cost[v][u] = cost[u][v];
}
Dijkstra(st);
DFS(ed);
printf("%d %d\n", d[ed], c[ed]);
return 0;
}
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXV = 510;
const int INF = 100000000;
int n, m, st, ed;
int G[MAXV][MAXV], cost[MAXV][MAXV];
int d[MAXV], minCost = INF;
bool vis[MAXV] = { false };
vector<int>pre[MAXV];
vector<int>tempPath, path;
void Dijkstra(int s)
{
fill(d, d + MAXV, INF);
d[s] = 0;
for (int i = 0; i < n; i++)
{
int u = -1, MIN = INF;
for (int j = 0; j < n; j++)
{
if (vis[j] == false && d[j] < MIN)
{
u = j;
MIN = d[j];
}
}
if (u == -1) return;
vis[u] = true;
for (int v = 0; v < n; v++)
{
if (vis[v] == false && G[u][v] != INF) {
if (d[u] + G[u][v] < d[v]) {
d[v] = d[u] + G[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if (d[u] + G[u][v] == d[v])
{
pre[v].push_back(v);
}
}
}
}
}
void DFS(int v) {
if (v == st) {//递归边界,到达叶子结点(路径起点)
tempPath.push_back(v);
int tempCost = 0;
for (int i = tempPath.size() - 1; i > 0; i--)
{
//当前结点id、下个结点idNext
int id = tempPath[i], idNext = tempPath[i - 1];
tempCost += cost[id][idNext];
}
if (tempCost < minCost) {
minCost = tempCost;
path = tempPath;
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for (int i = 0; i < pre[v].size(); i++)
{
DFS(pre[v][i]);
}
tempPath.pop_back();
}
int main()
{
scanf("%d%d%d%d", &m, &n,&st, &ed);
int u, v;
fill(G[0], G[0] + MAXV * MAXV, INF);
fill(cost[0], cost[0] + MAXV * MAXV, INF);
for (int i = 0; i < m; i++)
{
scanf("%d%d", &u, &v);
scanf("%d%d", &G[u][v], &cost[u][v]);
G[v][u] = G[u][v];
cost[v][u] = cost[u][v];
}
Dijkstra(st);
DFS(ed);
for (int i = path.size() - 1; i >= 0; i--)
{
printf("%d ", path[i]);
}
printf("%d %d\n", d[ed], minCost);
return 0;
}
复习:
这道题原本以为是只能用Dijkstra和DFS结合起来才能做,后面发现其实Dijkstra也能做出来,因为最短且花费最小的路径只有一条,所以只需要记录路径上每个节点的前驱节点就可以了。如果要打印多条路径就只能加上DFS的方法了。这道题还是很经典,要多看看。注意记录前驱节点的数组要初始化一下。
二刷代码:
#include <iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m, st, ed;
const int maxn = 510,INF=0x7fffffff;
int G[maxn][maxn], d[maxn],cost[maxn][maxn],c[maxn],pre[maxn];
bool vis[maxn] = { false };
void Dijkstra(int s)
{
fill(d, d + maxn, INF);
fill(c, c + maxn, INF);
d[s] = 0;
c[s] = 0;
for (int i = 0; i < n; i++)
{
pre[i] = i;
}
for (int i = 0; i < n; i++)
{
int u = -1, min = INF;
for (int j = 0; j < n; j++)
{
if (vis[j] == false && d[j] < min)
{
u =j;
min = d[j];
}
}
if (u == -1) return;
vis[u] == true;
for (int v = 0; v < n; v++)
{
if (vis[v] == false && G[u][v] != INF)
{
if (d[u] + G[u][v] < d[v])
{
d[v] = d[u] + G[u][v];
c[v] = c[u] + cost[u][v];
pre[v] = u;
}
else if (d[u] + G[u][v] == d[v])
{
if (c[u] + cost[u][v] < c[v])
{
c[v] = c[u] + cost[u][v];
pre[v] = u;
}
}
}
}
}
}
void DFS(int s)
{
if (s == st)
{
printf("%d ", s);
return;
}
DFS(pre[s]);
printf("%d ", s);
}
int main()
{
scanf("%d%d%d%d", &n, &m, &st, &ed);
fill(G[0], G[0] + maxn * maxn, INF);
fill(cost[0], cost[0] + maxn * maxn, INF);
int u, v;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &u, &v);
scanf("%d%d", &G[u][v], &cost[u][v]);
G[v][u] = G[u][v];
cost[v][u] = cost[u][v];
}
Dijkstra(st);
DFS(ed);
printf("%d %d\n", d[ed], c[ed]);
return 0;
}