QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#772742#7991. 最小环ToboTL 0ms3920kbC++204.3kb2024-11-22 21:31:032024-11-22 21:31:12

Judging History

你现在查看的是最新测评结果

  • [2024-11-22 21:31:12]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3920kb
  • [2024-11-22 21:31:03]
  • 提交

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);
    vector<multiset<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)
        {
            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;
        adj[from].erase(adj[from].find({cur, rev[cur].begin()->second}));
        rev[to].erase(rev[to].find({cur, adj[cur].begin()->second}));
        deg[cur] = 0;
        adj[cur] = rev[cur] = {};
        adj[from].insert({to, len});
    }

    for (int t = 1; t <= n; t++)
    {
        if (!deg[t])
            continue;
        vector<pair<int, long long>> tmp{adj[t].begin(), adj[t].end()};
        for (auto &[s, len] : tmp)
        {
            adj[t].erase(adj[t].find({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});
            }
            ans = min(ans, dis[t] + len);
            adj[t].insert({s, len});
        }
    }
    if (ans == 1e18)
        ans = -1;
    cout << ans << '\n';
}
/*
2 2
1 2 1
2 1 1
 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3920kb

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: 3836kb

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: -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...

output:


result: