A1030 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.

1
2
3
4
5
6
7
8
9
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<stack>
#include<cstring>
#define INF 0x3f3f3f
#define MAX 510
using namespace std;
int d[MAX],c[MAX],p[MAX];
int cost[MAX][MAX],G[MAX][MAX];
int n,m,st,ed;
bool vis[MAX]={false};
void dij(int st){
fill(d,d+MAX,INF);//!
d[st]=0;
c[st]=0;
for(int i=0;i<n;i++){
int min=INF,u=-1;
for(int j=0;j<n;j++){
if(d[j]<min&&!vis[j]){
min=d[j];
u=j;
}
}
if(u==-1) return;
vis[u]=true;//!
for(int v=0;v<n;v++){
if(G[u][v]!=INF&&!vis[v]){
if(d[v]>d[u]+G[u][v]){
d[v]=d[u]+G[u][v];
c[v]=c[u]+cost[u][v];
p[v]=u;
}
else if(d[v]==d[u]+G[u][v]){
if(c[u]+cost[u][v]<c[v]){
c[v]=c[u]+cost[u][v];
p[v]=u;
}

}

}
}
}

}

void DFS(int v){
if(v==st){
cout<<v<<" ";return;
}
DFS(p[v]);
cout<<v<<" ";
}
int main(){
// freopen("3-19.txt","r",stdin);
// memset(p,-1,sizeof(p));
fill(G[0],G[0]+MAX*MAX,INF);
// memset(vis,0,sizeof(vis));
cin>>n>>m>>st>>ed;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
cin>>G[u][v];//!! cin>>u>>v>>G[u][v] 错误 u,v值还没存储
cin>>cost[u][v];
G[v][u]=G[u][v];//! v,u注意
cost[v][u]=cost[u][v];
}

dij(st);
DFS(ed);
cout<<d[ed]<<" "<<c[ed]<<endl;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
#define INF 0x3f3f3f
#define MAX 510
using namespace std;
int cost[MAX][MAX],G[MAX][MAX];
int dis[MAX];
int n,m,st,ed;
bool vis[MAX];
int mini=INF;
vector<int> tmp,path,pre[MAX];
void dij(int st){
fill(dis,dis+MAX,INF);

dis[st]=0;
for(int i=0;i<n;i++){
int min=INF,k=-1;
for(int j=0;j<n;j++){
if(dis[j]<min&&vis[j]==false){
min=dis[j];
k=j;
}



}
if(k==-1) return;//!
vis[k]=true;
for(int t=0;t<n;t++){
if(G[k][t]!=INF&&vis[t]==false){
if(G[k][t]+dis[k]<dis[t]){

dis[t]=G[k][t]+dis[k];
pre[t].clear();
pre[t].push_back(k);
}
else if(G[k][t]+dis[k]==dis[t]){
pre[t].push_back(k);
}
}
}


}
}
void DFS(int v){
if(v==st){
tmp.push_back(v);
int tmpcost=0;
for(int j=tmp.size()-1;j>0;j--)
{
int u=tmp[j],v=tmp[j-1];
tmpcost+=cost[u][v];
}
if(tmpcost<mini){
mini=tmpcost;
path=tmp;
}
tmp.pop_back();
return;
}


tmp.push_back(v);
for(int i=0;i<pre[v].size();i++)
{
DFS(pre[v][i]);
}
tmp.pop_back();
}
int main(int argc, const char * argv[]) {
scanf("%d %d %d %d", &n, &m, &st, &ed);
fill(G[0], G[0] + MAX*MAX, INF);
fill(cost[0],cost[0] + MAX*MAX, INF);//!
for(int i = 0; i < m; i++) {
int a, b, len, c;
scanf("%d %d %d %d", &a, &b, &len, &co);
G[a][b] = G[b][a] = len;
cost[a][b] = cost[b][a] = co;
}
dij(st);
DFS(ed);
for(int i = path.size() - 1; i >= 0; i--) {
printf("%d ", path[i]);
}
printf("%d %d\n", dis[ed], mini);
return 0;
}

参考链接
https://blog.csdn.net/feng_zhiyu/article/details/81275054