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>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define ff(i,s,f) for(int i=s;i<f;i++)
#define fb(i,s,f) for(int i=(s)-1;i>=f;i--)
#define ffi(i,s,f) for(int i=s;i<=f;i++)
#define fbi(i,s,f) for(int i=s;i>=f;i--)
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,int) sort(a.begin(),a.end(),greater<int>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define ub(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define vstart auto startt=chrono::system_clock::now()
#define vend auto endd=chrono::system_clock::now()
#define vvreme chrono::duration<double> vremee=endd-startt
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef string str;
const int maxn=2e5+10;
struct edge{
int c,p,to,koji,dist=-1;
edge(int _c,int _p,int _to,int _koji){
c=_c; p=_p; to=_to; koji=_koji;
}
bool operator <(edge b){
return vector<int>({this->c,this->p})<vector<int>({b.c,b.p});
}
};
int n;
vector<edge> g[maxn];
map<int,int> sum[maxn];
int dijkstra(int poc){
vector<int> dist(maxn,-1);
priority_queue<pair<int,pii> ,vector<pair<int,pii> >, greater<pair<int,pii> > > pq;
pq.push({0,{poc,-1}});
dist[poc]=0;
while(!pq.empty()){
auto tren=pq.top();
pq.pop();
if(tren.yy.yy==-1){
if(tren.xx!=dist[tren.yy.xx]) continue;
}else{
if(tren.xx!=g[tren.yy.xx][tren.yy.yy].dist) continue;
}
if(tren.yy.yy==-1){
for(auto i:g[tren.yy.xx]){
///normalan
if(dist[i.to]>min(tren.xx+i.p,tren.xx+sum[tren.yy.xx][i.c]-i.p) || dist[i.to]==-1){
dist[i.to]=min(tren.xx+i.p,tren.xx+sum[tren.yy.xx][i.c]-i.p);
pq.push({dist[i.to],{i.to,-1}});
}
///ujeban
if(g[i.to][i.koji].dist>tren.xx+i.p || g[i.to][i.koji].dist==-1){
g[i.to][i.koji].dist=tren.xx+i.p;
pq.push({g[i.to][i.koji].dist,{i.to,i.koji}});
}
}
}else{
int naj=lower_bound(g[tren.yy.xx].begin(),g[tren.yy.xx].end(),edge(g[tren.yy.xx][tren.yy.yy].c+1,-1,-1,-1))-g[tren.yy.xx].begin()-1;
if(naj==tren.yy.yy) naj--;
if(naj<0 || g[tren.yy.xx][naj].c!=g[tren.yy.xx][tren.yy.yy].c) continue;
auto &grana=g[tren.yy.xx][naj];
int c=g[tren.yy.xx][tren.yy.yy].c;
if(g[grana.to][grana.koji].dist>tren.xx+sum[tren.yy.xx][c]-grana.p-g[tren.yy.xx][tren.yy.yy].p || g[grana.to][grana.koji].dist==-1){
g[grana.to][grana.koji].dist=tren.xx+sum[tren.yy.xx][c]-grana.p-g[tren.yy.xx][tren.yy.yy].p;
pq.push({g[grana.to][grana.koji].dist,{grana.to,grana.koji}});
}
if(dist[grana.to]>g[grana.to][grana.koji].dist || dist[grana.to]==-1){
dist[grana.to]=g[grana.to][grana.koji].dist;
pq.push({dist[grana.to],{grana.to,-1}});
}
}
}
return dist[n];
}
void solve(){
int m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b,c,p;
cin>>a>>b>>c>>p;
g[a].pb(edge(c,p,b,-1));
sum[a][c]+=p;
g[b].pb(edge(c,p,a,-1));
sum[b][c]+=p;
}
for(int i=1;i<=n;i++) sort(g[i].begin(),g[i].end());
for(int i=1;i<=n;i++){
for(auto &j:g[i]){
j.koji=lower_bound(g[j.to].begin(),g[j.to].end(),edge(j.c,j.p,i,-1))-g[j.to].begin();
}
}
cout<<dijkstra(1);
}
int main(){
ios;
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |