This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define rep(n) rep1(i,n)
#define rep1(i,n) rep2(i,0,n)
#define rep2(i,a,b) for(int i=a; i<(b); ++i)
#define rep3(i,a,b) for(int i=a; i>=(b); --i)
#define pii pair<int,int>
#define pb push_back
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define inf 1000'000'000'000'000'000
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ": " << #__VA_ARGS__ << "- ",_do(__VA_ARGS__)
template<typename T> void _do(T x){cerr << x << endl;}
template<typename T, typename ... S> void _do(T x, S ...y){cerr << x << ", ";_do(y...);}
#else
#define bug(...) 49
#endif
const int maxn=100005;
int n,m,dis[maxn];
vector<array<int,3>> adj[maxn];
vector<pii> adj2[maxn];
bool vis[maxn];
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> n >> m;
rep(m){
int u,v,a,b;
cin >> u >> v >> a >> b;
u--,v--;
adj[u].pb({v,a,b}),adj[v].pb({u,a,b});
}
rep(n){
map<int,vector<pii>> mm;
map<int,int> mm2;
for(auto& arr: adj[i]) mm[arr[1]].pb({arr[2],arr[0]}),mm2[arr[1]]+=arr[2];
for(auto& [c,vec]: mm){
sort(all(vec));
reverse(all(vec));
if(sz(vec)==1){
adj2[i].pb({vec[0].second,0});
continue;
}
for(auto p: vec) adj2[i].pb({p.second,min(p.first,mm2[c]-p.first)});
pii p1=vec.back();
for(auto p: vec) if(p!=p1) adj2[p.second].pb({p1.second,mm2[c]-p1.first});
pii p2=vec[sz(vec)-2];
for(auto p: vec) if(p!=p2) adj2[p.second].pb({p2.second,mm2[c]-p2.first});
}
}
priority_queue<pii,vector<pii>,greater<pii>> pq;
rep2(i,1,n) dis[i]=inf;
pq.push({0,0});
while(!pq.empty()){
auto [d,u]=pq.top();
pq.pop();
if(vis[u]) continue;
vis[u]=1;
for(auto [v,w]: adj2[u]){
if(d+w<dis[v]){
dis[v]=d+w;
pq.push({dis[v],v});
}
}
}
cout << (dis[n-1]==inf?-1:dis[n-1]) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |