#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define vl vector<long long>
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
int color[100'001];
ll cost[100'001];
vector<pii> graph[500'001];
vector<pll> base_graph[100'001];
map<int,vector<pii>> color_cnt[100'001];
map<int,ll> color_sum[100'001];
ll dist[500'001];
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random();
    int n,m;
    cin >> n >> m;
    rep2(i,1,500'001) dist[i] = -1;
    rep(i,m)
    {
        int a,b;
        cin >> a >> b >> color[i] >> cost[i];
        base_graph[a].pb({b,i});
        base_graph[b].pb({a,i});
        color_cnt[a][color[i]].pb({b,i});
        color_cnt[b][color[i]].pb({a,i});
        color_sum[a][color[i]]+=cost[i];
        color_sum[b][color[i]]+=cost[i];
    }
    int cur_vert = n+1;
    rep2(i,1,n)
    {
        forall(e,base_graph[i])
        {
            graph[i].pb({e.ff,min(cost[e.ss],color_sum[i][color[e.ss]] - cost[e.ss])});
        }
        forall(c,color_cnt[i])
        {
            forall(e,c.ss)
            {
                graph[e.ff].pb({cur_vert,0});
                graph[cur_vert].pb({e.ff,color_sum[i][c.ff]-cost[e.ss]});
            }
            cur_vert++;
        }
    }
    priority_queue<pll> q;
    q.push({0,1});
    while(!q.empty())
    {
        pll t = q.top();
        q.pop();
        if(dist[t.ss] != -1) continue;
       // cout << -t.ff << " " << t.ss << " t\n";
        dist[t.ss] = -t.ff;
        forall(it,graph[t.ss])
        {
            q.push({t.ff-it.ss,it.ff});
        }
    }
    cout << dist[n];
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:39:31: warning: iteration 500000 invokes undefined behavior [-Waggressive-loop-optimizations]
   39 |     rep2(i,1,500'001) dist[i] = -1;
      |                       ~~~~~~~~^~~~
Main.cpp:12:38: note: within this loop
   12 | #define rep2(i,a,b) for(int i = a; i <= (b); ++i)
      |                                      ^
Main.cpp:39:5: note: in expansion of macro 'rep2'
   39 |     rep2(i,1,500'001) dist[i] = -1;
      |     ^~~~
Main.cpp:39:31: warning: 'void* __builtin_memset(void*, int, long unsigned int)' writing 4000008 bytes into a region of size 4000000 overflows the destination [-Wstringop-overflow=]
   39 |     rep2(i,1,500'001) dist[i] = -1;
      |                       ~~~~~~~~^~~~
Main.cpp:31:4: note: at offset 8 into destination object 'dist' of size 4000008
   31 | ll dist[500'001];
      |    ^~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |