#pragma GCc optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 201, M = 50001;
ll n, m;
struct Edge {
ll u, v, i, c;
};
vector<pair<ll, pair<ll, bool>>> adj[N];
ll dist[N][N];
ll c[M], d[M];
Edge edges[M];
ll sssp(ll source, ll i, ll dest) {
vector<ll> dist(n + 1, 1e15);
dist[source] = 0;
priority_queue<pair<ll, ll>> pq;
pq.push({0, source});
while (!pq.empty()) {
ll node = pq.top().second, _dist = -pq.top().first;
pq.pop();
if (node == dest) {
return dist[node];
}
for (auto &nb : adj[node]) {
if (nb.second.second) {
if (nb.second.first != i) {
continue;
}
} else if (nb.second.first == i) {
continue;
}
if (dist[nb.first] > _dist + c[nb.second.first]) {
dist[nb.first] = _dist + c[nb.second.first];
pq.push({-dist[nb.first], nb.first});
}
}
}
return dist[dest];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (ll i = 1; i <= n; ++i) {
adj[i].clear();
}
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= n; ++j) {
dist[i][j] = 1e15;
}
}
for (ll i = 1; i <= n; ++i) {
dist[i][i] = 0;
}
for (ll i = 0, u, v; i < m; ++i) {
cin >> u >> v >> c[i] >> d[i];
adj[u].push_back({v, {i, 0}}), adj[v].push_back({u, {i, 1}});
edges[i].u = u, edges[i].v = v, edges[i].i = i, edges[i].c = c[i];
dist[u][v] = min(dist[u][v], c[i]);
}
for (ll k = 1; k <= n; ++k) {
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
ll ans = min((ll)1e15, dist[1][n] + dist[n][1]);
for (auto &[u, v, i, c] : edges) {
ll d1 = min(dist[1][n], dist[1][v] + c + dist[u][n]);
ll d2 = min(dist[n][1], dist[n][v] + c + dist[u][1]);
if (d1 + d2 + d[i] < ans) {
ll actual = d[i] + sssp(1, i, n) + sssp(n, i, 1);
ans = min(ans, actual);
}
}
if (ans == (ll)(1e15)) {
ans = -1;
}
cout << ans << "\n";
}
Compilation message
ho_t4.cpp:1: warning: ignoring '#pragma GCc optimize' [-Wunknown-pragmas]
1 | #pragma GCc optimize("Ofast")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2904 KB |
Output is correct |
2 |
Correct |
33 ms |
2852 KB |
Output is correct |
3 |
Correct |
8 ms |
2908 KB |
Output is correct |
4 |
Correct |
8 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
68 ms |
2652 KB |
Output is correct |
7 |
Correct |
5 ms |
2396 KB |
Output is correct |
8 |
Correct |
5 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Execution timed out |
1096 ms |
2956 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
6492 KB |
Output is correct |
2 |
Correct |
22 ms |
6488 KB |
Output is correct |
3 |
Correct |
28 ms |
6744 KB |
Output is correct |
4 |
Correct |
261 ms |
2908 KB |
Output is correct |
5 |
Correct |
398 ms |
3104 KB |
Output is correct |
6 |
Correct |
23 ms |
2652 KB |
Output is correct |
7 |
Correct |
55 ms |
2852 KB |
Output is correct |
8 |
Correct |
6 ms |
2392 KB |
Output is correct |
9 |
Correct |
34 ms |
6424 KB |
Output is correct |
10 |
Correct |
28 ms |
6492 KB |
Output is correct |
11 |
Correct |
26 ms |
6744 KB |
Output is correct |
12 |
Correct |
23 ms |
6492 KB |
Output is correct |
13 |
Correct |
23 ms |
6492 KB |
Output is correct |
14 |
Correct |
23 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2908 KB |
Output is correct |
2 |
Correct |
117 ms |
2844 KB |
Output is correct |
3 |
Correct |
18 ms |
5716 KB |
Output is correct |
4 |
Correct |
67 ms |
2652 KB |
Output is correct |
5 |
Correct |
20 ms |
6384 KB |
Output is correct |
6 |
Correct |
5 ms |
2392 KB |
Output is correct |
7 |
Correct |
6 ms |
2648 KB |
Output is correct |
8 |
Correct |
28 ms |
6492 KB |
Output is correct |
9 |
Correct |
30 ms |
6492 KB |
Output is correct |
10 |
Correct |
22 ms |
6492 KB |
Output is correct |
11 |
Correct |
21 ms |
6492 KB |
Output is correct |
12 |
Correct |
22 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
27 ms |
6744 KB |
Output is correct |
20 |
Correct |
22 ms |
6592 KB |
Output is correct |
21 |
Correct |
23 ms |
6492 KB |
Output is correct |
22 |
Correct |
23 ms |
6556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2904 KB |
Output is correct |
2 |
Correct |
33 ms |
2852 KB |
Output is correct |
3 |
Correct |
8 ms |
2908 KB |
Output is correct |
4 |
Correct |
8 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
68 ms |
2652 KB |
Output is correct |
7 |
Correct |
5 ms |
2396 KB |
Output is correct |
8 |
Correct |
5 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Execution timed out |
1096 ms |
2956 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |