QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#737813 | #7991. 最小环 | MiniLong | WA | 637ms | 210744kb | C++14 | 5.4kb | 2024-11-12 16:55:08 | 2024-11-12 16:55:09 |
Judging History
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<ll, ll> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 4e5 + 5, M = 3005;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n, m;
vector<PII> G[N], g[N];
struct edge{
int u, v, w;
}e[N];
int cnt, id[N], rev[N];
int dep[N], w[N];
ll dis[N];
bool flag[N];
void dfs(int u, int fa, int lst = 0){
dep[u] = dep[fa] + 1;
for(auto &[v, i] : G[u]){
if(dep[v]){
if(i != lst) flag[i] = 1;
continue;
}
g[u].pb({v, i});
debug("%d -> %d i:%d\n", u, v, i);
w[v] = e[i].u == u, dis[v] = dis[u] + e[i].w;
dfs(v, u, i);
}
}
int pre[N][25];
PII f[N][25];
PII operator+(PII x, PII y){return make_pair(x.fi + y.fi, (x.se == -1 || y.se == -1 || x.se != y.se) ? -1 : x.se);}
void dfs2(int u){
_rep(i, 1, 20) f[u][i] = f[u][i - 1] + f[pre[u][i - 1]][i - 1], pre[u][i] = pre[pre[u][i - 1]][i - 1];
for(auto &[v, i] : g[u]){
f[v][0] = {e[i].w, w[v]}, pre[v][0] = u;
dfs2(v);
}
}
int lca(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
int h = dep[x] - dep[y], p = 0;
// debug("lca(%d,%d) H:%d\n", x, y, h);
while(h){if(h & 1) x = pre[x][p]; h >>= 1, p++;}
if(x == y) return x;
_req(i, 20, 0) if(pre[x][i] != pre[y][i]) x = pre[x][i], y = pre[y][i];
return pre[x][0];
}
PII jump(int u, int h){
PII cur = {0, 0};
_req(i, 20, 0){
if(h >> i & 1){
if(!cur.fi) cur = f[u][i];
else cur = cur + f[u][i];
u = pre[u][i];
}
}
return cur;
}
ll d[M], nxt[M][M];
bool vis[N];
ll dij(int s, ll *dis){
_rep(i, 1, cnt) dis[i] = inf, vis[i] = 0;
priority_queue<PII, vector<PII>, greater<PII> > q;
q.push({dis[s] = 0, s});
while(q.size()){
int u = q.top().se; q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto &[v, w] : G[u]){
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
q.push({dis[v], v});
}
}
}
ll res = inf;
_rep(i, 1, cnt) if(nxt[i][s] != inf) res = min(res, dis[i] + nxt[i][s]);
return res;
}
int main(){
read(n, m);
_rep(i, 1, m){
int u, v, w; read(u, v, w);
e[i] = {u, v, w};
G[u].pb({v, i}), G[v].pb({u, i});
}
dfs(1, 0);
_rep(i, 1, n) G[i].clear();
_rep(i, 1, m){
if(!flag[i]) continue;
if(!id[e[i].u]) rev[id[e[i].u] = ++cnt] = e[i].u;
if(!id[e[i].v]) rev[id[e[i].v] = ++cnt] = e[i].v;
G[id[e[i].u]].pb({id[e[i].v], e[i].w});
}
dfs2(1);
_rep(i, 1, cnt){
_rep(j, 1, cnt){
if(i == j) continue;
int x = rev[i], y = rev[j], z = lca(x, y);
// debug("x:%d y:%d z:%d\n", x, y, z);
PII L = jump(x, dep[x] - dep[z]), R = jump(y, dep[x] - dep[z]);
if(L.se == -1 || R.se == -1 || L.se == R.se) continue;
if(L.se) G[j].pb({i, L.fi + R.fi});
else G[i].pb({j, L.fi + R.fi});
}
}
_rep(i, 1, cnt) _rep(j, 1, cnt) nxt[i][j] = inf;
_rep(i, 1, cnt) for(auto &[j, w] : G[i]) nxt[i][j] = min(nxt[i][j], w);
ll ans = inf;
_rep(i, 1, cnt){
ans = min(ans, dij(i, d));
}
writeln(ans == inf ? -1 : ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 28460kb
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: 24004kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 30620kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 111ms
memory: 210744kb
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: 637ms
memory: 202928kb
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:
6603520058
result:
wrong answer 1st lines differ - expected: '98674714245', found: '6603520058'