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;
typedef long long lo;
typedef pair< lo,lo > PII;
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 200005;
const lo mod = 1000000007;
int n,m,a[li],k,flag,t,x;
lo cev,cost[li],mn1[li],mn2[li];
vector<pair<int,pair<int,int>>> v[li];
unordered_map<int,long long> mpp[li];
//~ unordered_map<int,bool> vis[li];
bool vis[li][4];
inline int in(){
int x;
scanf("%lld",&x);
return x;
}
inline void sp(){
priority_queue<pair<lo,pair<int,int>>> pq;
pq.push({0,{0,1}});
for(int i=1;i<=max(n,m);i++){
cost[i]=20000000000000001;
mn1[i]=20000000000000001;
mn2[i]=20000000000000001;
}
cev=20000000000000001;
while(pq.size()){
lo co=-pq.top().fi;
int yes=pq.top().se.fi;
int node=pq.top().se.se;
pq.pop();
if(vis[node][yes])continue;
vis[node][yes]=1;
//~ cout<<node<<" :: "<<co<<endl;
//~ cout<<co<<endl;
cost[node]=min(cost[node],co);
if(node==n){cev=min(cev,co);continue;}
for(auto go:v[node]){
if(vis[go.fi][0]==0)pq.push({-co-mpp[node][go.se.fi]+go.se.se,{0,go.fi}});
if(vis[go.fi][0]==0)pq.push({-co-go.se.se,{0,go.fi}});
mn1[go.se.fi]=min(mn1[go.se.fi],cost[go.fi]);
//~ if(mn2[go.se.fi]<mn1[go.se.fi])swap(mn2[go.se.fi],mn1[go.se.fi]);
}
for(auto go:v[node]){
if(cost[go.fi]>mn1[go.se.fi]+mpp[node][go.se.fi]-go.se.se){
//~ cost[go.fi]=mn1[go.se.fi]+mpp[node][go.se.fi]-go.se.se;
//~ vis[go.fi]=0;
//~ cout<<go.fi<<" :: "<<cost[go.fi]<<" :: "<<node<<" :: "<<go.se.se<<" :: "<<mn1[go.se.fi]<<" :: "<<go.se.fi<<endl;
pq.push({-(mn1[go.se.fi]+mpp[node][go.se.fi]-go.se.se),{1,go.fi}});
}
}
for(auto go:v[node]){
mn1[go.se.fi]=20000000000000001;
mn2[go.se.fi]=20000000000000001;
}
}
if(cev>=20000000000000001)cev=-1;
printf("%lld\n",cev);
}
int32_t main(void){
n=in(),m=in();
for(int i=1;i<=m;i++){
int x=in(),y=in(),c=in(),p=in();
v[x].pb({y,{c,p}});
v[y].pb({x,{c,p}});
mpp[x][c]+=p;
mpp[y][c]+=p;
}
sp();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'long long int in()':
Main.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%lld",&x);
| ~~~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |