关于链前存图与vector存图的表现
有一天在做CF1131D的时候 看见题解区有人说“将链前存图换成vector即可差分约束卡过”
于是手造了个数据看下 怕卡评测数据范围开的很小也很弱 所以效果不是很理想 但是还是能看出来一些东西的
稀疏图的数据生成器如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 1050;
int mp[N][N];
int fa[N];
int main() {
srand(time(0) ^ random_device()());
int n = 800;
int m = n - 1 + 2 * n;
cout << n << " " << m << " " << "\n";
for (int i = 2; i <= n; ++i) {
fa[i] = rand() % n + 1;
while (fa[i] >= i) fa[i] = rand() % n + 1;
}
for (int i = 2; i <= n; ++i) {
mp[i][fa[i]] = mp[fa[i]][i] = rand() % 10000 + 1;
cout << i << " " << fa[i] << " " << mp[i][fa[i]] << "\n";
}
for (int i = 1; i <= 2 * n; ++i) {
int x = rand() % n + 1, y = rand() % n + 1;
while (mp[x][y]) x = rand() % n + 1, y = rand() % n + 1;
mp[x][y] = mp[y][x] = rand() % 10000 + 1;
cout << x << " " << y << " " << mp[x][y] << "\n";
}
return 0;
}
稠密图的数据生成器如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 1050;
int mp[N][N];
int fa[N];
int main() {
srand(time(0) ^ random_device()());
int n = 500;
int m = (n - 1) * n / 2;
cout << n << " " << m << " " << "\n";
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) cout << i << " " << j << " " << rand() % 10000 + 1 << "\n";
}
return 0;
}
共造了 30 组数据 以10组为单位分别对应 n = 200 / 500 / 800
结尾为1~5的对应稀疏图 6~0的对应稠密图
测试结果如下(均开启O2优化):
- Dijkstra + 链前

- Dijkstra + vector

- SPFA + 链前

- SPFA + vector

可以发现 确实在稀疏图中 链前的表现更好 而在稠密图中 vector 的表现更好

浙公网安备 33010602011771号