用户输出
26
系统信息
Exited with return code 0
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#89099 | #1388. 莉可莉丝 | Accepted | 100 | 1162 ms | 62900 K | C++ 17 (Clang) / 4.0 K | 新能源71 徐晨辰 | 2023-05-30 21:45:46 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
const int maxe = 10010;
int n, m, k = 2;
vector<vector<pair<int, int>>> G;
vector<vector<int>> dis[2], pre[2];
vector<bool> vis;
const int INF = 0x3f3f3f3f;
void InitializeSingleSource(int s) {
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < k; ++j) {
dis[j][s][i] = INF;
pre[j][s][i] = 0;
}
}
dis[0][s][s] = 0;
}
// DAG单源最短/次短路径
void DAG_TOP2_ShortestPath(int s, const vector<int>& topoList) {
InitializeSingleSource(s); // 初始化dis和pre
// 按照拓扑序进行松弛
for (int u : topoList) {
for (auto [v, w] : G[u]) {
if (dis[0][s][v] > dis[0][s][u] + w) { // 松弛
// 更新次短路
dis[1][s][v] = dis[0][s][v];
pre[1][s][v] = pre[0][s][v];
// 更新最短路
dis[0][s][v] = dis[0][s][u] + w;
pre[0][s][v] = u;
} else if (dis[1][s][v] > dis[0][s][u] + w) { // 次短路
dis[1][s][v] = dis[0][s][u] + w;
pre[1][s][v] = u;
// dis[0][s][u] + w <= dis[1][s][u] + w
}
}
}
}
/*
// DAG单源TopK短路
void DAG_TOPK_ShortestPath(int s, const vector<int>& topoList){
InitializeSingleSource(s); // 初始化dis和prev
// 按照拓扑序进行松弛
for (int u: topoList){
for (auto [v, w]: G[u]){
int key = dis[0][s][u] + w;
int i;
// 将key插入至dis[0][s][v], ..., dis[k-1][s][v]的递增序列中
for (i=k-1; i>=0 && dis[i][s][v]>key; --i){
if(i<k-1){ // 后移覆盖
dis[i+1][s][v] = dis[i][s][v];
pre[i+1][s][v] = pre[i][s][v];
}
}
if(i<k-1){ // 插入key
dis[i+1][s][v] = key;
pre[i+1][s][v] = u;
}
}
}
}*/
// 获得从u出发所有可达顶点构成的诱导子图G'的顶点逆拓扑序列
void dfs(int u, vector<int>& path) {
for (auto [v, _] : G[u]) { // 访问u的相邻顶点v
if (vis[v])
continue;
dfs(v, path);
vis[v] = true;
}
path.push_back(u); // 顶点完成访问时加入序列
}
void DAG_APSP() {
dis[0] = dis[1] = vector<vector<int>>(n + 1, vector<int>(n + 1, INF));
pre[0] = pre[1] = vector<vector<int>>(n + 1, vector<int>(n + 1));
vis = vector<bool>(n + 1, false);
vector<int> topo_order_list; // 拓扑序列
dfs(1, topo_order_list); // 求得1和可达顶点的诱导子图拓扑序
// for (auto x: topo_order_list) cout << x << "->";
// cout << endl;
reverse(topo_order_list.begin(),
topo_order_list.end()); // 序列反转
for (int s = 1; s <= n; ++s) {
DAG_TOP2_ShortestPath(s, topo_order_list);
}
}
// [s, ..., v]
vector<int> getPath(int k, int s, int v) {
vector<int> ret;
int cur = pre[k][s][v];
ret.emplace_back(v);
while (cur) {
ret.emplace_back(cur);
cur = pre[k][s][cur];
}
reverse(ret.begin(), ret.end());
return ret;
}
void printUnionPath(int x, int y) {
auto path = getPath(0, 1, x);
for (auto u : path) printf("%d->", u);
printf("\n==========================\n");
path = getPath(0, x, y);
for (auto u : path) printf("%d->", u);
printf("\n==========================\n");
path = getPath(1, x, y);
for (auto u : path) printf("%d->", u);
printf("\n==========================\n");
path = getPath(0, y, n);
for (auto u : path) printf("%d->", u);
printf("\n");
}
int main() {
// INPUTS
scanf("%d%d", &n, &m);
G = vector<vector<pair<int, int>>>(n + 1);
for (int i = 1; i <= m; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
G[u].push_back({ v, w });
}
// 预处理全源Top2短路
DAG_APSP();
long long res = INF;
int bestX, bestY;
// 枚举x,y in [1, n] and x!=y
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= n; ++y) {
if (x != y) {
long long cur = 1LL * dis[0][1][x] + dis[0][y][n] + dis[0][x][y] + dis[1][x][y];
if (cur < res) {
res = cur;
bestX = x, bestY = y;
}
}
}
}
// printUnionPath(bestX, bestY); // 打印最优路径并
printf("%lld\n", res);
// auto path = getPath(1, 1, 4);
// for (auto u: path) printf("%d->", u);
return 0;
}
用户输出
8
系统信息
Exited with return code 0
用户输出
8
系统信息
Exited with return code 0
50 100
1 2 331
2 3 423
3 4 831
3 5 196
2 6 467
4 7 841
5 8 589
6 9 930
9 10 481
3 11 293
7 12 968
5
<852 bytes omitted>
用户输出
4204
系统信息
Exited with return code 0
50 100
1 2 37
2 3 90
3 4 270
1 5 616
4 6 462
6 7 801
5 8 922
8 9 701
5 10 204
7 11 330
1 12 588
6 13
<840 bytes omitted>
用户输出
1685
系统信息
Exited with return code 0
942 9498
1 2 87293
1 3 92214
3 4 20834
3 5 924
4 6 71171
6 7 66669
6 8 57264
6 9 98803
5 10 52661
7
<129920 bytes omitted>
用户输出
214920
系统信息
Exited with return code 0
954 9620
1 2 48928
1 3 77602
2 4 9736
2 5 82149
1 6 14100
2 7 73201
7 8 31260
7 9 82801
2 10 69933
6
<131587 bytes omitted>
用户输出
184919
系统信息
Exited with return code 0
946 9136
1 2 3322
1 3 76072
1 4 16527
2 5 9562
5 6 60762
6 7 67224
1 8 88909
7 9 91358
9 10 25363
4
<124859 bytes omitted>
用户输出
191259
系统信息
Exited with return code 0
963 9814
1 2 14048
2 3 40980
2 4 76354
1 5 32242
5 6 57710
4 7 74690
3 8 696
2 9 93547
5 10 83686
3
<134310 bytes omitted>
用户输出
191404
系统信息
Exited with return code 0
907 9592
1 2 56498
2 3 39992
1 4 97777
3 5 43535
1 6 60669
3 7 8684
6 8 8342
5 9 3341
5 10 54069
3 1
<131170 bytes omitted>
用户输出
159164
系统信息
Exited with return code 0
961 9738
1 2 77603
1 3 55107
2 4 94838
1 5 86873
2 6 71895
4 7 42417
7 8 18633
3 9 50720
2 10 8124
2
<133328 bytes omitted>
用户输出
226759
系统信息
Exited with return code 0
922 9495
1 2 74386
1 3 46842
1 4 45986
3 5 28221
4 6 813
6 7 9673
5 8 84925
5 9 27070
1 10 28158
6 1
<129894 bytes omitted>
用户输出
206120
系统信息
Exited with return code 0
969 9940
1 2 52898
2 3 66949
1 4 33891
1 5 89883
3 6 58933
1 7 43115
6 8 705
1 9 11964
2 10 27082
7
<136015 bytes omitted>
用户输出
180825
系统信息
Exited with return code 0
958 9208
1 2 6355
1 3 33442
3 4 5708
1 5 82239
5 6 54649
4 7 90055
1 8 96152
8 9 66805
3 10 2693
9 1
<125934 bytes omitted>
用户输出
201075
系统信息
Exited with return code 0
1978 9706
1 2 78441
1 3 93204
1 4 34169
4 5 68390
4 6 68610
2 7 4971
2 8 48605
8 9 41970
4 10 67572
<143038 bytes omitted>
用户输出
235715
系统信息
Exited with return code 0
1988 9915
1 2 18876
2 3 50319
2 4 23783
3 5 12278
5 6 52624
2 7 94109
6 8 45323
7 9 85178
3 10 40196
<146223 bytes omitted>
用户输出
180635
系统信息
Exited with return code 0
1992 9832
1 2 8550
2 3 74022
2 4 32107
2 5 35012
1 6 63629
4 7 68299
4 8 32176
2 9 52376
7 10 37712
<145210 bytes omitted>
用户输出
400566
系统信息
Exited with return code 0
1904 9599
1 2 38275
1 3 8569
2 4 22406
4 5 52940
5 6 90205
4 7 65298
7 8 98943
5 9 69325
6 10 47027
<141327 bytes omitted>
用户输出
553323
系统信息
Exited with return code 0
1966 9554
1 2 17891
2 3 8060
3 4 6259
1 5 33026
5 6 71923
6 7 60286
6 8 54211
4 9 29160
9 10 31533
4
<141078 bytes omitted>
用户输出
893510
系统信息
Exited with return code 0