-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1111.cpp
129 lines (123 loc) · 2.61 KB
/
A1111.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
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN = 510;
const int INF = 0x3fffffff;
int N, M, S, E;
int preD[MAXN] = {0}, preT[MAXN] = {0};
int d[MAXN], t[MAXN], amt[MAXN];
bool vis[MAXN];
struct Node{
int v, len, time;
Node(int _v, int _len, int _time): v(_v), len(_len), time(_time) {}
};
vector<Node> Adj[MAXN];
void DijkstraD(int root){
fill(d, d+MAXN, INF);
fill(t, t+MAXN, INF);
fill(vis, vis+MAXN, false);
d[root] = 0;
t[root] = 0;
for(int i = 0; i < N; ++i){
int u = -1, MIN = INF;
for(int j = 0; j < N; ++j){
if(vis[j] == false && d[j] < MIN){
MIN = d[j];
u = j;
}
}
if(u == -1) return;
vis[u] = true;
for(int j = 0; j < Adj[u].size(); ++j){
int v = Adj[u][j].v;
int dis = Adj[u][j].len;
int time = Adj[u][j].time;
if(vis[v] == false && d[u] + dis < d[v]){
d[v] = d[u] + dis;
t[v] = t[u] + time;
preD[v] = u;
}else if(vis[v] == false && d[u] + dis == d[v] && t[u] + time < t[v]){
t[v] = t[u] + time;
preD[v] = u;
}
}
}
}
void DijkstraT(int root){
//fill(d, d+MAXN, INF);
fill(t, t+MAXN, INF);
fill(amt, amt+MAXN, INF);
fill(vis, vis+MAXN, false);
//d[root] = 0;
t[root] = 0;
amt[root] = 1;
for(int i = 0; i < N; ++i){
int u = -1, MIN = INF;
for(int j = 0; j < N; ++j){
if(vis[j] == false && t[j] < MIN){
MIN = t[j];
u = j;
}
}
if(u == -1) return;
vis[u] = true;
for(int j = 0; j < Adj[u].size(); ++j){
int v = Adj[u][j].v;
//int dis = Adj[u][j].len;
int time = Adj[u][j].time;
if(vis[v] == false && t[u] + time < t[v]){
//d[v] = d[u] + dis;
t[v] = t[u] + time;
amt[v] = amt[u] + 1;
preT[v] = u;
}else if(vis[v] == false && t[u] + time == t[v] && amt[u] + 1 < amt[v]){
//d[v] = d[u] + dis;
amt[v] = amt[u] + 1;
preT[v] = u;
}
}
}
}
void DFS(int v, int pre[]){
if(v == S){
printf("%d", v);
return;
}
DFS(pre[v], pre);
printf(" -> %d", v);
}
bool isSame(int preD[], int preT[]){
for(int i = 0; i < N; ++i){
if(preD[i] != preT[i]){
return false;
}
}
return true;
}
int main(){
scanf("%d%d", &N, &M);
for(int i = 0; i < M; ++i){
int v1, v2, valid, len, time;
scanf("%d%d%d%d%d", &v1, &v2, &valid, &len, &time);
Adj[v1].push_back(Node(v2, len, time));
if(valid == 0)
Adj[v2].push_back(Node(v1, len, time));
}
scanf("%d%d", &S, &E);
DijkstraD(S);
int dis = d[E];
//DFS(E, preD);
DijkstraT(S);
int time = t[E];
//DFS(E, preT);
if(isSame(preD, preT)){
printf("Distance = %d; Time = %d: ", dis, time);
DFS(E, preD);
}else{
printf("Distance = %d: ", dis);
DFS(E, preD);
printf("\nTime = %d: ", time);
DFS(E, preT);
}
return 0;
}