QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#377692 | #7991. 最小环 | gogo11# | TL | 7ms | 27072kb | C++14 | 2.0kb | 2024-04-05 16:45:04 | 2024-04-05 16:45:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define lowbit(x) ((x) & (-x))
constexpr int N = 2e5 + 10;
constexpr int M = 5e5 + 10;
constexpr ll mod = (1LL<<20);
constexpr int INF = 0x3f3f3f3f;
constexpr ll Base = 131;
constexpr int MAXBIT = 30;
#define pb push_back
constexpr int maxn = 1e6 + 10;
constexpr ll LNF = 0x3f3f3f3f3f3f3f3f;
#define int long long
ll n, m, ans = LNF;
int top = 0;
pll st[maxn];
bool vis[maxn], inst[maxn];
vector<pll> g[maxn];
void dfs(int u, ll fauw = 0)
{
vis[u] = 1;
inst[u] = true;
st[++ top] = {u, fauw};
for(auto it : g[u])
{
ll v = it.first, w = it.second;
if(vis[v])
{
if(inst[v])
{
// vector<int> path;
// path.pb(v);
int t = top;
ll res = w;
while(st[t].first != v)
{
// path.pb(st[t].first);
res += st[t--].second;
}
if(res < ans) ans = res;
// cout << res << " -> ";
// for(auto j : path)
// cout << j << " ";
// cout << "\n";
}
continue;
}
dfs(v, w);
}
inst[u] = false;
-- top;
vis[u] = 0;
}
void solve()
{
cin >> n >> m;
for(int i = 1; i <= m; i ++)
{
ll u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
}
// for(int i = n; i >= 1; i --)
for(int i = 1; i <= n; i ++)
// if(!vis[i]) {
dfs(i);
// }
// if(n >= 1)
// {
// memset(vis, 0, sizeof(vis));
// memset(inst, false, sizeof(inst));
// top = 0;
// int flag = n / 7 + 1;
// dfs(flag);
// for(int i = n; i >= 1; i --)
// if(!vis[i]) {
// dfs(i);
// }
// }
if(ans == LNF) cout << -1 << '\n';
else cout << ans << "\n";
}
signed main()
{
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// #endif
int _ = 1;
// cin >> _;
while (_--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 27072kb
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: 3ms
memory: 27012kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 27032kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: -100
Time Limit Exceeded
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...