abc073d <Floyed + 枚举排列>

发布时间 2023-07-10 17:47:02作者: O2iginal

D - joisino's travel

// https://atcoder.jp/contests/abc073/tasks/abc073_d
// Floyed + 枚举排列
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
using LL = long long;
const int N = 210;
int g[N][N];
int q[10], p[10];

void solv()
{
    int n, m, r;
    cin >> n >> m >> r;
    for (int i = 1; i <= r; i++) 
    {
        cin >> q[i];
        p[i] = i;
    }
    int a, b, c;
    memset(g, 0x3f, sizeof g);  // 注意: 这里别太大, 会溢出
    // for (int i = 1; i <= n; i ++)
    //     for (int j = 1; j <= n; j ++)
    //         if (i != j) g[i][j] = 0x3f3f3f3f;
    while (m --)
    {
        cin >> a >> b >> c;
        g[a][b] = g[b][a] = c;
    }
    for (int k = 1; k <= n; k ++)
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
    int ans = 2e9;
    do
    {
        int t = 0;
        for (int i = 1; i < r; i ++) t += g[q[p[i]]][q[p[i+1]]];
        ans = min(ans, t);
    } while (next_permutation(p+1, p+1+r));
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T = 1;
	// cin >> T;
    while (T --)
    {
        solv();
    }
    return 0;
}