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 sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
mt19937 rng(time(0));
int n,m,a,b,c,d,cnt;
ll tot[500005],dist[500005];
map<ii,int> mp;
vector<iii> AL[500005];
priority_queue<li,vector<li>,greater<li>> pq;
void add(int a,int b,int c,int d){
if(!mp.count({a,c})){
AL[a].push_back({cnt,0,0});
mp[{a,c}]=cnt++;
}
int z=mp[{a,c}];
AL[z].push_back({b,c,d});
tot[z]+=d;
}
void up(int v,ll d){
if(dist[v]<=d)return;
dist[v]=d;
pq.push({dist[v],v});
}
int main(){
sf("%d%d",&n,&m);
cnt=n;
for(int i=0;i<m;++i){
sf("%d%d%d%d",&a,&b,&c,&d);
--a;--b;
add(a,b,c,d);
add(b,a,c,d);
}
for(int i=0;i<cnt;++i)dist[i]=LINF;
dist[0]=0;
pq.push({dist[0],0});
while(!pq.empty()){
ll d;int u;tie(d,u)=pq.top();pq.pop();
if(d>dist[u])continue;
if(u<n){
for(iii t:AL[u]){
int v,c,w;tie(v,c,w)=t;
for(iii t2:AL[v]){
int v2,c2,w2;tie(v2,c2,w2)=t2;
up(v2,d+min((ll)w2,tot[v]-w2));
up(mp[{v2,c2}],d);
}
}
}
else{
for(iii t:AL[u]){
int v,c,w;tie(v,c,w)=t;
up(v,d+tot[u]-w);
}
}
}
if(dist[n-1]==LINF)dist[n-1]=-1;
pf("%lld\n",dist[n-1]);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:48:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | sf("%d%d",&n,&m);
| ^
Main.cpp:51:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | sf("%d%d%d%d",&a,&b,&c,&d);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |