//**********************
//
//**********************
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define add(a,b) a=(a+b)%MOD
#define BIT(mask,i) ((mask >> i) & 1ll )
#define OFF(mask,i) (mask & (~(1ll << i)))
#define ON(mask,i) (mask | (1ll << i))
#define endl '\n'
#define DAO(mask,i) (mask ^ (1ll << i))
#define add(a,b) a=(a+b)%MOD
#define m_p make_pair
#define m_t make_tuple
#define built(mask) __builtin_popcountll(mask)
#define all(x) x.begin(),x.end()
#define XD cout << '\n'
#define ii pair <int,int>
#define iii pair<int,pair<int, int> >
#define TASK "robot1"
using namespace std;
const long long oo = 1e18;
const int N = 6e5+5;
const int MOD = 1e9+7;
vector< ii > a[N];
map<int , int> mp[N];
vector< iii> g[N];
int dist[N];
int sum[N];
bool vis[N];
void dij(int x)
{
FOR(i,1,N) dist[i] = oo;
priority_queue< pair < int,int > , vector < pair < int ,int > > , greater < pair < int , int > > > q;
dist[x] = 0;
q.push(m_p(0, x));
while (!q.empty())
{
auto p1 = q.top();
int W = p1.fi;
int x = p1.se;
q.pop();
if (vis[x])
continue;
vis[x] = 1;
for (auto p2 : a[x])
{
int y = p2.fi;
int w = p2.se;
if (vis[y] || dist[y] < W + w)
continue;
dist[y] = W + w;
q.push({W + w, y});
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen(TASK".inp","r",stdin);
//freopen(TASK".out","w",stdout);
int n,m,x,y,c,p;
cin >> n >> m;
FOR(i,1,m)
{
cin >> x >> y >> c >> p;
g[x].push_back({c, {y, p}});
g[y].push_back({c, {x, p}});
a[x].push_back({y, p});
a[y].push_back({x, p});
}
int cur = n;
FOR(i,1,n)
{
sort(g[i].begin(), g[i].end());
for (auto x : g[i])
sum[x.fi] += x.se.se;
for (auto x : g[i])
{
if (!vis[x.fi])
{
++cur;
a[i].push_back(m_p(cur,0));
}
vis[x.fi] = 1;
a[cur].push_back(m_p(x.se.fi, sum[x.fi] - x.se.se));
a[x.se.fi].push_back(m_p(cur, 0));
}
for (auto x : g[i])
{
sum[x.fi] = 0;
vis[x.fi] = 0;
}
}
dij(1);
if (dist[n] == oo)
cout << "-1";
else
cout << dist[n];
return 0;
}
Compilation message
Main.cpp: In function 'void dij(long long int)':
Main.cpp:37:21: warning: iteration 600004 invokes undefined behavior [-Waggressive-loop-optimizations]
37 | FOR(i,1,N) dist[i] = oo;
| ~~~~~~~~^~~~
Main.cpp:8:52: note: within this loop
8 | #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
| ^
Main.cpp:37:2: note: in expansion of macro 'FOR'
37 | FOR(i,1,N) dist[i] = oo;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
61264 KB |
Output is correct |
2 |
Correct |
26 ms |
61276 KB |
Output is correct |
3 |
Correct |
26 ms |
61532 KB |
Output is correct |
4 |
Correct |
26 ms |
61528 KB |
Output is correct |
5 |
Correct |
26 ms |
61368 KB |
Output is correct |
6 |
Correct |
25 ms |
61528 KB |
Output is correct |
7 |
Correct |
28 ms |
61528 KB |
Output is correct |
8 |
Correct |
25 ms |
61524 KB |
Output is correct |
9 |
Correct |
26 ms |
62296 KB |
Output is correct |
10 |
Correct |
26 ms |
62044 KB |
Output is correct |
11 |
Correct |
26 ms |
62044 KB |
Output is correct |
12 |
Correct |
26 ms |
61856 KB |
Output is correct |
13 |
Correct |
26 ms |
62044 KB |
Output is correct |
14 |
Correct |
27 ms |
62040 KB |
Output is correct |
15 |
Correct |
25 ms |
61788 KB |
Output is correct |
16 |
Correct |
27 ms |
62064 KB |
Output is correct |
17 |
Correct |
27 ms |
61788 KB |
Output is correct |
18 |
Correct |
25 ms |
61520 KB |
Output is correct |
19 |
Correct |
27 ms |
62044 KB |
Output is correct |
20 |
Correct |
27 ms |
61788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
83404 KB |
Output is correct |
2 |
Correct |
63 ms |
71368 KB |
Output is correct |
3 |
Correct |
134 ms |
95484 KB |
Output is correct |
4 |
Correct |
83 ms |
75212 KB |
Output is correct |
5 |
Correct |
301 ms |
122256 KB |
Output is correct |
6 |
Correct |
271 ms |
118032 KB |
Output is correct |
7 |
Correct |
203 ms |
111220 KB |
Output is correct |
8 |
Correct |
276 ms |
112212 KB |
Output is correct |
9 |
Correct |
272 ms |
111956 KB |
Output is correct |
10 |
Correct |
172 ms |
94408 KB |
Output is correct |
11 |
Correct |
106 ms |
86352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
61264 KB |
Output is correct |
2 |
Correct |
26 ms |
61276 KB |
Output is correct |
3 |
Correct |
26 ms |
61532 KB |
Output is correct |
4 |
Correct |
26 ms |
61528 KB |
Output is correct |
5 |
Correct |
26 ms |
61368 KB |
Output is correct |
6 |
Correct |
25 ms |
61528 KB |
Output is correct |
7 |
Correct |
28 ms |
61528 KB |
Output is correct |
8 |
Correct |
25 ms |
61524 KB |
Output is correct |
9 |
Correct |
26 ms |
62296 KB |
Output is correct |
10 |
Correct |
26 ms |
62044 KB |
Output is correct |
11 |
Correct |
26 ms |
62044 KB |
Output is correct |
12 |
Correct |
26 ms |
61856 KB |
Output is correct |
13 |
Correct |
26 ms |
62044 KB |
Output is correct |
14 |
Correct |
27 ms |
62040 KB |
Output is correct |
15 |
Correct |
25 ms |
61788 KB |
Output is correct |
16 |
Correct |
27 ms |
62064 KB |
Output is correct |
17 |
Correct |
27 ms |
61788 KB |
Output is correct |
18 |
Correct |
25 ms |
61520 KB |
Output is correct |
19 |
Correct |
27 ms |
62044 KB |
Output is correct |
20 |
Correct |
27 ms |
61788 KB |
Output is correct |
21 |
Correct |
122 ms |
83404 KB |
Output is correct |
22 |
Correct |
63 ms |
71368 KB |
Output is correct |
23 |
Correct |
134 ms |
95484 KB |
Output is correct |
24 |
Correct |
83 ms |
75212 KB |
Output is correct |
25 |
Correct |
301 ms |
122256 KB |
Output is correct |
26 |
Correct |
271 ms |
118032 KB |
Output is correct |
27 |
Correct |
203 ms |
111220 KB |
Output is correct |
28 |
Correct |
276 ms |
112212 KB |
Output is correct |
29 |
Correct |
272 ms |
111956 KB |
Output is correct |
30 |
Correct |
172 ms |
94408 KB |
Output is correct |
31 |
Correct |
106 ms |
86352 KB |
Output is correct |
32 |
Correct |
100 ms |
94936 KB |
Output is correct |
33 |
Correct |
117 ms |
90860 KB |
Output is correct |
34 |
Correct |
194 ms |
99528 KB |
Output is correct |
35 |
Correct |
152 ms |
92004 KB |
Output is correct |
36 |
Correct |
195 ms |
97044 KB |
Output is correct |
37 |
Correct |
220 ms |
107992 KB |
Output is correct |
38 |
Correct |
236 ms |
113296 KB |
Output is correct |
39 |
Correct |
133 ms |
107756 KB |
Output is correct |
40 |
Correct |
269 ms |
113456 KB |
Output is correct |
41 |
Correct |
292 ms |
113492 KB |
Output is correct |
42 |
Correct |
265 ms |
108744 KB |
Output is correct |
43 |
Correct |
107 ms |
86740 KB |
Output is correct |
44 |
Correct |
178 ms |
111984 KB |
Output is correct |
45 |
Correct |
226 ms |
100936 KB |
Output is correct |
46 |
Correct |
196 ms |
97152 KB |
Output is correct |
47 |
Correct |
221 ms |
101916 KB |
Output is correct |
48 |
Correct |
289 ms |
120900 KB |
Output is correct |