#include<bits/stdc++.h>
using namespace std;
// define
#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
#define ll long long
#define ii pair <int , int>
#define iii pair <ll , ii>
#define se second
#define fi first
#define all(v) (v).begin() , (v).end()
#define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
#define bit(x,i) (((x) >> (i)) & 1LL)
#define flip(x,i) ((x) ^ (1LL << (i)))
#define ms(d,x) memset(d , x , sizeof(d))
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
#define sitingfake 1
#define orz 1
//constant
const long long mod = 1e9 + 7;
const long long linf = 4557430888798830399LL;
const long long nlinf = -4485090715960753727LL;
const int inf = 1061109567;
const int ninf = -1044266559;
const int dx[] = {0 , -1 , 0 , 1};
const int dy[] = {-1 , 0 , 1 , 0};
template<typename T> bool maximize(T &a, const T &b)
{
if(a < b) {a = b; return 1;}
return 0;
}
template<typename T> bool minimize(T &a, const T &b)
{
if(a > b) {a = b; return 1;}
return 0;
}
void Plus(ll & a ,ll b)
{
b %= mod;
a += b;
if(a < 0) a += mod;
a %= mod;
return;
}
void Mul(ll & a, ll b)
{
(a *= (b % mod)) %= mod;
return;
}
//code
const int maxn = 1e5 + 7;
map <int , ll> dist[maxn];
ll dp[maxn];
map <int , ll> sum[maxn];
int n , m;
struct edges {
int v;
ll w;
};
map <int , vector <edges>> adj[maxn];
void solve(void)
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int u , v , c , w;
cin >> u >> v >> c >> w;
adj[u][c].push_back({v , w});
adj[v][c].push_back({u , w});
dist[u][c] = dist[v][c] = linf;
sum[u][c] += w;
sum[v][c] += w;
}
priority_queue <iii ,vector<iii> , greater <iii>> pq;
ms(dp , 0x3f);
dp[1] = 0;
pq.push({0 , {1 , 0}});
while(!pq.empty())
{
int u = pq.top().se.fi;
ll cost = pq.top().fi;
int c = pq.top().se.se;
pq.pop();
if(c != 0)
{
if(cost > dist[u][c]) continue;
for(edges it : adj[u][c])
{
if(minimize(dp[it.v] , cost + sum[u][c] - it.w))
{
pq.push({dp[it.v] , {it.v , 0}});
}
}
}
else
{
if(cost > dp[u]) continue;
for(auto i : adj[u])
{
for(edges it : i.se)
{
if(minimize(dp[it.v] , cost + min(it.w , sum[u][i.fi] - it.w)))
{
pq.push({dp[it.v] , {it.v , 0}});
}
if(minimize(dist[it.v][i.fi] , cost))
{
pq.push({dist[it.v][i.fi] , {it.v , i.fi}});
}
}
}
}
}
ll ans = dp[n];
if(ans == linf) cout << "-1";
else cout << ans;
}
/**
**/
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) solve();
// execute;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:152:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:153:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
153 | freopen(task".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... |