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 int long long
int n;int m;
int ar[200005];int br[200005];int clr[200005];int wr[200005];
vector<int>e[200005];
int dis[200005];
int vis[200005];
map<int,int>mpsm[500005];
int mpf[500005];
map<int,vector<pair<int,int>>>mpe[200005];
map<int,int>mxe[200005];
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>ar[i]>>br[i]>>clr[i]>>wr[i];
e[ar[i]].push_back(i);
e[br[i]].push_back(i);
mpe[ar[i]][clr[i]].push_back({wr[i],i});
mpe[br[i]][clr[i]].push_back({wr[i],i});
mpsm[ar[i]][clr[i]]+=wr[i];
mpsm[br[i]][clr[i]]+=wr[i];
}
for(int i=1;i<=n;i++){
dis[i]=1e18;
vis[i]=0;
for(auto it=mpsm[i].begin();it!=mpsm[i].end();it++){
sort(mpe[i][(*it).first].begin(),mpe[i][(*it).first].end());
reverse(mpe[i][(*it).first].begin(),mpe[i][(*it).first].end());
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
dis[1]=0;
pq.push({0,1});
while(pq.size()){
int nw=pq.top().second;
pq.pop();
if(vis[nw]){continue;}
vis[nw]=1;
for(int i=0;i<e[nw].size();i++){
int id=e[nw][i];
int v=ar[id]^br[id]^nw;
//if(vis[v]){continue;}
int w=wr[id];
// if(mpf[clr[id]]==1){
// w=0;
//}
if(dis[v]>dis[nw]+min(w,mpsm[nw][clr[id]]-w) ){
dis[v]=dis[nw]+min(w,mpsm[nw][clr[id]]-w);
pq.push({dis[v],v});
}
int ow=dis[nw]+w;int cnt=3;
for(int j=0;j<mpe[v][clr[id]].size();j++){
// cout<<nw<<" "<<id<<endl;
cnt--;
if(cnt<0){break;}
if(id!=mpe[v][clr[id]][j].second){
int neid=mpe[v][clr[id]][j].second;
int vv=v^ar[neid]^br[neid];
w=mpsm[v][clr[id]]-wr[id]-wr[neid];
// cout<<nw<<" "<<v<<" "<<vv<<" "<<neid<<" "<<ow<<" "<<w<<endl;
if(dis[vv]>ow+w){
dis[vv]=ow+w;
pq.push({dis[vv],vv});
}
}
}
}
}
int ans=dis[n];
if(dis[n]>=1e16){
ans=-1;
}
cout<<ans<<endl;
}
/*
6 5
1 2 1 100000
1 3 1 1
2 6 1 1000
2 4 1 1
2 5 1 1
*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:45:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
Main.cpp:58:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int j=0;j<mpe[v][clr[id]].size();j++){
| ~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |