-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path7-9_旅游规划 (25).cpp
89 lines (81 loc) · 2.09 KB
/
7-9_旅游规划 (25).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
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
using namespace std;
const int maxn = 505;
int noteMoney[maxn], dist[maxn], path[maxn];
struct Edge {
int to;
int money;
int len;
bool operator < (const Edge &a) const {
if (a.len == len)
return money > a.money;
return len > a.len;
}
} temp, cur;
vector<Edge> AdjList[maxn];
/*BFS+Priority_Queue*/
void BFS(int s,int d) {
priority_queue<Edge> pq;
temp.len = 0;
temp.money = 0;
temp.to = s;
noteMoney[temp.to] = 0;
dist[temp.to] = 0;
pq.push(temp);
path[s] = -1;
while(!pq.empty()) {
temp = pq.top();
if(temp.to == d) {
stack<int> ans;
int pp = temp.to;
while(pp != -1) {
ans.push(pp);
pp = path[pp];
}
while(!ans.empty()) ans.pop();
printf("%d %d\n", temp.len, temp.money);
return;
}
pq.pop();
for (int i = 0; i < AdjList[temp.to].size(); i++) {
cur = AdjList[temp.to][i];
cur.money += temp.money;
cur.len += temp.len;
if(cur.len < dist[cur.to]) {
dist[cur.to] = cur.len;
noteMoney[cur.to] = cur.money;
path[cur.to] = temp.to;
pq.push(cur);
} else if(cur.len == dist[cur.to] && noteMoney[cur.to] > cur.money) {
noteMoney[cur.to] = cur.money;
path[cur.to] = temp.to;
pq.push(cur);
}
}
}
}
int main() {
int n, fee, r, i, s, d, l, t, start, end;
scanf("%d %d %d %d", &n, &r, &start, &end);
for (i = 1; i <= r; i++) {
scanf("%d %d %d %d", &s, &d, &l, &t);
temp.to = d;
temp.len = l;
temp.money = t;
AdjList[s].push_back(temp);
temp.to = s;
temp.len = l;
temp.money = t;
AdjList[d].push_back(temp);
}
for (i = 0; i < n; i++) {
noteMoney[i] = 10000000;
dist[i] = 10000000;
}
BFS(start,end);
return 0;
}