#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];
void process()
{
priority_queue<pll,vector<pll>,greater<pll>> h;
F(i,2,n,1) dp[i]=LLONG_MAX;
h.push({0,1});
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;
if(mi[c]>dp[v]) mi[c]=dp[v];
}
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]=LLONG_MAX;
}
}
cout<<(dp[n]==LLONG_MAX?-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;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |