QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#737738 | #7991. 最小环 | MiniLong | WA | 304ms | 99080kb | C++14 | 4.6kb | 2024-11-12 16:45:23 | 2024-11-12 16:45:24 |
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];
int dep[N], pre[N], w[N];
ll dis[N];
bool flag[N];
void dfs(int u, int fa, int lst = 0){
dep[u] = dep[fa] + 1, pre[u] = fa;
for(auto &[v, i] : G[u]){
if(dep[v]){
if(i != lst) flag[i] = 1;
continue;
}
g[u].pb({v, i});
w[v] = e[i].u == u, dis[v] = dis[u] + e[i].w;
dfs(v, u, i);
}
}
vector<int> dfs2(int u){
vector<int> cur;
for(auto &[v, i] : g[u]){
vector<int> t = dfs2(v);
if(w[v] == w[u]) for(auto &j : t) cur.pb(j);
if(id[u]){
for(auto &j : t){
if(w[v]) G[id[u]].pb({id[j], dis[j] - dis[u]});
else G[id[j]].pb({id[u], dis[j] - dis[u]});
}
}
}
if(id[u]) cur.pb(u);
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);
// if(n > 2323) return writeln(tt), 0;
_rep(i, 1, n) G[i].clear();
_rep(i, 1, m){
if(!flag[i]) continue;
if(!id[e[i].u]) id[e[i].u] = ++cnt;
if(!id[e[i].v]) id[e[i].v] = ++cnt;
// debug("%d -> %d\n", id[e[i].u], id[e[i].v]);
G[id[e[i].u]].pb({id[e[i].v], e[i].w});
}
assert(cnt <= 3000);
dfs2(1);
_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: 4ms
memory: 28832kb
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: 4ms
memory: 26296kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 32276kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 156ms
memory: 96680kb
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: 0
Accepted
time: 302ms
memory: 78244kb
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:
98674714245
result:
ok single line: '98674714245'
Test #6:
score: 0
Accepted
time: 291ms
memory: 81112kb
input:
270530 271285 80489 81855 218173724 188930 190845 783975756 29830 30626 22189315 234320 234472 70840355 198096 198272 300313423 224194 226906 105128197 115010 115834 37228105 134788 135583 18647938 257292 257358 98569041 146988 147215 69398857 248752 250002 409565478 62128 63751 839744551 121918 122...
output:
133486910467
result:
ok single line: '133486910467'
Test #7:
score: -100
Wrong Answer
time: 304ms
memory: 99080kb
input:
222087 223141 123107 123811 2984035 216346 217464 675263 139741 141286 892140 77973 78018 453931100 38603 39546 157182459 13105 14616 775862 97035 97704 379136464 86254 88311 84193802 83968 84398 246202498 152486 160164 65619516 73213 73517 1129576 15618 16541 498613468 192241 195576 889879 21363 21...
output:
54531778084
result:
wrong answer 1st lines differ - expected: '47599478278', found: '54531778084'