QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#296292 | #7991. 最小环 | stcmuy | TL | 85ms | 25376kb | C++20 | 1.8kb | 2024-01-02 17:59:52 | 2024-01-02 17:59:53 |
Judging History
answer
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define i64 long long
#define i128 __int128
#define int long long
#define endl '\n'
using namespace std;
const i64 mod = 1e18;
const int maxn = 3e5+10;
struct node {int u,v,w;};
signed main()
{
IOS;
int n,m;
cin >> n >> m;
vector<node> e;
i64 ans = mod;
for(int i = 1; i <= m; ++i)
{
int x,y,z;
cin >> x >> y >> z;
if(x == y)
{
ans = min(ans,z);
continue;
}
e.push_back((node){x,y,z});
}
vector<int> f(n+1);
for(int i = 1; i <= n; ++i) f[i] = i;
auto get = [&](auto self,int x) -> int
{
if(f[x] == x) return x;
return f[x] = self(self,f[x]);
};
sort(e.begin(),e.end(),[&](node a,node b){return a.w < b.w;});
vector<vector<pair<int,int>>> v(n+1);
vector<node> qs;
for(node &a : e)
{
int s1 = get(get,a.u);
int s2 = get(get,a.v);
if(s1 != s2)
{
v[a.u].push_back({a.v,a.w});
f[s1] = s2;
}
else qs.push_back(a);
}
for(node &a : qs)
{
vector<int> dis(n+1,mod);
dis[a.v] = 0;
priority_queue<pair<int,int>> q;
q.push({0,a.v});
while(q.size())
{
int x = q.top().second;
q.pop();
for(auto [u,val] : v[x])
{
if(dis[u] > dis[x] + val)
{
dis[u] = dis[x] + val;
q.push({-dis[u],u});
}
}
}
ans = min(ans,dis[a.u] + a.w);
}
if(ans == mod) cout << -1 << endl;
else cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3552kb
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: 3488kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 85ms
memory: 25376kb
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
Time Limit Exceeded
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 ...