結果

問題 No.1301 Strange Graph Shortest Path
ユーザー auaua
提出日時 2020-11-28 16:35:21
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,254 bytes
コンパイル時間 1,999 ms
コンパイル使用メモリ 183,768 KB
実行使用メモリ 21,588 KB
最終ジャッジ日時 2024-09-12 23:13:02
合計ジャッジ時間 10,791 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=998244353;
const ll mod=MOD;
const ll MAX=200010;
const double PI=acos(-1.0);

//dijkstra(O(ElogV))
//0-indexed
void dijkstra(ll n,ll s,vector<vector<pll> >&G,vector<ll> &d,vector<ll>&prev){
    priority_queue<pll,vector<pll>,greater<pll> >pq;
    rep(i,n){
        d[i]=inf;
    }
    d[s]=0;
    pq.push(mp(0,s));
    while(!pq.empty()){
        pll k=pq.top();pq.pop();
        ll v=k.second;
        if(d[v]<k.first)continue;
        for(auto e:G[v]){
            if(d[e.first]>d[v]+e.second){
                d[e.first]=d[v]+e.second;
                prev[e.fi]=v;
                pq.push(mp(d[e.first],e.first));
            }
        }
    }
}
signed main(){
    ll n,m;cin>>n>>m;
    vector<ll>u(m),v(m),c(m),d(m);
    rep(i,m){
        cin>>u[i]>>v[i]>>c[i]>>d[i];
        u[i]--;v[i]--;
    }
    
    ll len1,len2;
    vector<ll>prev(n);
    set<pll>st;
    {
        vector<vector<pll> >G(n);
        vector<ll>dist(n);
        rep(i,m){
            G[u[i]].pb(mp(v[i],c[i]));
            G[v[i]].pb(mp(u[i],c[i]));
        }
        dijkstra(n,0,G,dist,prev);
        len1=dist[n-1];
        ll now=n-1;
        while(now!=0){
            st.insert(mp(prev[now],now));
            st.insert(mp(now,prev[now]));
            now=prev[now];
        }
    }
    {
        vector<vector<pll> >G(n);
        vector<ll>dist(n);
        rep(i,m){
            if(!st.count(mp(u[i],v[i]))){
                G[u[i]].pb(mp(v[i],c[i]));
                G[v[i]].pb(mp(u[i],c[i]));
            }else{
                G[u[i]].pb(mp(v[i],d[i]));
                G[v[i]].pb(mp(u[i],d[i]));
            }
        }
        dijkstra(n,0,G,dist,prev);
        len2=dist[n-1];
    }
    cout<<len2+len1<<endl;
}
0