#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <utility>
using namespace std;
#define int long long
#define pi pair<int,int>
const int N = 203, M = 5 * 10^4 + 3;
const int inf = 1e17;
vector<vector<int>> dis(N, vector<int>(N, inf));
multiset<pi> g[N];
vector<int> wt;
vector<int> cost;
int n, m;
int dijkstra(int x, int y){
vector<int> dist(n + 1, inf);
vector<bool> vis(n + 1, 0);
priority_queue<pi, vector<pi>, greater<pi>> ms;
ms.push({0, x});
dist[x] = 0;
while (!ms.empty()) {
auto p = ms.top();
int v = p.second;
ms.pop();
if(vis[v])
continue;
vis[v] = true;
for (auto ch : g[v]){
int ch_v = ch.first;
int ch_wt = wt[ch.second];
if (dist[v] + ch_wt < dist[ch_v]){
dist[ch_v] = dist[v] + ch_wt;
ms.push({dist[ch_v], ch_v});
}
}
}
return dist[y];
}
signed main()
{
cin >> n >> m;
wt.resize(m);
cost.resize(m);
for (int i = 1; i <= n; i++)
{
dis[i][i] = 0;
}
pi edges[m];
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v >> wt[i] >> cost[i];
g[u].insert({v, i});
edges[i] = {u, v};
dis[u][v] = min(dis[u][v], wt[i]);
}
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
int ans = min(inf, dis[1][n] + dis[n][1]);
for (int i = 0; i < m; i++)
{
int u = edges[i].first, v = edges[i].second;
if (min(dis[1][n], dis[1][v] + wt[i] + dis[u][n]) +
min(dis[n][1], dis[n][v] + wt[i] + dis[u][1]) + cost[i] <
ans)
{
g[u].erase(g[u].find({v, i}));
g[v].insert({u, i});
ans = min(ans, cost[i] + dijkstra(1, n) + dijkstra(n, 1));
g[v].erase(g[v].find({edges[i].first, i}));
g[u].insert({v, i});
}
}
if (ans == inf)
{
cout << -1;
return (int)0;
}
cout << ans;
}
Compilation message
ho_t4.cpp:10:33: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
10 | const int N = 203, M = 5 * 10^4 + 3;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
9 ms |
804 KB |
Output is correct |
3 |
Correct |
9 ms |
1144 KB |
Output is correct |
4 |
Correct |
8 ms |
796 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
8 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
1112 KB |
Output is correct |
10 |
Correct |
11 ms |
860 KB |
Output is correct |
11 |
Correct |
20 ms |
860 KB |
Output is correct |
12 |
Correct |
9 ms |
860 KB |
Output is correct |
13 |
Correct |
9 ms |
860 KB |
Output is correct |
14 |
Correct |
8 ms |
888 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
9 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
6228 KB |
Output is correct |
2 |
Correct |
54 ms |
6464 KB |
Output is correct |
3 |
Correct |
56 ms |
6548 KB |
Output is correct |
4 |
Correct |
9 ms |
860 KB |
Output is correct |
5 |
Correct |
8 ms |
856 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
80 ms |
6484 KB |
Output is correct |
10 |
Correct |
68 ms |
6480 KB |
Output is correct |
11 |
Correct |
59 ms |
6500 KB |
Output is correct |
12 |
Correct |
58 ms |
6736 KB |
Output is correct |
13 |
Correct |
57 ms |
6476 KB |
Output is correct |
14 |
Correct |
56 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
800 KB |
Output is correct |
3 |
Correct |
42 ms |
4956 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
54 ms |
6252 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
76 ms |
6392 KB |
Output is correct |
9 |
Correct |
85 ms |
6392 KB |
Output is correct |
10 |
Correct |
54 ms |
6380 KB |
Output is correct |
11 |
Correct |
64 ms |
6628 KB |
Output is correct |
12 |
Correct |
59 ms |
6184 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
55 ms |
6224 KB |
Output is correct |
20 |
Correct |
59 ms |
6372 KB |
Output is correct |
21 |
Correct |
55 ms |
6192 KB |
Output is correct |
22 |
Correct |
55 ms |
6228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
9 ms |
804 KB |
Output is correct |
3 |
Correct |
9 ms |
1144 KB |
Output is correct |
4 |
Correct |
8 ms |
796 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
8 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
1112 KB |
Output is correct |
10 |
Correct |
11 ms |
860 KB |
Output is correct |
11 |
Correct |
20 ms |
860 KB |
Output is correct |
12 |
Correct |
9 ms |
860 KB |
Output is correct |
13 |
Correct |
9 ms |
860 KB |
Output is correct |
14 |
Correct |
8 ms |
888 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
9 ms |
860 KB |
Output is correct |
17 |
Correct |
53 ms |
6228 KB |
Output is correct |
18 |
Correct |
54 ms |
6464 KB |
Output is correct |
19 |
Correct |
56 ms |
6548 KB |
Output is correct |
20 |
Correct |
9 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
856 KB |
Output is correct |
22 |
Correct |
8 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
80 ms |
6484 KB |
Output is correct |
26 |
Correct |
68 ms |
6480 KB |
Output is correct |
27 |
Correct |
59 ms |
6500 KB |
Output is correct |
28 |
Correct |
58 ms |
6736 KB |
Output is correct |
29 |
Correct |
57 ms |
6476 KB |
Output is correct |
30 |
Correct |
56 ms |
6480 KB |
Output is correct |
31 |
Correct |
8 ms |
860 KB |
Output is correct |
32 |
Correct |
7 ms |
800 KB |
Output is correct |
33 |
Correct |
42 ms |
4956 KB |
Output is correct |
34 |
Correct |
7 ms |
604 KB |
Output is correct |
35 |
Correct |
54 ms |
6252 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
76 ms |
6392 KB |
Output is correct |
39 |
Correct |
85 ms |
6392 KB |
Output is correct |
40 |
Correct |
54 ms |
6380 KB |
Output is correct |
41 |
Correct |
64 ms |
6628 KB |
Output is correct |
42 |
Correct |
59 ms |
6184 KB |
Output is correct |
43 |
Correct |
1 ms |
600 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
0 ms |
604 KB |
Output is correct |
49 |
Correct |
55 ms |
6224 KB |
Output is correct |
50 |
Correct |
59 ms |
6372 KB |
Output is correct |
51 |
Correct |
55 ms |
6192 KB |
Output is correct |
52 |
Correct |
55 ms |
6228 KB |
Output is correct |
53 |
Correct |
54 ms |
6248 KB |
Output is correct |
54 |
Correct |
53 ms |
6288 KB |
Output is correct |
55 |
Correct |
53 ms |
6240 KB |
Output is correct |
56 |
Correct |
9 ms |
856 KB |
Output is correct |
57 |
Correct |
8 ms |
856 KB |
Output is correct |
58 |
Correct |
54 ms |
5200 KB |
Output is correct |
59 |
Correct |
54 ms |
5200 KB |
Output is correct |
60 |
Correct |
57 ms |
5236 KB |
Output is correct |
61 |
Correct |
72 ms |
5240 KB |
Output is correct |
62 |
Correct |
181 ms |
5340 KB |
Output is correct |
63 |
Correct |
380 ms |
5376 KB |
Output is correct |
64 |
Correct |
110 ms |
5736 KB |
Output is correct |
65 |
Correct |
258 ms |
5836 KB |
Output is correct |
66 |
Correct |
469 ms |
5920 KB |
Output is correct |
67 |
Correct |
42 ms |
5196 KB |
Output is correct |
68 |
Correct |
78 ms |
6480 KB |
Output is correct |
69 |
Correct |
79 ms |
6764 KB |
Output is correct |
70 |
Correct |
58 ms |
6636 KB |
Output is correct |
71 |
Correct |
62 ms |
6484 KB |
Output is correct |
72 |
Correct |
61 ms |
6604 KB |
Output is correct |
73 |
Correct |
59 ms |
6484 KB |
Output is correct |
74 |
Correct |
63 ms |
6468 KB |
Output is correct |