#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 <int , 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];
map <int , ll> sum[maxn];
int n , m;
struct edges {
int v;
int c;
ll w;
};
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].push_back({v , c , w});
adj[v].push_back({u , c , w});
dist[u][c] = dist[v][c] = linf;
sum[u][c] += w;
sum[v][c] += w;
}
for(int i = 1; i <= n; i++)
{
dist[i][0] = linf;
}
priority_queue <iii ,vector<iii> , greater <iii>> pq;
dist[1][0] = 0;
pq.push({0 , {1 , 0}});
//cout << 1 << endl;;
//int cnt = 0;
while(!pq.empty())
{
int u = pq.top().se.fi;
ll cost = pq.top().fi;
int c = pq.top().se.se;
pq.pop();
//cout << u << " " << c << " " << cost << endl;
// ++cnt;
// if(cnt == 100) break;
if(cost > dist[u][c]) continue;
if(c != 0)
{
for(edges it : adj[u])
{
if(it.c == c && minimize(dist[it.v][0] , cost + sum[u][it.c] - it.w))
{
pq.push({dist[it.v][0] , {it.v , 0}});
}
}
}
else
{
for(edges it : adj[u])
{
if(minimize(dist[it.v][0] , cost + min(it.w , sum[u][it.c] - it.w)))
{
pq.push({dist[it.v][0] , {it.v , 0}});
}
if(minimize(dist[it.v][it.c] , cost))
{
pq.push({dist[it.v][it.c] , {it.v , it.c}});
}
}
}
}
ll ans = dist[n][0];
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 'void solve()':
Main.cpp:117:28: warning: narrowing conversion of 'dist[it.edges::v].std::map<int, long long int>::operator[](0)' from 'std::map<int, long long int>::mapped_type' {aka 'long long int'} to 'int' [-Wnarrowing]
117 | pq.push({dist[it.v][0] , {it.v , 0}});
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:127:28: warning: narrowing conversion of 'dist[it.edges::v].std::map<int, long long int>::operator[](0)' from 'std::map<int, long long int>::mapped_type' {aka 'long long int'} to 'int' [-Wnarrowing]
127 | pq.push({dist[it.v][0] , {it.v , 0}});
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:131:28: warning: narrowing conversion of 'dist[it.edges::v].std::map<int, long long int>::operator[](it.edges::c)' from 'std::map<int, long long int>::mapped_type' {aka 'long long int'} to 'int' [-Wnarrowing]
131 | pq.push({dist[it.v][it.c] , {it.v , it.c}});
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:155:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:156:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | 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... |