結果

問題 No.1301 Strange Graph Shortest Path
ユーザー mel02mel02
提出日時 2020-11-27 22:29:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,750 bytes
コンパイル時間 1,921 ms
コンパイル使用メモリ 186,656 KB
実行使用メモリ 23,064 KB
最終ジャッジ日時 2023-10-09 21:30:15
合計ジャッジ時間 12,999 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 233 ms
17,252 KB
testcase_04 AC 336 ms
21,772 KB
testcase_05 AC 244 ms
18,476 KB
testcase_06 AC 296 ms
19,940 KB
testcase_07 AC 282 ms
19,348 KB
testcase_08 AC 230 ms
17,208 KB
testcase_09 AC 286 ms
19,392 KB
testcase_10 WA -
testcase_11 AC 300 ms
20,280 KB
testcase_12 AC 311 ms
20,500 KB
testcase_13 AC 285 ms
19,580 KB
testcase_14 AC 269 ms
18,880 KB
testcase_15 AC 268 ms
18,756 KB
testcase_16 AC 343 ms
21,716 KB
testcase_17 AC 298 ms
20,236 KB
testcase_18 AC 265 ms
18,468 KB
testcase_19 AC 300 ms
20,352 KB
testcase_20 AC 301 ms
19,960 KB
testcase_21 AC 287 ms
19,980 KB
testcase_22 AC 310 ms
20,564 KB
testcase_23 AC 275 ms
20,072 KB
testcase_24 AC 298 ms
20,140 KB
testcase_25 AC 315 ms
21,328 KB
testcase_26 AC 279 ms
19,780 KB
testcase_27 AC 296 ms
20,008 KB
testcase_28 AC 244 ms
18,132 KB
testcase_29 WA -
testcase_30 AC 310 ms
20,900 KB
testcase_31 AC 311 ms
21,156 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 302 ms
23,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

//dijkstraをする。Gは<to, cost>の配列。dは結果。vは始点。
void dijkstra(vector<vector<llll>> &G, vector<ll> &d, vector<ll> &pre, ll v){
    fill(ALL(d), INF / 2);
    d[v] = 0;

    priority_queue<llll, vector<llll>, greater<llll>> q;
    q.push(make_pair(0, v));
    while(!q.empty()){
        auto p = q.top(); q.pop();
        auto pv = p.second;
        if(d[pv] < p.first) continue; //p.secondへの距離が更新済みならスルー
        for (auto next : G[pv]) {
            if(d[pv] + next.second >= d[next.first]) continue;
            d[next.first] = d[pv] + next.second;
            pre[next.first] = pv;
            q.push(make_pair(d[next.first], next.first));
        }
    }
}

int main(){
    ll N, M;
    cin >> N >> M;
    vector<vector<llll>> G(N);
    map<llll, ll> m;
    vector<ll> u(M), v(M), c(M), d(M);
    for (ll i = 0; i < M; i++) {
        cin >> u[i] >> v[i] >> c[i] >> d[i];
        u[i]--; v[i]--;
        G[u[i]].push_back({v[i], c[i]});
        G[v[i]].push_back({u[i], c[i]});
        m[{u[i], v[i]}] = i;
    }
    vector<ll> dis(N), pre(N);
    dijkstra(G, dis, pre, 0);
    ll tmp = N-1;
    ll ans = dis[N-1];
    for (ll i = 0; i < N; i++) {
        G[i].clear();
    }
    vector<ll> ind;
    while(tmp != 0){
        ll ttmp = pre[tmp];
        bool ch = false;
        if(ttmp < tmp){
            swap(ttmp, tmp);
            ch = true;
        }
        ind.push_back(m[{tmp, ttmp}]);      
        if(ch == false){
            swap(ttmp, tmp);
        }
    }
    int indd = 0;
    sort(ALL(ind));
    for (int i = 0; i < M; i++) {
        if(ind[indd] == i){
            G[u[i]].push_back({v[i], d[i]});
            G[v[i]].push_back({u[i], d[i]});            
            indd++;
        }
        else{
            G[u[i]].push_back({v[i], c[i]});
            G[v[i]].push_back({u[i], c[i]});
        }
    }
    // for (auto vv : m){
    //     printf("%lld %lld %lld\n", vv.first.first, vv.first.second, vv.second);
    // }
    // for (int i = 0; i < N; i++) {
    //     cout << i << " " << pre[i] << endl;
    // }
    // for (int i = 0; i < ind.size(); i++) {
    //     cout << ind[i] << endl;
    // }

    dijkstra(G, dis, pre, N-1);
    ans += dis[0];
    printf("%lld\n", ans);

}
0