답안 #1113187

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113187 2024-11-16T03:13:44 Z votranngocvy 페리들 (NOI13_ferries) C++14
0 / 40
165 ms 23624 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e5 + 5;
const int inf = 1e18;
int dist[N],n,m,d[N];
vector<int>g[N],cost[N],adj[N];

void bfs(int s) {
    for (int i = 1; i <= n; i++) d[i] = inf;
    queue<int>q;
    q.push(s);
    d[s] = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto v: adj[u]) {
            if (d[v] != inf) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
}

void dijkstra(int s) {
    priority_queue<pii,vector<pii>,greater<pii>>q;
    for (int i = 1; i <= n; i++) dist[i] = inf;
    q.push({0,s});
    dist[s] = 0;
    while (!q.empty()) {
        int u = q.top().se,c = q.top().fi;
        q.pop();
        if (dist[u] != c) continue;
        //minimum path match with maximum cost
        for (int i = 0; i < (int)g[u].size(); i++) {
            int v = g[u][i],c = cost[u][i];
            if (dist[v] > dist[u] + c) {
                dist[v] = dist[u] + c;
                q.push({dist[v],v});
            }
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u,v,c;
        cin >> u >> v >> c;
        g[u].push_back(v);
        adj[v].push_back(u);
        cost[u].push_back(c);
    }
    for (int i = 1; i <= n; i++)
        sort(cost[i].begin(),cost[i].end(),greater<int>());
    bfs(n);
    for (int i = 1; i <= n; i++) {
        sort(g[i].begin(),g[i].end(),[&](int &a,int &b) {
             return d[a] < d[b];
        });
    }
    dijkstra(1);
    cout << dist[n] << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 7504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7504 KB Output is correct
2 Incorrect 6 ms 7600 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 9040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 165 ms 23624 KB Output isn't correct
2 Halted 0 ms 0 KB -