제출 #224129

#제출 시각아이디문제언어결과실행 시간메모리
224129dwscOlympic Bus (JOI20_ho_t4)C++14
16 / 100
136 ms4876 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> ii;
typedef pair<ii,int> i3;
main(){
    int n,m;
    cin >> n >> m;
    if (m <= 1000){
        int ans = 1e18;
        i3 edges[m];
        int invert[m+1];
        for (int i = 0; i < m; i++) {
            cin >> edges[i].first.first >> edges[i].first.second >> edges[i].second;
            cin >> invert[i];
        }
        invert[m] = 0;
        for (int i = 0; i < m+1; i++){
            int temp = invert[i];
            vector<ii> tempadj[n+1];
            for (int j = 0; j < m; j++){
                if (j == i) tempadj[edges[j].first.second].push_back(ii(edges[j].first.first,edges[j].second));
                else tempadj[edges[j].first.first].push_back(ii(edges[j].first.second,edges[j].second));
            }
            int dist1[n+1],dist2[n+1];
            for (int j = 1; j <= n; j++) dist1[j] = dist2[j] = 1e18;
            priority_queue<ii,vector<ii>,greater<ii> >pq;
            pq.push(ii(0,1));
            dist1[1] = 0;
            while (!pq.empty()){
                ii f = pq.top();
                pq.pop();
                int d = f.first, u = f.second;
                if (d > dist1[u]) continue;
                for (int j = 0; j < tempadj[u].size(); j++){
                    ii v = tempadj[u][j];
                    if (dist1[u]+v.second < dist1[v.first]){
                        dist1[v.first] = dist1[u]+v.second;
                        pq.push(ii(dist1[v.first],v.first));
                    }
                }
            }
            pq.push(ii(0,n));
            dist2[n] = 0;
            while (!pq.empty()){
                ii f = pq.top();
                pq.pop();
                int d = f.first, u = f.second;
                if (d > dist2[u]) continue;
                for (int j = 0; j < tempadj[u].size(); j++){
                    ii v = tempadj[u][j];
                    if (dist2[u]+v.second < dist2[v.first]){
                        dist2[v.first] = dist2[u]+v.second;
                        pq.push(ii(dist2[v.first],v.first));
                    }
                }
            }
            ans = min(ans,temp+dist1[n]+dist2[1]);
        }
        if (ans == 1e18) cout << -1;
        else cout << ans;
    }
    else{
        i3 edges[m];
        int invert[m+1];
        vector<ii> adj[n+1];
        vector<ii> radj[n+1];
        for (int i = 0; i < m; i++) {
            cin >> edges[i].first.first >> edges[i].first.second >> edges[i].second;
            cin >> invert[i];
            adj[edges[i].first.first].push_back(ii(edges[i].first.second,edges[i].second));
            radj[edges[i].first.second].push_back(ii(edges[i].first.first,edges[i].second));
        }
        int dist1[n+1],dist2[n+1];
        for (int j = 1; j <= n; j++) dist1[j] = dist2[j] = 1e18;
        priority_queue<ii,vector<ii>,greater<ii> >pq;
        pq.push(ii(0,1));
        dist1[1] = 0;
        while (!pq.empty()){
            ii f = pq.top();
            pq.pop();
            int d = f.first, u = f.second;
            if (d > dist1[u]) continue;
            for (int j = 0; j < adj[u].size(); j++){
                ii v = adj[u][j];
                if (dist1[u]+v.second < dist1[v.first]){
                    dist1[v.first] = dist1[u]+v.second;
                    pq.push(ii(dist1[v.first],v.first));
                }
            }
        }
        pq.push(ii(0,n));
        dist2[n] = 0;
        while (!pq.empty()){
            ii f = pq.top();
            pq.pop();
            int d = f.first, u = f.second;
            if (d > dist2[u]) continue;
            for (int j = 0; j < adj[u].size(); j++){
                ii v = adj[u][j];
                if (dist2[u]+v.second < dist2[v.first]){
                    dist2[v.first] = dist2[u]+v.second;
                    pq.push(ii(dist2[v.first],v.first));
                }
            }
        }
        int rdist1[n+1],rdist2[n+1];
        for (int j = 1; j <= n; j++) rdist1[j] = rdist2[j] = 1e18;
        pq.push(ii(0,1));
        rdist1[1] = 0;
        while (!pq.empty()){
            ii f = pq.top();
            pq.pop();
            int d = f.first, u = f.second;
            if (d > rdist1[u]) continue;
            for (int j = 0; j < radj[u].size(); j++){
                ii v = radj[u][j];
                if (rdist1[u]+v.second < rdist1[v.first]){
                    rdist1[v.first] = rdist1[u]+v.second;
                    pq.push(ii(rdist1[v.first],v.first));
                }
            }
        }
        pq.push(ii(0,n));
        rdist2[n] = 0;
        while (!pq.empty()){
            ii f = pq.top();
            pq.pop();
            int d = f.first, u = f.second;
            if (d > rdist2[u]) continue;
            for (int j = 0; j < radj[u].size(); j++){
                ii v = radj[u][j];
                if (rdist2[u]+v.second < rdist2[v.first]){
                    rdist2[v.first] = rdist2[u]+v.second;
                    pq.push(ii(rdist2[v.first],v.first));
                }
            }
        }
        //for (int i = 1; i <= n; i++) cout << dist2[i] <<" ";
        //cout << "\n";
        //for (int i = 1; i <= n; i++) cout << rdist2[i] <<" ";
        //cout << "\n";
        int ans = 1e18;
        ans = min(ans,dist1[n]+dist2[1]);
        for (int i = 0; i < m; i++){
            int a = edges[i].first.first,b = edges[i].first.second,w = edges[i].second;
           // cout << a << " " << b << " " << w<< "hi\n";
            int temp1 = min(dist1[n],dist1[b]+w+rdist2[a]);
            int temp2 = min(dist2[1],dist2[b]+w+rdist1[a]);
            //cout << temp1 << " " << temp2 << " " << invert[i] << "\n";
            ans = min(ans,temp1+temp2+invert[i]);
        }
        if (ans == 1e18) cout << -1;
        else cout << ans;
    }
}

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

ho_t4.cpp:6:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:35:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int j = 0; j < tempadj[u].size(); j++){
                                 ~~^~~~~~~~~~~~~~~~~~~
ho_t4.cpp:50:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int j = 0; j < tempadj[u].size(); j++){
                                 ~~^~~~~~~~~~~~~~~~~~~
ho_t4.cpp:84:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < adj[u].size(); j++){
                             ~~^~~~~~~~~~~~~~~
ho_t4.cpp:99:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < adj[u].size(); j++){
                             ~~^~~~~~~~~~~~~~~
ho_t4.cpp:116:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < radj[u].size(); j++){
                             ~~^~~~~~~~~~~~~~~~
ho_t4.cpp:131:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < radj[u].size(); j++){
                             ~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...