제출 #1336806

#제출 시각아이디문제언어결과실행 시간메모리
1336806SemicolonCommuter Pass (JOI18_commuter_pass)C++20
0 / 100
182 ms21732 KiB
/**
 *     Author: Lưu Diệp Thành (Save Diệp Thành)
 *     Le Hong Phong High School for the Gifted (i2528)
**/
#include<bits/stdc++.h>
using namespace std;

#define int long long
//#define ll long long
#define ushort unsigned short
#define FOR(i,l,r) for(int i = (l), _r = (r); i <= _r; i++)
#define FORN(i,r,l) for(int i = (r), _l = (l); i >= _l; i--)
#define endl '\n'
#define sz(x) (int)x.size()
#define fi first
#define se second
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
#define ins insert
#define segleft (id<<1)
#define segright (id<<1|1)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)

const int MOD = 1e9+7;
const int INF = LLONG_MAX;
const int N = 1e5+5;

vector<pair<int,int>> edge[N];
vector<int> distU, distV;

int n, m, s, t, u, v;

vector<int> dijkstra(int st) {
    vector<int> dist(n+5, INF);
    dist[st] = 0;

    priority_queue<
        pair<int,int>,
        vector<pair<int,int>>,
        greater<pair<int,int>>
    > q;

    q.push({0, st});

    while(!q.empty()) {
        auto [du, u] = q.top();
        q.pop();

        if (du > dist[u]) continue;

        for(auto [v,w] : edge[u]) {
            if (dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                q.push({dist[v], v});
            }
        }
    }

    return dist;
}

struct State {
    int d, u, par;
};

struct cmp {
    bool operator()(const State &a, const State &b) const {
        return a.d > b.d;
    }
};

int getans(int st) {
    vector<int> dist(n+5, LLONG_MAX);
    vector<int> dp1(n+5, LLONG_MAX), dp2(n+5, LLONG_MAX);

    priority_queue<State, vector<State>, cmp> q;

    dist[st] = 0;
    q.push({0, st, 0});

    while(!q.empty()) {
        auto [curdist, cur, par] = q.top(); q.pop();
        if (curdist != dist[cur]) continue;

        dp1[cur] = min({dp1[cur], distU[cur], dp1[par]});
        dp2[cur] = min({dp2[cur], dp1[cur] + distV[cur], dp2[par]});

        for(auto[v,w] : edge[cur]) {
            if (dist[v] > dist[cur] + w) {
                dist[v] = dist[cur] + w;
                q.push({dist[v], v, cur});
            } else if (dist[v] <= dist[cur] + w) {
                dp1[v] = min(dp1[v], dp1[cur]);
                dp2[v] = min(dp2[v], dp2[cur]);
            }
        }
    }

    return dp2[(st == s ? t : s)];
}

void Semicolon() {
    cin >> n >> m >> s >> t >> u >> v;

    FOR(i, 1, m) {
        int u,v,w;
        cin >> u >> v >> w;
        edge[u].pb({v,w});
        edge[v].pb({u,w});
    }

    vector<int> dist = dijkstra(u);
    swap(dist, distU);

    dist = dijkstra(v);
    swap(dist, distV);

    // cout << getans(s) << endl;
    // cout << getans(t) << endl;

    int ans1 = getans(s), ans2 = getans(t);
    cout << min({ans1, ans2, distU[v], distV[u]}) << endl;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("", "r", stdin);
    //freopen("", "w", stdout);
    if (fopen("input.txt", "r")) freopen("input.txt", "r", stdin);

    int t = 1;
    //cin >> t;
    while(t--) Semicolon();

    cerr << endl;
    cerr << "Time elapsed: " << TIME << " s.\n ";
    return (0 ^ 0);
}

컴파일 시 표준 에러 (stderr) 메시지

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:132:41: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |     if (fopen("input.txt", "r")) freopen("input.txt", "r", stdin);
      |                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...