#include<bits/stdc++.h>
using ll=long long;
const int N=1e6+5;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define F(i,a,b,c) for(int i=a;i<=b;i+=c)
#define fo(i,a,b,c) for(int i=a;i>=b;i-=c)
#define pb push_back
const int MOD=1e9+7;
#define el cout<<'\n'
#define all(x) x.begin(),x.end()
#define name "robot1"
using namespace std;
ll n,m;
struct tri
{
ll fi,se,th;
};
vector<tri> g[N];
void init()
{
cin>>n>>m;
F(i,1,m,1)
{
ll a,b,c,p;cin>>a>>b>>c>>p;
g[a].pb({b,c,p});
g[b].pb({a,c,p});
}
}
ll dp[N];
ll s[N],mi[N];
const ll INF=1e17;
void process()
{
priority_queue<pll,vector<pll>,greater<pll>> h;
F(i,2,n,1) dp[i]=INF;
h.push({0,1});
F(i,1,m,1) mi[i]=INF;
while(h.size())
{
auto [d,u]=h.top();h.pop();
if(d>dp[u]) continue;
for(auto [v,c,p]:g[u])
{
s[c]+=p;
mi[c]=min(mi[c],dp[u]);
}
for(auto [v,c,p]:g[u])
{
ll w=min(p,s[c]-p);
if(dp[v]>d+w)
h.push({dp[v]=d+w,v});
if(dp[v]>mi[c]+s[c]-w)
h.push({dp[v]=mi[c]+s[c]-w,v});
}
for(auto [v,c,p]:g[u])
{
s[c]=0;
mi[c]=INF;
}
}
cout<<(dp[n]==INF?-1:dp[n]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// if(fopen(name".inp","r"))
// {
// freopen(name".inp","r",stdin);
// freopen(name".out","w",stdout);
// }
//input:
init();
clock_t begin = clock();
//code:
process();
clock_t end = clock();
cerr<<endl<<"Time run: "<<(float)(end-begin)/CLOCKS_PER_SEC<<" s"<<endl;
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |