Submission #18088325


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

void init(int n, int cost[100][100])
{
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            cost[i][j] = -1;
        cost[i][i] = 0;
    }
}

int calc_cost(int cost[100][100], int a, int b)
{
    --a; --b;
    return cost[a][b];
}

void update(int n, int cost[100][100], int c, int d)
{
    int cost_cd = cost[c][d];
    for (int i = 0; i < n; ++i) {
        int cost_ic = cost[i][c];
        if (cost_ic == -1)
            continue;
        for (int j = 0; j < n; ++j) {
            int cost_dj = cost[d][j];
            if (cost_dj == -1)
                continue;
            int cost_ij = cost_ic + cost_cd + cost_dj;
            if (cost[i][j] == -1 || cost[i][j] > cost_ij)
                cost[i][j] = cost_ij;
        }
    }
}

void update(int n, int cost[100][100], int c, int d, int e)
{
    --c; --d;
    if (cost[c][d] != -1 && cost[c][d] <= e)
        return;
    cost[c][d] = cost[d][c] = e;
    update(n, cost, c, d);
    update(n, cost, d, c);
}

int main()
{
    int n, k;
    cin >> n >> k;
    int cost[100][100];
    init(n, cost);
    for (int i = 0; i < k; ++i) {
        int info, a, b, c, d, e;
        cin >> info;
        if (info == 0) {
            cin >> a >> b;
            cout << calc_cost(cost, a, b) << endl;
        }
        if (info == 1) {
            cin >> c >> d >> e;
            update(n, cost, c, d, e);
        }
    }
}

Submission Info

Submission Time
Task F - 船旅
User atug
Language C++ (GCC 9.2.1)
Score 100
Code Size 1443 Byte
Status AC
Exec Time 23 ms
Memory 3604 KiB

Judge Result

Set Name set01 set02 set03 set04 set05
Score / Max Score 20 / 20 20 / 20 20 / 20 20 / 20 20 / 20
Status
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
Set Name Test Cases
set01 data1
set02 data2
set03 data3
set04 data4
set05 data5
Case Name Status Exec Time Memory
data1 AC 7 ms 3540 KiB
data2 AC 3 ms 3544 KiB
data3 AC 7 ms 3420 KiB
data4 AC 20 ms 3604 KiB
data5 AC 23 ms 3592 KiB