Submission #49739929


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 20;

struct E
{
    int to, w;
};

int n, m, dp[1 << N][N], dis[N][N];
vector<E> g[N];

signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n >> m;
    for (int i = 0, u, v, w; i < m; i++) cin >> u >> v >> w, u--, v--, g[u].push_back({v, w});
    memset(dis, 0x3f, sizeof dis);
    for (int i = 0; i < n; i++) 
        for (E e : g[i]) dis[i][e.to] = e.w;
    for (int i = 0; i < n; i++) dis[i][i] = 0;
    for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (dis[i][k] + dis[k][j] < dis[i][j]) dis[i][j] = dis[i][k] + dis[k][j];
    // for (int i = 0; i < n; i++)
    //     for (int j = 0; j < n; j++) cout << i << ' ' << j << ' ' << dis[i][j] << endl;
    memset(dp, 0x3f, sizeof dp);
    for (int i = 0; i < n; i++) dp[1 << i][i] = 0;
    for (int s = 1; s < (1 << n); s++)
    {
        vector<int> v;
        for (int i = 0; i < n; i++)
        {
            if (!(s & (1 << i))) continue;
            v.push_back(i);
        }
        for (int i : v)
            for (int j : v)
                dp[s][j] = min(dp[s][j], dp[s][i] + dis[i][j]);
        for (int i : v)
        {
            for (int k = 0; k < n; k++)
            {
                if (s & (1 << k)) continue;
                dp[s | (1 << k)][k] = min(dp[s | (1 << k)][k], dp[s][i] + dis[i][k]);
            }
        }
        // for (int i = 0; i < n; i++)
        // cout << bitset<3>(s) << ' ' << i << ' ' << dp[s][i] << endl;
    }
    int ans = LLONG_MAX;
    for (int i = 0; i < n; i++) ans = min(ans, dp[(1 << n) - 1][i]);
    if (ans > 1e18) cout << "No";
    else cout << ans;
}

Submission Info

Submission Time
Task F - Negative Traveling Salesman
User user10086
Language C++ 20 (gcc 12.2)
Score 500
Code Size 1794 Byte
Status AC
Exec Time 620 ms
Memory 167480 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 34
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_tree_00.txt, 02_tree_01.txt, 02_tree_02.txt, 02_tree_03.txt, 02_tree_04.txt, 03_minmax_00.txt, 03_minmax_01.txt, 03_minmax_02.txt, 04_killer_00.txt, 04_killer_01.txt, 04_killer_02.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 67 ms 167368 KiB
00_sample_01.txt AC 66 ms 167348 KiB
00_sample_02.txt AC 67 ms 167476 KiB
01_random_00.txt AC 615 ms 167348 KiB
01_random_01.txt AC 192 ms 167432 KiB
01_random_02.txt AC 617 ms 167228 KiB
01_random_03.txt AC 67 ms 167228 KiB
01_random_04.txt AC 615 ms 167368 KiB
01_random_05.txt AC 73 ms 167432 KiB
01_random_06.txt AC 617 ms 167356 KiB
01_random_07.txt AC 190 ms 167232 KiB
01_random_08.txt AC 617 ms 167296 KiB
01_random_09.txt AC 69 ms 167368 KiB
01_random_10.txt AC 616 ms 167312 KiB
01_random_11.txt AC 68 ms 167344 KiB
01_random_12.txt AC 614 ms 167360 KiB
01_random_13.txt AC 67 ms 167364 KiB
01_random_14.txt AC 614 ms 167440 KiB
01_random_15.txt AC 328 ms 167448 KiB
01_random_16.txt AC 616 ms 167372 KiB
01_random_17.txt AC 328 ms 167444 KiB
01_random_18.txt AC 616 ms 167376 KiB
01_random_19.txt AC 67 ms 167364 KiB
02_tree_00.txt AC 616 ms 167480 KiB
02_tree_01.txt AC 615 ms 167360 KiB
02_tree_02.txt AC 620 ms 167296 KiB
02_tree_03.txt AC 619 ms 167436 KiB
02_tree_04.txt AC 617 ms 167360 KiB
03_minmax_00.txt AC 615 ms 167276 KiB
03_minmax_01.txt AC 617 ms 167476 KiB
03_minmax_02.txt AC 67 ms 167348 KiB
04_killer_00.txt AC 616 ms 167432 KiB
04_killer_01.txt AC 616 ms 167260 KiB
04_killer_02.txt AC 617 ms 167372 KiB