1030 Travel Plan
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
总结:有了之前写最短路的经验,这次很快就有思路了,时间主要就是花在了调试上,因为我把它当成了有向图,只拿了18分,改成有向图后,成功AC了,还是挺满意的
思路:用二维数组g表示两个城市之间的距离,q表示两个城市之间高铁的费用(注意是无向图,因为起点和终点一开始是未知的),数组 p 记录到达 i 点的前一个点,dijkstra算法中算出最短路和到达当前点(最短路的前提下)要花的费用,因为数组 p 存的是到达当前点的前一个点,所以需要将路劲翻转一下
#include <iostream>
#include <cstring>
using namespace std;
const int N=510;
int g[N][N],q[N][N];//分别表示两城市的距离和高铁的价格
int p[N],dist[N],c[N];//表示达到当前城市的前一个城市和到达当前城市的距离
bool st[N];
int n,m,s,d;
int dijkstra(int s){
memset(dist,0x3f,sizeof dist);
dist[s]=0;
for(int i=0;i<n-1;i++){
int t=-1;
for(int j=0;j<n;j++){
if(!st[j] && (dist[t]>dist[j] || t==-1))
t=j;
}
for(int j=0;j<n;j++){
if(dist[j]>dist[t]+g[t][j]){
dist[j]=dist[t]+g[t][j];
p[j]=t;
c[j]=c[t]+q[t][j];
}
else if(dist[j]==dist[t]+g[t][j]){
if(c[j]>c[t]+q[t][j]){
c[j]=c[t]+q[t][j];
p[j]=t;
}
}
}
st[t]=true;
}
}
int main(){
cin >> n >> m >> s >> d;
memset(g,0x3f,sizeof g);
for(int i=0;i<m;i++){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
g[a][b]=g[b][a]=c;
q[a][b]=q[b][a]=d;
}
dijkstra(s);
int w[N],index=0;
for(int i=d;;i=p[i]){
w[index++]=i;
if(i==s) break;
}
for(int i=--index;i>=0;i--) printf("%d ",w[i]);
printf("%d %d",dist[d],c[d]);
return 0;
}
大佬的代码:
这种思路可以好好的学习一下,之前那个共享单车那题也是用了这种方法,先求出最短路径,用pre vector的数组保存当前点的前一个点,然后dfs终点到起始点,算出每一条路劲从起点到终点的花费,从中取出最小花费!
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n, m, s, d;
int e[510][510], dis[510], cost[510][510];
vector<int> pre[510];
bool visit[510];
const int inf = 99999999;
vector<int> path, temppath;
int mincost = inf;
void dfs(int v) {
temppath.push_back(v);
if(v == s) {
int tempcost = 0;
for(int i = temppath.size() - 1; i > 0; i--) {
int id = temppath[i], nextid = temppath[i-1];
tempcost += cost[id][nextid];
}
if(tempcost < mincost) {
mincost = tempcost;
path = temppath;
}
temppath.pop_back();
return ;
}
for(int i = 0; i < pre[v].size(); i++)
dfs(pre[v][i]);
temppath.pop_back();
}
int main() {
fill(e[0], e[0] + 510 * 510, inf);
fill(dis, dis + 510, inf);
scanf("%d%d%d%d", &n, &m, &s, &d);
for(int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
scanf("%d", &e[a][b]);
e[b][a] = e[a][b];
scanf("%d", &cost[a][b]);
cost[b][a] = cost[a][b];
}
pre[s].push_back(s);
dis[s] = 0;
for(int i = 0; i < n; i++) {
int u = -1, minn = inf;
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;
for(int v = 0; v < n; v++) {
if(visit[v] == false && e[u][v] != inf) {
if(dis[v] > dis[u] + e[u][v]) {
dis[v] = dis[u] + e[u][v];
pre[v].clear();
pre[v].push_back(u);
} else if(dis[v] == dis[u] + e[u][v]) {
pre[v].push_back(u);
}
}
}
}
dfs(d);
for(int i = path.size() - 1; i >= 0; i--)
printf("%d ", path[i]);
printf("%d %d", dis[d], mincost);
return 0;
}
好好学习,天天向上!
我要考研!