发布于  更新于 

CF575G Run for beer

图论 题解 OI

9月30日的 Codeforces Div.2 的题解大概就鸽了吧

题意

个点条边的带权图, 求的最小权值且最小长度的路径, 权值为把路径经过的边的权从终点到起点往依次写下组成的十进制数, .

思路

先把到终点距离为的点预处理出来, 然后从多起点开始跑字典序最小最短路(分层BFS).

代码

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
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

typedef long long int64;

const int MAXN = 100010;
const int INF = 0x3f3f3f3f;

#include <cctype>
#include <cstdio>

template <typename T = int>
inline T read() {
T X = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch)) {
X = (X << 3) + (X << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -X : X;
}

template <typename T>
inline void write(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}

class lfs {
public:
lfs(int N) {
memset(head, -1, sizeof head);
ecnt = 0;
n = N;
}
void adde(int from, int to, int w) {
e[ecnt].to = to;
e[ecnt].w = w;
e[ecnt].next = head[from];
head[from] = ecnt++;
}
void addde(int a, int b, int w) {
adde(a, b, w);
adde(b, a, w);
}

void solve() {
vector<int> bfs_order;
memset(dis, INF, sizeof dis);
dis[0] = 0;
bfs_order.push_back(0);
int cur = 0; // bfs队列不用pop, 空间换时间
while (cur < bfs_order.size()) {
int u = bfs_order[cur];
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].to;
if (dis[v] == INF) {
dis[v] = dis[u] + 1;
bfs_order.push_back(v);
}
}
cur++;
}

vector<int> end_point;
end_point.push_back(n - 1);
memset(vis, false, sizeof vis);
memset(pre, 0, sizeof pre);
vis[n - 1] = true;
cur = 0;
int mindis = dis[n - 1];
while (cur < end_point.size()) {
int u = end_point[cur];
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].to;
if (e[i].w == 0 && !vis[v]) {
end_point.push_back(v);
pre[v] = u; // 实际顺序是反的
vis[v] = true;
mindis = min(mindis, dis[v]);
}
}
cur++;
}

bool leading_zeros = true; // 当前bfs是否还在前导零中
for (int l = mindis; l > 0; l--) {
int now = INF;
vector<int> next_point;
for (auto& u : end_point) {
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].to;
if (dis[v] + 1 == l) {
now = min(now, e[i].w);
}
}
}

if (now != 0) leading_zeros = false;
if (l == 1 || !leading_zeros) cout << now;

for (auto& u : end_point) {
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].to;
if (dis[v] + 1 == l && e[i].w == now && !vis[v]) {
vis[v] = true;
next_point.push_back(v);
pre[v] = u;
}
}
}
end_point = next_point;
}
if (leading_zeros) cout << 0; // 都是0的情况
cout << endl;
vector<int> path;
int u = 0;
path.push_back(u);
while (u != n - 1) {
u = pre[u];
path.push_back(u);
}
cout << path.size() << endl;
for (auto& i : path) {
cout << i << ' ';
}
cout << endl;
}


protected:
struct Edge {
int to, next, w;
} e[MAXN * 2];
int head[MAXN];
int ecnt;
int n;

int dis[MAXN];
bool vis[MAXN];
int pre[MAXN];
};

int main() {
int n = read();
int m = read();
auto graph = new lfs(n);
for (int i = 1; i <= m; i++) {
int u = read();
int v = read();
int w = read();
graph->addde(u, v, w);
}
graph->solve();
}