QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#438930 | #7790. 最短路求和 | Made_in_Code | 0 | 26ms | 21152kb | C++14 | 5.5kb | 2024-06-11 12:04:30 | 2024-06-11 12:04:31 |
Judging History
answer
#include <algorithm>
#include <iostream>
#include <queue>
#define LL long long
using namespace std;
const int kMaxN = 1e5 + 1, kMaxM = 2001, kInf = 1e9;
struct E {
int s, w;
LL lz, rz;
vector<int> l;
vector<int> t;
void Rev() {
swap(lz, rz);
reverse(l.begin(), l.end());
reverse(t.begin(), t.end());
}
} e[kMaxN];
struct V {
int d, w, o;
bool b;
vector<pair<int, int>> e;
} v[kMaxN];
int n, m, k, a[kMaxM];
LL ans, d[kMaxM][kMaxM];
void Init() {
int m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
v[i].w = 1;
}
for (int i = 1, x, y, z; i <= m; i++) {
cin >> x >> y >> z, v[x].d++, v[y].d++;
v[x].e.push_back({y, z}), v[y].e.push_back({x, z});
}
}
void Del1() {
queue<int> q;
for (int i = 1; i <= n; i++) {
if (v[i].d == 1) {
q.push(i);
}
}
for (; !q.empty(); q.pop()) {
int x = q.front();
int y = v[x].e.front().first;
int z = v[x].e.front().second;
ans += 1LL * v[x].w * (n - v[x].w) * z;
v[y].w += v[x].w, v[x].w = 0, v[y].d--;
if (v[y].d == 1) {
q.push(y);
}
}
}
void ReBuild() {
vector<pair<int, int>> e;
for (int i = 1; i <= n; i++) {
if (v[i].d == 1) {
v[i].e.clear();
} else {
for (auto j : v[i].e) {
if (v[j.first].d > 1) {
e.push_back(j);
}
}
v[i].e = e, e.clear();
}
}
}
void Dij(int s) {
priority_queue<pair<int, int>> q;
for (int i = 1; i <= n; i++) {
v[i].d = kInf;
}
for (v[s].d = 0, q.push({0, s}); !q.empty();) {
int x = q.top().second;
q.pop();
for (auto i : v[x].e) {
int y = i.first, z = i.second;
if (v[y].d > v[x].d + z) {
v[y].d = v[x].d + z, q.push({-v[y].d, y});
}
}
}
}
void Calc3() {
for (int i = 1; i <= n; i++) {
if (v[i].d > 2) {
a[++k] = i, v[i].o = k;
}
}
for (int i = 1; i <= k; i++) {
Dij(a[i]);
for (int j = 1; j <= k; j++) {
d[i][j] = v[a[j]].d;
}
LL s = 0;
for (int j = 1; j <= n; j++) {
s += v[j].w * v[j].d;
}
ans += v[a[i]].w * s;
}
for (int i = 1; i <= k; i++) {
for (int j = i + 1; j <= k; j++) {
ans -= d[i][j] * v[a[i]].w * v[a[j]].w;
}
}
}
void Find(int j, int p) {
for (; v[p].e.size() == 2;) {
v[j].b = 1, e[m].l.push_back(j);
if (v[p].e.front().first == j) {
e[m].t.push_back(v[p].e.front().second);
} else {
e[m].t.push_back(v[p].e.back().second);
}
if (v[j].e.front().first == p) {
p = j, j = v[j].e.back().first;
} else {
p = j, j = v[j].e.front().first;
}
}
}
void CalcChain(int _i) {
E &i = e[_i];
int x = 1, y = 1;
int disx = i.t.front(), dism = 0, disy = i.s - disx, wm = 0, wy = i.w;
LL zm = 0, zy = i.rz;
LL dis = d[v[i.l.front()].o][v[i.l.back()].o];
for (; x <= i.l.size() - 2 && y <= i.l.size() - 2; x++) {
while (y <= i.l.size() - 2) {
if (dism <= disx + dis + disy) {
zm += 1LL * dism * v[i.l[y]].w;
zy -= 1LL * disy * v[i.l[y]].w;
dism += i.t[y], disy -= i.t[y];
wm += v[i.l[y]].w, wy -= v[i.l[y]].w;
y++;
} else {
break;
}
}
ans += v[i.l[x]].w * (1LL * wy * (disx + dis) + zy + zm);
disx += i.t[x], dism -= i.t[x];
zm -= 1LL * (wm - v[i.l[x]].w) * i.t[x];
}
}
void FindChain() {
for (int i = 1; i <= n; i++) {
if (!v[i].b && v[i].e.size() == 2) {
m++, v[i].b = 1;
Find(v[i].e.front().first, i);
e[m].Rev(), e[m].l.push_back(i);
Find(v[i].e.back().first, i);
for (int j : e[m].t) {
e[m].s += j;
}
for (int j = 1; j <= e[m].l.size() - 2; j++) {
e[m].w += v[e[m].l[j]].w;
}
for (int j = 1, z = 0; j <= e[m].l.size() - 2; j++) {
z += e[m].t[j - 1], e[m].lz += v[e[m].l[j]].w * z;
}
for (int j = e[m].l.size() - 2, z = 0; j >= 1; j--) {
z += e[m].t[j], e[m].rz += v[e[m].l[j]].w * z;
}
}
}
for (int i = 1; i <= m; i++) {
CalcChain(i);
}
}
void Cross(int _i, int _j, int o00, int o01, int o10, int o11) {
E &i = e[_i], &j = e[_j];
int x = 1, y = j.l.size() - 2;
int x0 = v[i.l.front()].o, x1 = v[i.l.back()].o;
int y0 = v[j.l.front()].o, y1 = v[j.l.back()].o;
int xdis0 = i.t.front(), xdis1 = i.s - xdis0;
int ydis1 = j.t.back(), ydis0 = j.s - ydis1, w = j.w;
LL z = j.lz;
for (; x <= i.l.size() - 2 && y >= 1; x++) {
while (y >= 1) {
LL dis00 = d[x0][y0] + xdis0 + ydis0 << 2 | o00;
LL dis01 = d[x0][y1] + xdis0 + ydis1 << 2 | o01;
LL dis10 = d[x1][y0] + xdis1 + ydis0 << 2 | o10;
LL dis11 = d[x1][y1] + xdis1 + ydis1 << 2 | o11;
if (dis00 > min({dis01, dis10, dis11})) {
z -= 1LL * v[j.l[y]].w * ydis0;
w -= v[j.l[y]].w, y--;
ydis0 -= j.t[y], ydis1 += j.t[y];
} else {
break;
}
}
ans += v[i.l[x]].w * (1LL * w * (xdis0 + d[x0][y0]) + z);
xdis0 += i.t[x], xdis1 -= i.t[x];
}
}
void Calc2() {
for (int i = 1; i <= m; i++) {
for (int j = i + 1; j <= m; j++) {
Cross(i, j, 0, 1, 2, 3); // 00
e[j].Rev();
Cross(i, j, 1, 0, 3, 2); // 01
e[i].Rev();
Cross(i, j, 3, 2, 1, 0); // 11
e[j].Rev();
Cross(i, j, 2, 3, 0, 1); // 10
e[i].Rev();
}
}
}
int main() {
cin.tie(0), cout.tie(0);
ios::sync_with_stdio(0);
Init(), Del1(), ReBuild();
Calc3(), FindChain(), Calc2();
cout << ans << '\n';
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 10
Accepted
time: 18ms
memory: 21152kb
input:
300 1300 90 125 9397 157 77 3704 197 112 8218 152 235 1702 271 107 5600 117 92 1401 104 61 2242 127 230 1471 91 116 2740 29 127 4326 151 78 2569 273 241 7487 170 115 3100 152 171 2504 193 95 5921 30 281 1309 285 262 6462 100 265 8151 200 90 277 237 151 1123 231 219 974 238 176 2239 89 147 2256 233 2...
output:
324731073
result:
ok single line: '324731073'
Test #2:
score: -10
Wrong Answer
time: 3ms
memory: 14656kb
input:
300 299 168 161 181 71 254 4119 160 298 8533 148 29 4098 277 279 73 204 174 644 230 113 1265 89 194 6883 296 21 1759 280 190 4793 298 86 3667 185 67 7427 163 257 7845 15 54 8936 52 22 2786 154 199 5543 136 278 4548 256 27 9557 147 34 4208 255 292 1753 242 300 619 263 37 2565 215 109 866 75 153 4924 ...
output:
1347421185838
result:
wrong answer 1st lines differ - expected: '3693554127', found: '1347421185838'
Subtask #2:
score: 0
Wrong Answer
Test #5:
score: 0
Wrong Answer
time: 26ms
memory: 20360kb
input:
100000 99999 54625 54626 7146 20763 20764 300 41530 41531 9968 37448 37449 7434 81056 81055 700 27731 27730 8783 12408 12409 514 90652 90653 99 84104 84105 2524 83093 83094 195 17757 17756 2560 81925 81926 8935 14220 14219 9619 25516 25515 5883 89413 89412 275 46936 46937 3997 82755 82754 2775 53080...
output:
416820187913059770
result:
wrong answer 1st lines differ - expected: '834269687204155387', found: '416820187913059770'
Subtask #3:
score: 0
Runtime Error
Test #7:
score: 0
Runtime Error
input:
100000 100000 35241 48789 5098 4546 39869 6127 31415 22834 6026 25703 1952 6807 86143 78951 3421 34193 9615 4329 31012 98959 1664 81244 37874 3542 600 74315 9939 91066 57088 2111 5064 33313 9799 78834 28718 1133 41687 82171 4214 44801 87500 4238 40150 73606 5172 17787 30281 3718 52715 82529 4419 924...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #2:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%