跳到正文

题解

题解:P15805 [GESP202603 八级] 子图最短路

zhang_kevin约 6 分钟
原文

这是 2026年3月16日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月16日
收录
2026年3月16日
更新
2026年11月14日
篇幅
约 6 分钟
获赞
11
题目
P15805

版本

  1. v2当前
  2. v1

依题意模拟即可,先枚举 l,rl,r,然后暴力跑 floyd 算最短路。

时间复杂度我只能分析到 O(n5)\mathcal{O}(n^5),但就是过了。

C++
#include<bits/stdc++.h>
// #define int long long
#define fo(i, l, r) for(int i = (l); i <= (r); i++)
#define fd(i, l, r) for(int i = (l); i >= (r); i--)
#define fu(i, l, r) for(int i = (l); i <  (r); i++)
#define y1 zhang_kevin
#define pii pair<int, int>
#define fi first
#define se second
#define vec vector
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define ll long long
#define ull unsigned long long
#define flush() (fwrite(obuf, 1, p3 - obuf, stdout), p3 = obuf)
#define puts wrs
using namespace std;
bool ST;
char ibuf[1 << 20], *p1 = ibuf, *p2 = ibuf, obuf[1 << 20], *p3 = obuf;
inline char gc(){
    if(p1 == p2){
        p1 = ibuf, p2 = ibuf + fread(ibuf, 1, 1 << 20, stdin);
        if(p1 == p2) return EOF;
        return *p1++;
    }
    return *p1++;
}
inline char pc(char ch){
    if(p3 == obuf + (1 << 20)) flush();
    *p3 = ch;
    return *p3++;
}
template<typename type>
inline int rd(type &x){
    x = 0; bool f = 0; char ch = gc();
    while(!isdigit(ch)) f |= ch == '-', ch = gc();
    while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
    return f ? x = -x : 0;
}
template<typename type, typename ...T>
inline void rd(type &x, T &...y){rd(x), rd(y...);}
class Flush{public: ~Flush(){flush();}}_;
template<typename type>
inline void wr(type x){
    if(x < 0) pc('-'), x = -x;
    if(x > 9) wr(x / 10);
    pc(x % 10 + '0');
    return;
}
inline void wrs(string s){for(auto ch : s) pc(ch);}
namespace Solution{
    const ll inf = 1e18 + 114514ll, mo = 1000000000;
    int n, m;
    ll g[105][105], e[105][105];
    inline void Solve(){
        rd(n, m);
        fo(i, 1, n) fo(j, 1, n) g[i][j] = inf;
        fo(i, 1, m){
            int u, v, w; rd(u, v, w);
            g[u][v] = g[v][u] = min(g[u][v], (ll)w);
        }
        fo(i, 1, n) g[i][i] = 0;
        ll ans = 0;
        fo(l, 1, n){
            fo(r, l, n){
                fo(i, l, r) fo(j, l, r) e[i][j] = g[i][j];
                fo(k, l, r){
                    fo(i, l, r){
                        fo(j, l, r){
                            if(e[i][k] != inf && e[k][j] != inf){
                                e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
                            }
                        }
                    }
                }
                fo(u, l, r){
                    fo(v, u, r){
                        if(e[u][v] != inf){
                            ans = (ans + e[u][v]) % mo;
                        }
                    }
                }
            }
        }
        wr(ans);
        return;
    }
}
bool ED;
signed main(){
    clock_t START = clock();
    // freopen("input.in", "r", stdin), freopen("output.out", "w", stdout);
    Solution::Solve();
    cerr << (double)(clock() - START) / CLOCKS_PER_SEC << " s" << '\n';
    cerr << 1.0 * abs(&ED - &ST) / 1024 / 1024 << " MB" << '\n';
    return 0;
}