//**********************
//
//**********************
#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;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
61528 KB |
Output is correct |
2 |
Correct |
23 ms |
61532 KB |
Output is correct |
3 |
Correct |
31 ms |
61520 KB |
Output is correct |
4 |
Correct |
26 ms |
61532 KB |
Output is correct |
5 |
Correct |
25 ms |
61520 KB |
Output is correct |
6 |
Correct |
25 ms |
61532 KB |
Output is correct |
7 |
Correct |
25 ms |
61524 KB |
Output is correct |
8 |
Correct |
25 ms |
61532 KB |
Output is correct |
9 |
Correct |
27 ms |
62044 KB |
Output is correct |
10 |
Correct |
26 ms |
62044 KB |
Output is correct |
11 |
Correct |
31 ms |
62036 KB |
Output is correct |
12 |
Correct |
31 ms |
62044 KB |
Output is correct |
13 |
Correct |
27 ms |
62044 KB |
Output is correct |
14 |
Correct |
26 ms |
62044 KB |
Output is correct |
15 |
Correct |
29 ms |
62032 KB |
Output is correct |
16 |
Correct |
27 ms |
62056 KB |
Output is correct |
17 |
Correct |
27 ms |
61744 KB |
Output is correct |
18 |
Correct |
26 ms |
61532 KB |
Output is correct |
19 |
Correct |
26 ms |
62044 KB |
Output is correct |
20 |
Correct |
26 ms |
61788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
83404 KB |
Output is correct |
2 |
Correct |
65 ms |
71408 KB |
Output is correct |
3 |
Correct |
136 ms |
95264 KB |
Output is correct |
4 |
Correct |
84 ms |
75368 KB |
Output is correct |
5 |
Correct |
305 ms |
122432 KB |
Output is correct |
6 |
Correct |
278 ms |
117808 KB |
Output is correct |
7 |
Correct |
209 ms |
111104 KB |
Output is correct |
8 |
Correct |
275 ms |
112260 KB |
Output is correct |
9 |
Correct |
265 ms |
111956 KB |
Output is correct |
10 |
Correct |
169 ms |
94408 KB |
Output is correct |
11 |
Correct |
118 ms |
86352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
61528 KB |
Output is correct |
2 |
Correct |
23 ms |
61532 KB |
Output is correct |
3 |
Correct |
31 ms |
61520 KB |
Output is correct |
4 |
Correct |
26 ms |
61532 KB |
Output is correct |
5 |
Correct |
25 ms |
61520 KB |
Output is correct |
6 |
Correct |
25 ms |
61532 KB |
Output is correct |
7 |
Correct |
25 ms |
61524 KB |
Output is correct |
8 |
Correct |
25 ms |
61532 KB |
Output is correct |
9 |
Correct |
27 ms |
62044 KB |
Output is correct |
10 |
Correct |
26 ms |
62044 KB |
Output is correct |
11 |
Correct |
31 ms |
62036 KB |
Output is correct |
12 |
Correct |
31 ms |
62044 KB |
Output is correct |
13 |
Correct |
27 ms |
62044 KB |
Output is correct |
14 |
Correct |
26 ms |
62044 KB |
Output is correct |
15 |
Correct |
29 ms |
62032 KB |
Output is correct |
16 |
Correct |
27 ms |
62056 KB |
Output is correct |
17 |
Correct |
27 ms |
61744 KB |
Output is correct |
18 |
Correct |
26 ms |
61532 KB |
Output is correct |
19 |
Correct |
26 ms |
62044 KB |
Output is correct |
20 |
Correct |
26 ms |
61788 KB |
Output is correct |
21 |
Correct |
121 ms |
83404 KB |
Output is correct |
22 |
Correct |
65 ms |
71408 KB |
Output is correct |
23 |
Correct |
136 ms |
95264 KB |
Output is correct |
24 |
Correct |
84 ms |
75368 KB |
Output is correct |
25 |
Correct |
305 ms |
122432 KB |
Output is correct |
26 |
Correct |
278 ms |
117808 KB |
Output is correct |
27 |
Correct |
209 ms |
111104 KB |
Output is correct |
28 |
Correct |
275 ms |
112260 KB |
Output is correct |
29 |
Correct |
265 ms |
111956 KB |
Output is correct |
30 |
Correct |
169 ms |
94408 KB |
Output is correct |
31 |
Correct |
118 ms |
86352 KB |
Output is correct |
32 |
Correct |
99 ms |
94940 KB |
Output is correct |
33 |
Correct |
114 ms |
90912 KB |
Output is correct |
34 |
Correct |
194 ms |
99524 KB |
Output is correct |
35 |
Correct |
150 ms |
91844 KB |
Output is correct |
36 |
Correct |
193 ms |
97196 KB |
Output is correct |
37 |
Correct |
247 ms |
107932 KB |
Output is correct |
38 |
Correct |
208 ms |
113304 KB |
Output is correct |
39 |
Correct |
130 ms |
107720 KB |
Output is correct |
40 |
Correct |
250 ms |
113412 KB |
Output is correct |
41 |
Correct |
268 ms |
113492 KB |
Output is correct |
42 |
Correct |
262 ms |
108488 KB |
Output is correct |
43 |
Correct |
105 ms |
86740 KB |
Output is correct |
44 |
Correct |
169 ms |
111804 KB |
Output is correct |
45 |
Correct |
212 ms |
100948 KB |
Output is correct |
46 |
Correct |
184 ms |
97104 KB |
Output is correct |
47 |
Correct |
224 ms |
101916 KB |
Output is correct |
48 |
Correct |
292 ms |
120904 KB |
Output is correct |