最短路-Floyd

LANSGANBS發表於2024-05-23

Floyd

//
// Created by LANSGANBS on 2024/3/18.
//
/*
 * code template: https://github.com/LANSGANBS/code-template
 * local: C:\Users\18019\CLionProjects\.cpp-code
 * URL: https://www.luogu.com.cn/problem/B3647
 * Status: AC
 * 寫完這道就去原
*/
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl '\n'
#define buff ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define debug cout << "********************************************" << endl;
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define _REP(i, b, a) for (int i = b; i >= a; i--)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define _rep(i, b, a) for (int i = b; i > a; i--)
#define all(x) begin(x), end(x)
#define mem(a, x) memset(a, x, sizeof(a))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a / gcd(a, b) * b)
#define sz(x) (int)x.size()
#define lowbit(x) (x & -x)
#define pb push_back
#define ll long long
#define int ll
#define ld long double
#define vi vector<int>
#define pii pair<int,int>

i64 ceilDiv(i64 n, i64 m) // up
{
    if (n >= 0)
    {
        return (n + m - 1) / m;
    }
    else
    {
        return n / m;
    }
}

i64 floorDiv(i64 n, i64 m) // low
{
    if (n >= 0)
    {
        return n / m;
    }
    else
    {
        return (n - m + 1) / m;
    }
}

template<typename T1, typename T2>
istream &operator>>(istream &cin, pair<T1, T2> &a)
{
    return cin >> a.first >> a.second;
}

template<typename T1>
istream &operator>>(istream &cin, vector<T1> &a)
{
    for (auto &x: a)
    {
        cin >> x;
    }
    return cin;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &cout, const pair<T1, T2> &a)
{
    return cout << a.first << ' ' << a.second;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &cout, const vector<pair<T1, T2>> &a)
{
    for (auto &x: a)
    {
        cout << x << endl;
    }
    return cout;
}

template<typename T1>
ostream &operator<<(ostream &cout, const vector<T1> &a)
{
    int n = a.size();
    if (!n)
    {
        return cout;
    }
    cout << a[0];
    for (int i = 1; i < n; i++)
    {
        cout << ' ' << a[i];
    }
    return cout;
}

template<typename... Args>
void see(Args &...args)
{
    ((cin >> args), ...);
}

template<typename... Args>
void out(Args... args)
{
    ((cout << args << " "), ...);
}

template<typename... Args>
void outl(Args... args)
{
    ((cout << args << endl), ...);
}

const int mod = 1e9 + 7;
#ifdef ONLINE_JUDGE
constexpr int N = 1e4 + 7;
#else
constexpr int N = 1e3 + 7;
#endif

int g[N][N], dis[N][N];

void solve()
{
    int n, m;
    see(n, m);
    REP(i, 1, n)
    {
        REP(j, 1, n)
        {
            g[i][j] = dis[i][j] = i == j ? 0 : 0x3f3f3f3f;
        }
    }
    while (m--)
    {
        int u, v, w;
        cin >> u >> v >> w;
        g[u][v] = g[v][u] = min(g[u][v], w);
        dis[u][v] = dis[v][u] = min(dis[u][v], w);
    }
    REP (k, 1, n)
    {
        REP (i, 1, n)
        {
            REP (j, 1, n)
            {
                dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
            }
        }
    }
    REP (i, 1, n)
    {
        REP (j, 1, n)
        {
            cout << dis[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;
}

signed main()
{
    buff;
    int tt = 1;
    // cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

相關文章