QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#352736 | #7991. 最小环 | zzy0922 | WA | 428ms | 161868kb | C++14 | 2.4kb | 2024-03-13 16:19:14 | 2024-03-13 16:19:15 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 600005;
int n, m, tot;
int u[N << 1], v[N << 1];
i64 w[N << 1];
bool del[N << 1];
std::unordered_set<int> eS[N];
inline void add_e(int uu, int vv, i64 ww) {
++tot;
u[tot] = uu;
v[tot] = vv;
w[tot] = ww;
eS[uu].insert(tot);
eS[vv].insert(tot);
}
std::vector<std::pair<int, i64> > g[N];
std::unordered_map<int, i64> dis[N];
std::unordered_set<int> vis;
inline void dij(int s) {
dis[s][s] = 0;
vis.clear();
std::priority_queue <std::pair<i64, int>, std::vector<std::pair<i64, int> >, std::greater<std::pair<i64, int> > > pq;
pq.emplace(0, s);
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (vis.count(u)) continue;
vis.insert(u);
for (const auto &[v, w] : g[u]) {
if (!dis[s].count(v) || dis[s][v] > dis[s][u] + w) {
dis[s][v] = dis[s][u] + w;
pq.emplace(dis[s][v], v);
}
}
}
}
i64 ans = 0x7fffffffffffffffll;
int main() {
std::cin >> n >> m;
for (int i = 1, u, v, w; i <= m; i++) {
std::cin >> u >> v >> w;
add_e(u, v, w);
if (u == v) ans = std::min(ans, 1ll * w);
}
auto del_e = [&](int id) {
eS[u[id]].erase(id);
eS[v[id]].erase(id);
del[id] = 1;
};
std::queue<int> q;
auto chk = [&](int u) {
if (!eS[u].empty() && eS[u].size() <= 2) q.push(u);
};
for (int i = 1; i <= n; i++) chk(i);
while (!q.empty()) {
int x = q.front();
q.pop();
if (eS[x].empty()) continue;
if (eS[x].size() == 1) {
int e = *eS[x].begin();
del_e(e);
chk(u[e]);
chk(v[e]);
}
else {
int e1 = *eS[x].begin(), e2 = *std::next(eS[x].begin());
del_e(e1);
del_e(e2);
if (v[e2] == u[e1]) std::swap(e1, e2);
if (v[e1] == u[e2]) {
add_e(u[e1], v[e2], w[e1] + w[e2]);
chk(u[e1]);
chk(v[e2]);
} else if (u[e1] == u[e2]){
chk(v[e1]);
chk(v[e2]);
} else {
chk(u[e1]);
chk(u[e2]);
}
}
}
// if (n >= 20000) return 0;
for (int i = 1; i <= tot; i++)
if (!del[i]) g[u[i]].emplace_back(v[i], w[i]);// std::cout << "ADD: " << u[i] << ' ' << v[i] << ' ' << w[i] << '\n';
for (int i = 1; i <= n; i++) dij(i);
for (int u = 1; u <= n; u++)
for (const auto &[v, w] : g[u]) {
if (v != u && dis[v].count(u)) ans = std::min(ans, dis[v][u] + w);
// std::cout << u << ' ' << v << ' ' << w << '\n';
}
std::cout << (ans == 0x7fffffffffffffffll ? -1 : ans) << '\n';
}
详细
Test #1:
score: 100
Accepted
time: 11ms
memory: 89516kb
input:
4 6 1 2 1 4 3 3 4 1 9 2 4 1 3 1 2 3 2 6
output:
7
result:
ok single line: '7'
Test #2:
score: 0
Accepted
time: 17ms
memory: 83556kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 19ms
memory: 90088kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 428ms
memory: 160216kb
input:
258420 258419 33061 33062 767169384 212916 212917 1741339 229881 229882 896760805 173467 173468 273055172 233189 233190 800433307 10157 10158 126766550 174605 174606 552176083 224030 224031 886617880 229102 229103 783848581 67588 67589 510826095 233648 233649 879695751 214453 214454 867104578 153140...
output:
-1
result:
ok single line: '-1'
Test #5:
score: -100
Wrong Answer
time: 401ms
memory: 161868kb
input:
248016 248896 82688 82755 592665252 202847 203260 294408121 26821 28237 269132335 150967 152178 3125829 246069 247390 29492546 7470 7673 55843492 33975 35414 442802995 28451 28948 193736988 34484 34637 84441058 60168 60309 501991354 79579 79844 26854803 239672 239706 111702667 73548 73658 149840530 ...
output:
101736764788
result:
wrong answer 1st lines differ - expected: '98674714245', found: '101736764788'