题目
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
代码和思路
思路一
- 因为只涉及两个关键字,满足最优子结构
- DIjskra算法中加一个边权就好
代码1
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxv = 510;
const int INF = 100000;
int n, m, s, d;
int pre[510];
int cost[510][510];
int c[510];
int dis[maxv];
int G[maxv][maxv];
bool visit[maxv] = {false};
void Dijistra(int s) {
fill(c, c + 510, INF);
fill(dis, dis + 510, INF);
dis[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 (visit[j] == false && dis[j] < min) {
u = j;
min = dis[j];
}
}
if (u == -1) break;
visit[u] = true;
for (int v = 0; v < n; v++) {
if (visit[v] == false && G[u][v] != INF) {
if (dis[v] > dis[u] + G[u][v]) {
dis[v] = dis[u] + G[u][v];
c[v] = cost[u][v] + c[u];
pre[v] = u;
}
else if (dis[v] == dis[u] + G[u][v]) {
if(c[v] > cost[u][v] + c[u]){
pre[v] = u;
c[v] = cost[u][v] + c[u];
}
}
}
}
}
}
int main() {
int cos, k, city1, city2;
scanf("%d %d %d %d", &n, &m, &s, &d);
fill(G[0], G[0] + maxv * maxv, INF);
for (int i = 0; i < m; i++) {
scanf("%d %d %d %d", &city1, &city2, &k, &cos);
G[city1][city2] = k;
G[city2][city1] = k;
cost[city1][city2] = cos;
cost[city2][city1] = cos;
}
vector<int> v;
Dijistra(s);
int destination = d;
while (destination != pre[destination]) {
v.push_back(destination);
destination = pre[destination];
}
v.push_back(destination);
for (int i = v.size() - 1; i >= 0; i--) {
printf("%d ", v[i]);
}
printf("%d ", dis[d]);
printf("%d", c[d]);
}
注意
- 初始化的地方有很多,邻接矩阵先初始化为均不可达,初始总花费和边花费都初始化为INF。
- 千万记得初始化pre数组,后续回溯的时候否则没有出口了 pre[i] = i
- 初始节点是s,别自己默认为题目的节点输入进去了
思路二
- 使用Dijskra和DFS,先把每条最短路径记录下来,然后再去找最短的那个。
- DFS入口是目的地,从目的地倒着回溯整个路径
- 回溯到出发地的时候,整个路径完整,计算路径花费,然后比较
- 因为回溯的过程是倒着的,输出路径的时候要把路径ans反过来输出
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxv = 510;
const int INF = 100000;
int n, m, s, d;
vector<int> pre[510];
int cost[510][510];
int dis[maxv];
int G[maxv][maxv];
bool visit[maxv] = { false };
vector<int> ans;
vector<int> tempPath;
int minCost = INF;
void DFS(int v) {
if (v == s) {
tempPath.push_back(v);
int tempCost = 0;
for (int i = tempPath.size() - 1; i > 0; i--) {
int idx = tempPath[i], idxnext = tempPath[i - 1];
tempCost += cost[idx][idxnext];
}
if (tempCost < minCost) {
ans = tempPath;
minCost = tempCost;
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for (int i = 0; i < pre[v].size(); i++) {
DFS(pre[v][i]);
}
tempPath.pop_back();
}
void Dijstra(int s) {
fill(dis, dis + maxv, INF);
dis[s] = 0;
for (int i = 0; i < n; i++) {
int min = INF, u = -1;
for (int j = 0; j < n; j++) {
if (dis[j] < min && visit[j] == false) {
u = j;
min = dis[j];
}
}
if (u == -1) break;
visit[u] = true;
for (int v = 0; v < n; v++) {
if (visit[v] == false && G[u][v] != INF) {
if (dis[v] > dis[u] + G[u][v]) {
dis[v] = dis[u] + G[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if (dis[v] = dis[u] + G[u][v]) {
pre[v].push_back(u);
}
}
}
}
}
int main() {
int cos, k, city1, city2;
scanf_s("%d %d %d %d", &n, &m, &s, &d);
fill(G[0], G[0] + maxv * maxv, INF);
for (int i = 0; i < m; i++) {
scanf_s("%d %d %d %d", &city1, &city2, &k, &cos);
G[city1][city2] = k;
G[city2][city1] = k;
cost[city1][city2] = cos;
cost[city2][city1] = cos;
}
Dijstra(s);
DFS(d);
for (int i = ans.size() - 1; i >= 0; i--) {
printf("%d ", ans[i]);
}
printf("%d %d\n", dis[d], minCost);
return 0;
}
注意
这个题目是有最优子结构的,写Dijskra+DFS主要是为了熟练这个过程,毕竟考试的时候应该是没有最优子结构的