#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;
ll dist[maxn][2];
ll sum[2 * maxn];
int n , m;
struct edges
{
int v;
int from;
int c;
int 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;
int szu = adj[u].size();
int szv = adj[v].size();
adj[u].push_back({v , szv , c , w});
adj[v].push_back({u , szu , c , w});
}
ms(dist , 0x3f);
dist[1][0] = dist[1][1] = 0;
priority_queue <pair <ll , iii> ,vector<pair <ll , iii>> , greater <pair <ll , iii>>> pq;
pq.push({0 , {1 , {adj[1].size() , 0}}});
while(!pq.empty())
{
int u = pq.top().se.fi;
ll cost = pq.top().fi;
int from = pq.top().se.se.fi;
int t = pq.top().se.se.se;
pq.pop();
if(cost > dist[u][t]) continue;
for(int i = 0; i < adj[u].size(); i++)
{
if(i == from && t == 1) continue;
sum[adj[u][i].c] += adj[u][i].w;
}
for(int i = 0; i < adj[u].size(); i++)
{
if(i == from) continue;
int v = adj[u][i].v;
int to = adj[u][i].from;
int color = adj[u][i].c;
ll w = adj[u][i].w;
if(minimize(dist[v][1] , dist[u][t] + w))
{
pq.push({dist[v][1] , {v , {to , 1}}});
}
if(minimize(dist[v][0] , dist[u][t] + sum[color] - w))
{
pq.push({dist[v][0] , {v , {to , 0}}});
}
}
for(int i = 0; i < adj[u].size(); i++)
{
if(i == from && t == 1) continue;
sum[adj[u][i].c] -= adj[u][i].w;
}
}
ll ans = linf;
ans = min({ans , dist[n][1] , 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;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:154:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
154 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
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".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... |