This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Ai_2007
//Make the impossible possible
#include<bits/stdc++.h>
#define ll long long
#define fo(i,m,n) for(int i=m; i<=n; i++)
#define fod(i,m,n) for(int i=m; i>=n; i--)
#define fi first
#define se second
#define Nmax 1000001
#define pb push_back
#define pii pair<int,int>
#define getbit(i,j) ((i>>j)&1)
#define onbit(i,j) (i=(1<<j))
#define offbit(i,j) (i^=(1<<j))
#define pli pair<long long,int>
using namespace std;
const int N=1e5+5;
int n,m,dem[2*N],val[N];
ll dis[N],sum[2*N];
bool vst[N];
struct dl
{
int u,v,c,p;
};
vector<pair<int,pii>> adj[N];
void dijkstra()
{
fo(i,1,n) {
dis[i]=1e18;
vst[i]=0;
}
dis[1]=0;
priority_queue<pli,vector<pli>,greater<pli>> q;
q.push({0,1});
while(!q.empty()){
int u=q.top().se;
ll kc=q.top().fi;
// cout<<u<<" ";
q.pop();
if(vst[u]) continue;
vst[u]=1;
int cnt=0;
for(auto it:adj[u]){
int mau=it.se.fi;
dem[mau]=0;
sum[mau]=0;
// if(mau==col)sum+=cost;
}
for(auto it:adj[u]){
int mau=it.se.fi;
int cos=it.se.se;
dem[mau]++;
sum[mau]+=cos;
if(dem[mau]==2) cnt++;
// if(mau==col)sum+=cost;
}
// if(cnt==m) continue;
for(auto it:adj[u]){
int mau=it.se.fi;
// if(mau!=col) continue;
int cost=it.se.se;
int v=it.fi;
// if(u==2) cout<<mau<<" ";
// if(dem[mau]==1) cost=0;
if(dem[mau]==1){
if(dis[v] > dis[u]){
dis[v]=dis[u];
q.push({dis[v],v});
}
}
else{
ll cos=1e18;
if(cnt<m) cos=cost;
if(dis[v] > dis[u] + min(cos,sum[mau] - cost)){
dis[v]=dis[u] + min(cos,sum[mau] - cost);
q.push({dis[v],v});
}
}
}
}
// if(dis[)
}
void solve()
{
cin>>n>>m;
fo(i,1,m){
int u,v,c,p;
cin>>u>>v>>c>>p;
adj[u].pb({v,{c,p}});
adj[v].pb({u,{c,p}});
// dd[c]=3;
}
// ll ans=1e18;
dijkstra();
// cout<<dis[3];
if(dis[n]==1e18) cout<<-1;
else cout<<dis[n];
}
main()
{
// freopen("robot1.inp","r",stdin);
// freopen("robot1.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void dijkstra()':
Main.cpp:37:12: warning: unused variable 'kc' [-Wunused-variable]
37 | ll kc=q.top().fi;
| ^~
Main.cpp: At global scope:
Main.cpp:99:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
99 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |