-
Notifications
You must be signed in to change notification settings - Fork 0
/
onlineMap.cpp
171 lines (158 loc) · 3.96 KB
/
onlineMap.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include<iostream>
#include<vector>
#include<string.h>
#include<set>
#include<algorithm>
using namespace std;
const int inf = 999999999;
int da[505][505];
int ta[505][505];
int src,des;
vector<int> tmptPath, truetPath;
vector<int> tmpdPath, truedPath;
bool visit[505];
int preDis[505], preTime[505];
int disa[505], timea[505];
int speeda[505], nodea[505];
void dfsd(int u){
truedPath.push_back(u);
if(u==src){
return;
}
dfsd(preDis[u]);
}
void dfst(int u){
truetPath.push_back(u);
if(u==src){
return;
}
dfst(preTime[u]);
}
int main(){
int n,m;
// fill(timea, timea+505, inf);
// fill(disa, disa+505, inf);
memset(timea,0x3f,sizeof(timea));
memset(disa,0x3f,sizeof(disa));
memset(speeda, 0x3f, sizeof(speeda));
memset(nodea, 0, sizeof(nodea));
memset(da,0x3f,sizeof(da));
memset(ta,0x3f,sizeof(ta));
// fill(da[0], da[0]+505*505, inf);
// fill(ta[0], ta[0]+505*505, inf);
scanf("%d %d", &n, &m);
for(int i=0;i<m;i++){
int a,b,c,d,e;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if(c==1){
da[a][b]=d;
ta[a][b]=e;
}
else
{
da[a][b]=da[b][a]=d;
ta[a][b]=ta[b][a]=e;
}
}
cin>>src>>des;
tmpdPath.push_back(src);
for(int i = 0; i < n; i++)
preDis[i] = i;
disa[src]=0;
for(int i=0;i<n;i++){
int tmin=999999999;
int u=-1;
for(int j=0;j<n;j++){
if(visit[j] == false && disa[j]<tmin){
tmin = disa[j];
u=j;
}
}
if(u==-1){
break;
}
visit[u]=true;
for(int j=0;j<n;j++){
if(visit[j] == false && da[u][j]<999999999){
if(disa[j]>disa[u]+da[u][j]){
disa[j]=disa[u]+da[u][j];
preDis[j]=u;
timea[j]=timea[u]+ta[u][j];
}
else if(disa[j] == disa[u]+da[u][j] && timea[u]+ta[u][j] < timea[j] )
{
preDis[j]=u;
timea[j]=timea[u]+ta[u][j];
}
}
}
}
dfsd(des);
fill(visit, visit + 505, false);
speeda[src]=0;
for(int i = 0; i < n; i++)
preTime[i] = i;
for(int i=0;i<n;i++){
int tmin=999999999;
int u=-1;
for(int j=0;j<n;j++){
if(visit[j] == false && speeda[j]<tmin){
tmin = speeda[j];
u=j;
}
}
if(u==-1){
break;
}
visit[u]=true;
for(int j=0;j<n;j++){
if(visit[j] == false && ta[u][j]<999999999){
if(speeda[j]>speeda[u]+ta[u][j]){
speeda[j]=speeda[u]+ta[u][j];
preTime[j]=u;
nodea[j]=nodea[u]+1;
}
else if(speeda[j] == speeda[u]+ta[u][j] && nodea[u]+1 < nodea[j] )
{
preTime[j]=u;
nodea[j]=nodea[u]+1;
}
}
}
}
dfst(des);
if(truetPath == truedPath){
printf("Distance = %d; Time = %d: ",disa[des], speeda[des]);
for(int i=truedPath.size()-1;i >= 0;i--){
if(i==truedPath.size()-1){
cout<<truedPath[i];
}
else
{
cout<<" -> "<<truedPath[i];
}
}
return 0;
}
printf("Distance = %d: ",disa[des]);
for(int i=truedPath.size()-1;i >= 0;i--){
if(i==truedPath.size()-1){
cout<<truedPath[i];
}
else
{
cout<<" -> "<<truedPath[i];
}
}
cout<<endl;
printf("Time = %d: ", speeda[des]);
for(int i=truetPath.size()-1;i >= 0;i--){
if(i==truetPath.size()-1){
cout<<truetPath[i];
}
else
{
cout<<" -> "<<truetPath[i];
}
}
}