#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1000000007;
const ll root = 3;
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 100001;
int n, m;
ll dist[mxN];
map<int, ll> trans[mxN];
map<int, ll> fdist[mxN];
vector<array<int, 3>> adj[mxN];
void solve() {
cin>>n>>m;
owo(i, 0, m) {
int a, b, c, d;
cin>>a>>b>>c>>d;
a--; b--;
adj[a].senpai({b, c, d});
adj[b].senpai({a, c, d});
trans[a][c]+=d;
trans[b][c]+=d;
fdist[a][c] = INFLL;
fdist[b][c] = INFLL;
}
memset(dist, INFLL, sizeof(dist));
dist[0] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.push({0, 0});
while(!pq.empty()) {
auto[dx, u] = pq.top();
//cout<<dx<<" "<<u<<" "<<dist[u]<<endl;
pq.pop();
if(dx!=dist[u])continue;
for(auto v: adj[u]) {
//transition changing all but this edge
ll cost = min(dx, fdist[u][v[1]]) + trans[u][v[1]] - v[2];
cost = min(cost, dx + v[2]);
//cout<<u<<" "<<v[0]<<" "<<dx<<" "<<v[2]<<" "<<cost<<"\n";
if(cost < dist[v[0]]) {
dist[v[0]] = cost;
pq.push({cost, v[0]});
}
if(dx < fdist[v[0]][v[1]])fdist[v[0]][v[1]] = dx;
}
}
cout<<(dist[n-1]==INFLL ? -1 : dist[n-1])<<"\n";
}
int main() {
//freopen("file.in", "r", stdin);
//freopen("file.out", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
cin.tie(0)->sync_with_stdio(0);
int T = 1;
for(int tc = 1; tc <= T; tc++) {
solve();
}
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:41:18: warning: overflow in conversion from 'll' {aka 'long long int'} to 'int' changes value from '4557430888798830399' to '1061109567' [-Woverflow]
41 | memset(dist, INFLL, sizeof(dist));
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12748 KB |
Output is correct |
2 |
Correct |
9 ms |
12828 KB |
Output is correct |
3 |
Correct |
9 ms |
12768 KB |
Output is correct |
4 |
Correct |
10 ms |
12832 KB |
Output is correct |
5 |
Correct |
9 ms |
12832 KB |
Output is correct |
6 |
Correct |
10 ms |
12828 KB |
Output is correct |
7 |
Correct |
10 ms |
12940 KB |
Output is correct |
8 |
Correct |
9 ms |
12876 KB |
Output is correct |
9 |
Correct |
12 ms |
13228 KB |
Output is correct |
10 |
Correct |
12 ms |
13196 KB |
Output is correct |
11 |
Correct |
11 ms |
12968 KB |
Output is correct |
12 |
Correct |
10 ms |
13004 KB |
Output is correct |
13 |
Correct |
11 ms |
13004 KB |
Output is correct |
14 |
Correct |
10 ms |
13004 KB |
Output is correct |
15 |
Correct |
10 ms |
13004 KB |
Output is correct |
16 |
Correct |
10 ms |
13100 KB |
Output is correct |
17 |
Correct |
10 ms |
13080 KB |
Output is correct |
18 |
Correct |
10 ms |
13004 KB |
Output is correct |
19 |
Correct |
10 ms |
12972 KB |
Output is correct |
20 |
Correct |
11 ms |
12972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
26000 KB |
Output is correct |
2 |
Correct |
70 ms |
19652 KB |
Output is correct |
3 |
Correct |
177 ms |
25716 KB |
Output is correct |
4 |
Correct |
96 ms |
21764 KB |
Output is correct |
5 |
Correct |
898 ms |
55552 KB |
Output is correct |
6 |
Correct |
700 ms |
49108 KB |
Output is correct |
7 |
Correct |
307 ms |
37832 KB |
Output is correct |
8 |
Correct |
369 ms |
37876 KB |
Output is correct |
9 |
Correct |
364 ms |
37820 KB |
Output is correct |
10 |
Correct |
287 ms |
36156 KB |
Output is correct |
11 |
Correct |
153 ms |
28020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12748 KB |
Output is correct |
2 |
Correct |
9 ms |
12828 KB |
Output is correct |
3 |
Correct |
9 ms |
12768 KB |
Output is correct |
4 |
Correct |
10 ms |
12832 KB |
Output is correct |
5 |
Correct |
9 ms |
12832 KB |
Output is correct |
6 |
Correct |
10 ms |
12828 KB |
Output is correct |
7 |
Correct |
10 ms |
12940 KB |
Output is correct |
8 |
Correct |
9 ms |
12876 KB |
Output is correct |
9 |
Correct |
12 ms |
13228 KB |
Output is correct |
10 |
Correct |
12 ms |
13196 KB |
Output is correct |
11 |
Correct |
11 ms |
12968 KB |
Output is correct |
12 |
Correct |
10 ms |
13004 KB |
Output is correct |
13 |
Correct |
11 ms |
13004 KB |
Output is correct |
14 |
Correct |
10 ms |
13004 KB |
Output is correct |
15 |
Correct |
10 ms |
13004 KB |
Output is correct |
16 |
Correct |
10 ms |
13100 KB |
Output is correct |
17 |
Correct |
10 ms |
13080 KB |
Output is correct |
18 |
Correct |
10 ms |
13004 KB |
Output is correct |
19 |
Correct |
10 ms |
12972 KB |
Output is correct |
20 |
Correct |
11 ms |
12972 KB |
Output is correct |
21 |
Correct |
167 ms |
26000 KB |
Output is correct |
22 |
Correct |
70 ms |
19652 KB |
Output is correct |
23 |
Correct |
177 ms |
25716 KB |
Output is correct |
24 |
Correct |
96 ms |
21764 KB |
Output is correct |
25 |
Correct |
898 ms |
55552 KB |
Output is correct |
26 |
Correct |
700 ms |
49108 KB |
Output is correct |
27 |
Correct |
307 ms |
37832 KB |
Output is correct |
28 |
Correct |
369 ms |
37876 KB |
Output is correct |
29 |
Correct |
364 ms |
37820 KB |
Output is correct |
30 |
Correct |
287 ms |
36156 KB |
Output is correct |
31 |
Correct |
153 ms |
28020 KB |
Output is correct |
32 |
Correct |
141 ms |
23620 KB |
Output is correct |
33 |
Correct |
130 ms |
24004 KB |
Output is correct |
34 |
Correct |
326 ms |
37072 KB |
Output is correct |
35 |
Correct |
241 ms |
31424 KB |
Output is correct |
36 |
Correct |
262 ms |
32504 KB |
Output is correct |
37 |
Correct |
275 ms |
35124 KB |
Output is correct |
38 |
Correct |
288 ms |
37600 KB |
Output is correct |
39 |
Correct |
156 ms |
26400 KB |
Output is correct |
40 |
Correct |
368 ms |
39084 KB |
Output is correct |
41 |
Correct |
378 ms |
39400 KB |
Output is correct |
42 |
Correct |
426 ms |
45704 KB |
Output is correct |
43 |
Correct |
189 ms |
29536 KB |
Output is correct |
44 |
Correct |
372 ms |
35024 KB |
Output is correct |
45 |
Correct |
300 ms |
35652 KB |
Output is correct |
46 |
Correct |
272 ms |
35160 KB |
Output is correct |
47 |
Correct |
280 ms |
34604 KB |
Output is correct |
48 |
Correct |
882 ms |
57848 KB |
Output is correct |