提出 #7224908


ソースコード 拡げる

Copy
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) begin(x), end(x)
using ll = long long;
using namespace std;
template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); }
/**
* @note O((E + V) log V)
*/
vector<ll> dijkstra(vector<vector<pair<int, ll> > > const & g, int root) {
vector<ll> dist(g.size(), LLONG_MAX);
priority_queue<pair<ll, int> > que;
dist[root] = 0;
que.emplace(- dist[root], root);
while (not que.empty()) {
ll dist_i; int i; tie(dist_i, i) = que.top(); que.pop();
if (dist[i] < - dist_i) continue;
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) begin(x), end(x)
using ll = long long;
using namespace std;
template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); }

/**
 * @note O((E + V) log V)
 */
vector<ll> dijkstra(vector<vector<pair<int, ll> > > const & g, int root) {
    vector<ll> dist(g.size(), LLONG_MAX);
    priority_queue<pair<ll, int> > que;
    dist[root] = 0;
    que.emplace(- dist[root], root);
    while (not que.empty()) {
        ll dist_i; int i; tie(dist_i, i) = que.top(); que.pop();
        if (dist[i] < - dist_i) continue;
        for (auto it : g[i]) {
            int j; ll cost; tie(j, cost) = it;
            if (- dist_i + cost < dist[j]) {
                dist[j] = - dist_i + cost;
                que.emplace(dist_i - cost, j);
            }
        }
    }
    return dist;
}

/**
 * @description find the cost to connect edges from x1 and x2 to y
 */
vector<ll> dijkstra_two_nodes(vector<vector<pair<int, ll> > > const & g, int root1, int root2) {
    vector<ll> dist1 = dijkstra(g, root1);
    vector<ll> dist2 = dijkstra(g, root2);
    vector<ll> dist(g.size(), LLONG_MAX);
    priority_queue<pair<ll, int> > que;
    REP (i, g.size()) if (dist1[i] != LLONG_MAX and dist2[i] != LLONG_MAX) {
        dist[i] = dist1[i] + dist2[i];
        que.emplace(- dist[i], i);
    }
    while (not que.empty()) {
        ll dist_i; int i; tie(dist_i, i) = que.top(); que.pop();
        if (dist[i] < - dist_i) continue;
        for (auto it : g[i]) {
            int j; ll cost; tie(j, cost) = it;
            if (- dist_i + cost < dist[j]) {
                dist[j] = - dist_i + cost;
                que.emplace(dist_i - cost, j);
            }
        }
    }
    return dist;
}

int main() {
    // input
    int n, m; cin >> n >> m;
    int s1, t1, s2, t2; cin >> s1 >> t1 >> s2 >> t2;
    -- s1;
    -- t1;
    -- s2;
    -- t2;
    vector<vector<pair<int, ll> > > g(n);
    vector<vector<pair<int, ll> > > g_inv(n);
    REP (i, m) {
        ll cost; int s, t; cin >> cost >> s >> t;
        -- s;
        -- t;
        g[s].emplace_back(t, cost);
        g_inv[t].emplace_back(s, cost);
    }

    // solve
    auto from_s1 = dijkstra(g, s1);
    auto from_s2 = dijkstra(g, s2);
    auto to_t1 = dijkstra(g_inv, t1);
    auto to_t2 = dijkstra(g_inv, t2);
    auto from = dijkstra_two_nodes(g, s1, s2);
    auto to = dijkstra_two_nodes(g_inv, t1, t2);
    ll answer = LLONG_MAX;
    if (from_s1[t1] != LLONG_MAX and from_s2[t2] != LLONG_MAX) {;
        chmin(answer, from_s1[t1] + from_s2[t2]);
        REP (i, n) if (from[i] != LLONG_MAX and to[i] != LLONG_MAX) {
            chmin(answer, from[i] + to[i]);
        }
    }

    // output
    if (answer == LLONG_MAX) {
        cout << "Impossible" << endl;
    } else {
        cout << answer << endl;
    }
    return 0;
}

提出情報

提出日時
問題 F - Road Construction
ユーザ kimiyuki
言語 C++14 (GCC 5.4.1)
得点 100
コード長 3095 Byte
結果 AC
実行時間 471 ms
メモリ 27048 KB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 100 / 100
結果
AC × 3
AC × 35
セット名 テストケース
Sample 00_sample_01, 00_sample_02, 00_sample_03
All 00_sample_01, 00_sample_02, 00_sample_03, 01_small_01, 01_small_02, 01_small_03, 01_small_04, 01_small_05, 02_random_01, 02_random_02, 02_random_03, 02_random_04, 02_random_05, 02_random_06, 02_random_07, 02_random_08, 02_random_09, 02_random_10, 03_special_01, 03_special_02, 10_random_01, 10_random_02, 10_random_03, 10_random_04, 10_random_05, 10_random_06, 10_random_07, 10_random_08, 10_random_09, 10_random_10, 10_random_11, 10_random_12, 10_random_13, 10_random_14, 10_random_15
ケース名 結果 実行時間 メモリ
00_sample_01 AC 1 ms 256 KB
00_sample_02 AC 1 ms 256 KB
00_sample_03 AC 1 ms 256 KB
01_small_01 AC 1 ms 256 KB
01_small_02 AC 1 ms 256 KB
01_small_03 AC 1 ms 256 KB
01_small_04 AC 1 ms 256 KB
01_small_05 AC 1 ms 256 KB
02_random_01 AC 225 ms 17480 KB
02_random_02 AC 226 ms 18536 KB
02_random_03 AC 215 ms 12764 KB
02_random_04 AC 294 ms 15180 KB
02_random_05 AC 461 ms 23092 KB
02_random_06 AC 193 ms 9496 KB
02_random_07 AC 393 ms 22608 KB
02_random_08 AC 106 ms 6924 KB
02_random_09 AC 412 ms 23264 KB
02_random_10 AC 335 ms 14532 KB
03_special_01 AC 471 ms 27048 KB
03_special_02 AC 311 ms 14900 KB
10_random_01 AC 89 ms 6904 KB
10_random_02 AC 73 ms 9928 KB
10_random_03 AC 116 ms 6400 KB
10_random_04 AC 134 ms 13256 KB
10_random_05 AC 233 ms 16264 KB
10_random_06 AC 164 ms 11696 KB
10_random_07 AC 206 ms 18120 KB
10_random_08 AC 196 ms 17672 KB
10_random_09 AC 168 ms 9080 KB
10_random_10 AC 55 ms 12072 KB
10_random_11 AC 188 ms 15944 KB
10_random_12 AC 185 ms 10392 KB
10_random_13 AC 247 ms 15960 KB
10_random_14 AC 217 ms 17440 KB
10_random_15 AC 94 ms 9816 KB


2025-04-09 (水)
21:50:05 +00:00