This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// GH SAI KEERTAN
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define rep(Begin,End) for (Begin=0;Begin<(End);Begin++)
#define all(x) x.begin(), x.end()
#define all1(x) x.rbegin(), x.rend()
#define sz(x) (int32_t)(x.size())
const int32_t N = 1e5+4, mod =1e9+7;
const int64_t N1=1e18;
const double eps=1e-6,cmp=1e-3;
void solve(){
int n,m;
cin>>n>>m;
int gh=n;
vector<vector<pair<int,int>>> adj(n+2*m);
map<pair<int,int>,int> mid;
vector<int> sum(n+2*m);
int cur=n;
auto id=[&](int v,int c)->int{
auto it=mid.insert({{v,c},n});
n+=it.second;
return it.first->second;
};
for (int i=1,u,v,c,p;i<=m;i++){
cin>>u>>v>>c>>p;
u--,v--;
adj[u].emplace_back(v,p);
adj[v].emplace_back(u,p);
int uid=id(u,c),vid=id(v,c);
sum[uid]+=p;
sum[vid]+=p;
adj[uid].emplace_back(v,-p);
adj[vid].emplace_back(u,-p);
adj[u].emplace_back(vid,0);
adj[v].emplace_back(uid,0);
}
for (auto it:mid){
int par=it.first.first;
adj[par].emplace_back(it.second,0);
for (pair<int,int>& jt:adj[it.second]){
jt.second+=sum[it.second];
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
vector<int> dis(n,1e18);
dis[0]=0;
q.emplace(0,0);
while(!q.empty()){
int w,u;
tie(w,u)=q.top();
q.pop();
if (dis[u]!=w){continue;}
for (pair<int,int>& it:adj[u]){
if (dis[it.first]>it.second+w){
dis[it.first]=w+it.second;
q.emplace(dis[it.first],it.first);
}
}
}
cout<<(dis[gh-1]==1e18?-1:dis[gh-1]);
}
int32_t main(){
ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
int tq = 1;
//cin>>tq;
for (;tq--;){solve();}
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:21:9: warning: unused variable 'cur' [-Wunused-variable]
21 | int cur=n;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |