답안 #1047926

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1047926 2024-08-07T17:40:23 Z vjudge1 Bitaro the Brave (JOI19_ho_t1) C++17
0 / 100
3 ms 5212 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
using i64 = long long;
const i64 oo = 1e18;
#define eb emplace_back
using pii = pair<int, int>;
using pli = pair<i64, int>;
int n, m;
vector<pii> adj[N];
vector<i64> dist[4];
vector<i64> dp[2];
priority_queue<pli, vector<pli>, greater<pli>> Q;
int a[4];
i64 ans = oo;
 
#define S 0
#define T 1
#define U 2
#define V 3
 
void dijkstra(int u, vector<i64> &dist) {
    dist.assign(n + 1, oo);
    Q.emplace(0, u);
    dist[u] = 0;
 
    while(!Q.empty()) {
        pli top = Q.top();
        Q.pop();
        int u = top.second;
 
        if(dist[u] != top.first)
            continue;
 
        for(auto [v, w] : adj[u]) {
            if(dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                Q.emplace(dist[v], v);
            }
        }
    }
}
 
void solve(int type) {
    vector<pli> res;
 
    for(int i = 1; i <= n; i++) {
        if(dist[S][a[T]] == dist[S][i] + dist[T][i])
            res.eb(dist[type][i], i);
    }
 
    sort(res.begin(), res.end());
 
    for(pli x : res) {
        int u = x.second;
        dp[0][u] = dist[U][u];
        dp[1][u] = dist[V][u];
 
        for(auto [v, w] : adj[u]) {
            if(dist[type][v] + w == dist[type][u]) {
                dp[0][u] = min(dp[0][u], dp[0][v]);
                dp[1][u] = min(dp[1][u], dp[1][v]);
            }
        }
 
        ans = min(ans, min(dp[0][u] + dist[V][u], dp[1][u] + dist[U][u]));
    }
}
 
int main() {
    if(fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
 
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
 
    for(int i = 0; i < 4; i++)
        cin >> a[i];
 
    for(int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].eb(v, w);
        adj[v].eb(u, w);
    }
 
    for(int i = 0; i < 4; i++)
        dijkstra(a[i], dist[i]);
 
//    for(int j = 0; j < 4; j++) {
//        cout << j << " : ";
//
//        for(int i = 1; i <= n; i++)
//            cout << dist[j][i] << " ";
//
//        cout << "\n";
//    }
    ans = dist[V][a[U]];
    dp[0].assign(n + 1, oo);
    dp[1].assign(n + 1, oo);
    solve(0);
    solve(1);
    cout << ans;
}

Compilation message

joi2019_ho_t1.cpp: In function 'int main()':
joi2019_ho_t1.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
joi2019_ho_t1.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 5212 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 5212 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 5212 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -