結果

問題 No.614 壊れたキャンパス
ユーザー mamekinmamekin
提出日時 2018-01-14 13:10:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 2,038 bytes
コンパイル時間 1,287 ms
コンパイル使用メモリ 121,084 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2023-08-25 17:11:24
合計ジャッジ時間 5,910 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 155 ms
6,012 KB
testcase_09 AC 239 ms
12,748 KB
testcase_10 AC 263 ms
10,700 KB
testcase_11 AC 162 ms
5,848 KB
testcase_12 AC 219 ms
5,960 KB
testcase_13 AC 177 ms
5,872 KB
testcase_14 AC 186 ms
6,064 KB
testcase_15 AC 186 ms
8,028 KB
testcase_16 AC 175 ms
7,016 KB
testcase_17 AC 237 ms
14,336 KB
testcase_18 AC 166 ms
10,008 KB
testcase_19 AC 177 ms
11,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const long long INF = LLONG_MAX / 4;

int main()
{
    int n, m, k, s, t;
    cin >> n >> m >> k >> s >> t;

    vector<vector<pair<int, int> > > edges(n+1);
    edges[0].push_back(make_pair(-1, s));
    edges[n].push_back(make_pair(t, -1));
    for(int i=0; i<m; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        edges[a].push_back(make_pair(b, c));
    }

    vector<long long> dp(1, 0);
    for(int i=0; i<n; ++i){
        vector<tuple<int, int, bool> > v;
        for(unsigned j=0; j<edges[i].size(); ++j)
            v.push_back(make_tuple(edges[i][j].second, j, true));
        for(unsigned j=0; j<edges[i+1].size(); ++j)
            v.push_back(make_tuple(edges[i+1][j].first, j, false));

        vector<long long> nextDp(edges[i+1].size(), INF);
        long long cost = INF;
        sort(v.begin(), v.end());
        for(const auto& t : v){
            int pos, k;
            bool isInput;
            tie(pos, k, isInput) = t;
            if(isInput)
                cost = min(cost, dp[k] - pos);
            else
                nextDp[k] = min(nextDp[k], cost + pos);
        }
        
        cost = INF * 2;
        reverse(v.begin(), v.end());
        for(const auto& t : v){
            int pos, k;
            bool isInput;
            tie(pos, k, isInput) = t;
            if(isInput)
                cost = min(cost, dp[k] + pos);
            else
                nextDp[k] = min(nextDp[k], cost - pos);
        }

        dp.swap(nextDp);
    }

    if(dp[0] < INF)
        cout << dp[0] << endl;
    else
        cout << -1 << endl;

    return 0;
}
0