제출 #634697

#제출 시각아이디문제언어결과실행 시간메모리
634697uroskRobot (JOI21_ho_t4)C++14
0 / 100
149 ms87896 KiB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);

using namespace std;
using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
    x+=y;
    if(x<0){
        x%=mod;
        x+=mod;
    }else{
        if(x>=mod) x%=mod;
    }
    return x;
}
ll mul(ll a,ll b){
	ll ans = (a*b)%mod;
	if(ans<0) ans+=mod;
	return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
    return uniform_int_distribution<ll>(l,r)(rng);
}
*/
#define maxn 100005
ll n,m;
map<int,ll> sum[maxn];
map<int,ll> mx[maxn];
vector<bool> ima[maxn];
ll d[maxn];
bool vis[maxn],vis2[maxn];
map<int,ll> d2[maxn];
struct nxt{
    int fi,p,c; ll d;
};
bool cmp(nxt a,nxt b){
    if(a.d!=b.d) return a.d>b.d;
    return a.p>b.p;
}
map<int,vector<nxt> > g[maxn];
queue<nxt> pq;
int main(){
	daj_mi_malo_vremena
    cin >> n >> m;
    for(ll i = 1;i<=m;i++){
        int x,y,c,p;
        cin >> x >> y >> c >> p;
        sum[x][c]+=p; sum[y][c]+=p;
        mx[x][c] = max(mx[x][c],(ll)p);
        mx[y][c] = max(mx[y][c],(ll)p);
        g[x][c].pb({y,p,c,0});
        g[y][c].pb({x,p,c,0});
    }
    for(ll i = 1;i<=n;i++) ima[i].resize(sz(g[i])+1);
    fill(d,d+maxn,llinf);
    d[1] = 0;
    pq.push({1,0,0,0});
    while(sz(pq)){
        nxt e = pq.front(); pq.pop();
        ll u = e.fi;
        bool f = !!e.c;
        if(!f&&e.d!=d[u]) continue;
        if(f&&e.d!=d2[u][e.c]) continue;
        else vis2[u] = 1;
        if(f){
            for(nxt a : g[u][e.c]){
                int s = a.fi, c = a.c, p = a.p;
                ll w = sum[u][c]-p;
                if(e.d+w<d[s]){
                    d[s] = e.d + w;
                    pq.push({s,0,0,d[s]});
                }
            }
            continue;
        }
        for(auto &it : g[u]){
            for(nxt a : it.sc){
                int s = a.fi, c = a.c, p = a.p;
                ll w = (ll)(min((ll)p,sum[u][c]-p));
                if(e.d+w<d[s]){
                    d[s] = e.d + w;
                    pq.push({s,0,0,d[s]});
                }
                if(ima[s][c]==0||e.d<d2[s][c]){
                    d2[s][c] = e.d;
                    ima[s][c] = 1;
                    pq.push({s,p,c,e.d});
                }
            }
        }
    }
    ll ans = d[n];
    if(ans==llinf) ans = -1;
    cout<<ans<<endl;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...