题目描述
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.
输入
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.
输出
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.
思路
简单的最短路径,N给的比较小,深搜和dijkstra都可以,嫌麻烦深搜,建议学会dijkstra。
下面给出两个的实现。
对于dijkstra算法,我们还需要记录每一个前一个是谁,方便输出路径
代码
深搜
#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
int dis[501][501] = { 0 };
int cost[501][501] = { 0 };
int pan[501] = { 0 };
int N, M, s, e;
int min_dis = 2147483647;
int min_cost = 2147483647;
vector<int> fin;
vector<int> temp;
void dfs(int cur,int d,int c)
{
if (cur == e) {
if (min_dis > d) {
fin.clear();
fin.push_back(s);
for (int i = 0; i < temp.size(); i++)
{
fin.push_back(temp[i]);
}
min_cost = c;
min_dis = d;
return;
}
else if (min_dis == d)
{
if (c < min_cost)
{
fin.clear();
fin.push_back(s);
for (int i = 0; i < temp.size(); i++)
{
fin.push_back(temp[i]);
}
min_cost = c;
min_dis = d;
}
return;
}
return;
}
if (d > min_dis)
{
return;
}
for (int i = 0; i < N; i++)
{
if (pan[i] != 1 && dis[cur][i]!=-1)
{
pan[i] = 1;
temp.push_back(i);
dfs(i, d + dis[cur][i], c + cost[cur][i]);
temp.pop_back();
pan[i] = 0;
}
}
}
int main()
{
cin >> N >> M >> s >> e;
for (int i = 0; i <= 500; i++)
{
for (int j = 0; j <= 500; j++)
{
dis[i][j] = cost[i][j] = -1;
}
}
for (int i = 0; i < M; i++)
{
int a, b, d, c;
cin >> a >> b >> d >> c;
dis[a][b] = dis[b][a] = d;
cost[a][b] = cost[b][a] = c;
}
pan[s] = 1;
fin.push_back(s);
dfs(s,0,0);
for (int i = 0; i < fin.size(); i++)
{
printf("%d ", fin[i]);
}
printf("%d %d\n", min_dis, min_cost);
}
dijkstra
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<vector>
using namespace std;
#define MAXN 0x7fff
int graph[550][550];
int cost[550][550] = { 0 };
int visit[550] = { 0 };
int pre[550];
int dis[550] = { 0 };
int mcost[550] = { 0 };
int N, M, S, D;
void dijkstra(int s)
{
fill(pre, pre + N, -1);
for (int i = 0; i < N; i++)
{
dis[i] = MAXN;
}
for (int i = 0; i < N; i++)
{
dis[i] = graph[s][i];
}
for (int i = 0; i < N; i++)
{
mcost[i] = cost[s][i];
}
dis[s] = 0;
mcost[s] = 0;
while (1)
{
int mmin = MAXN;
int cur = -1;
for (int i = 0; i < N; i++)
{
if (visit[i] == 0 && dis[i] < mmin)
{
mmin = graph[s][i];
cur = i;
}
}
if (cur == -1)
{
break;
}
visit[cur] = 1;
for (int i = 0; i < N; i++)
{
if (visit[i] == 0 && graph[cur][i] != MAXN)
{
if (graph[cur][i] + dis[cur] < dis[i])
{
dis[i] = graph[cur][i]+dis[cur];
mcost[i] = mcost[cur] + cost[cur][i];
pre[i] = cur;
}
else if (dis[i] == graph[cur][i] + dis[cur])
{
if (mcost[cur] + cost[cur][i] < mcost[i])
{
mcost[i] = mcost[cur] + cost[cur][i];
pre[i] = cur;
}
}
}
}
}
}
int main()
{
cin >> N >> M >> S >> D;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
graph[i][j] = MAXN;
cost[i][j] = MAXN;
}
}
for (int i = 0; i < M; i++)
{
int a, b, d, c;
cin >> a >> b >> d >> c;
graph[a][b] = d;
graph[b][a] = d;
cost[a][b] = c;
cost[b][a] = c;
}
visit[S] = 1;
dijkstra(S);
vector<int> v;
for (int i = D; i != -1; i = pre[i])
{
v.push_back(i);
}
cout << S << " ";
for (int i = v.size() - 1; i >= 0; i--)
{
printf("%d ", v[i]);
}
printf("%d %d\n", dis[D], mcost[D]);
}