QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#772715 | #7991. 最小环 | Tobo | WA | 281ms | 64972kb | C++20 | 4.3kb | 2024-11-22 21:22:23 | 2024-11-22 21:22:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
bool Memory_begin;
/*
*/
template <int mod>
unsigned int down(unsigned int x)
{
return x >= mod ? x - mod : x;
}
template <int mod>
struct Modint
{
unsigned int x;
Modint() = default;
Modint(unsigned int x) : x(x) {}
friend istream &operator>>(istream &in, Modint &a) { return in >> a.x; }
friend ostream &operator<<(ostream &out, Modint a) { return out << a.x; }
friend Modint operator+(Modint a, Modint b) { return down<mod>(a.x + b.x); }
friend Modint operator-(Modint a, Modint b) { return down<mod>(a.x - b.x + mod); }
friend Modint operator*(Modint a, Modint b) { return 1ULL * a.x * b.x % mod; }
friend Modint operator/(Modint a, Modint b) { return a * ~b; }
friend Modint operator^(Modint a, int b)
{
Modint ans = 1;
for (; b; b >>= 1, a *= a)
if (b & 1)
ans *= a;
return ans;
}
friend Modint operator~(Modint a) { return a ^ (mod - 2); }
friend Modint operator-(Modint a) { return down<mod>(mod - a.x); }
friend Modint &operator+=(Modint &a, Modint b) { return a = a + b; }
friend Modint &operator-=(Modint &a, Modint b) { return a = a - b; }
friend Modint &operator*=(Modint &a, Modint b) { return a = a * b; }
friend Modint &operator/=(Modint &a, Modint b) { return a = a / b; }
friend Modint &operator^=(Modint &a, int b) { return a = a ^ b; }
friend Modint &operator++(Modint &a) { return a += 1; }
friend Modint operator++(Modint &a, int)
{
Modint x = a;
a += 1;
return x;
}
friend Modint &operator--(Modint &a) { return a -= 1; }
friend Modint operator--(Modint &a, int)
{
Modint x = a;
a -= 1;
return x;
}
friend bool operator==(Modint a, Modint b) { return a.x == b.x; }
friend bool operator!=(Modint a, Modint b) { return !(a == b); }
};
const int mod = 1e9 + 7;
typedef Modint<mod> mint;
bool Memory_end;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cerr << (&Memory_end - &Memory_begin) / 1048576.0 << "MB" << '\n';
int n, m;
cin >> n >> m;
vector<int> deg(n + 1), del(n + 1);
vector<set<pair<int, long long>>> adj(n + 1);
auto rev = adj;
long long ans = 1e18;
for (int i = 1, u, v, w; i <= m; i++)
{
cin >> u >> v >> w;
if (u == v)
{
ans = min(ans, 1ll * w);
continue;
}
adj[u].insert({v, w});
rev[v].insert({u, w});
deg[u]++, deg[v]++;
}
for (int cur = 1; cur <= n; cur++)
{
if (deg[cur] != 2)
continue;
if (adj[cur].size() == 2 or rev[cur].size() == 2)
{
del[cur] = 1;
adj[cur] = rev[cur] = {};
deg[cur] = 0;
continue;
}
long long len = adj[cur].begin()->second + rev[cur].begin()->second;
int to = adj[cur].begin()->first, from = rev[cur].begin()->first;
deg[cur] = 0;
adj[from].erase({cur, rev[cur].begin()->second});
rev[to].erase({cur, adj[cur].begin()->second});
adj[from].insert({to, len});
adj[cur] = rev[cur] = {};
del[cur] = 1;
}
for (int t = 1; t <= n; t++)
{
vector<pair<int, long long>> tmp{adj[t].begin(), adj[t].end()};
for (auto &[s, len] : tmp)
{
adj[t].erase({s, len});
vector<long long> dis(n + 1, 1e18);
vector<int> vis(n + 1);
dis[s] = 0;
priority_queue<pair<long long, int>> que;
que.push({0, s});
while (!que.empty())
{
int cur = que.top().second;
que.pop();
if (vis[cur])
continue;
vis[cur] = 1;
for (auto &[i, w] : adj[cur])
if (dis[i] > dis[cur] + w)
dis[i] = dis[cur] + w, que.push({-dis[i], i});
}
if (dis[t] <= 1e16)
ans = min(ans, dis[t] + len);
adj[t].insert({s, len});
}
}
if (ans == 1e18)
ans = -1;
cout << ans << '\n';
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3992kb
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: 0ms
memory: 3816kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 152ms
memory: 64972kb
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: 281ms
memory: 62804kb
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:
-1
result:
wrong answer 1st lines differ - expected: '98674714245', found: '-1'