#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e10;
const int N = 205;
using T = pair<int,int>;
struct Edge {
int u, v, w, c;
tuple<int,int,int,int> init() {
return {u, v, w, c};
}
};
int n, m, d[N][N], dis[N];
Edge edges[maxn];
set<int> adj[maxn];
priority_queue<T, vector<T>, greater<T>> pq;
int dijkstra(int s, int t) {
for (int i = 1; i <= n; i++)
dis[i] = INF;
dis[s] = 0; pq.push({0, s});
while (!pq.empty()) {
int u, d; tie(d, u) = pq.top(); pq.pop();
if (dis[u] != d) continue;
for (int i : adj[u]) {
int v = edges[i].v, w = edges[i].w;
if (dis[u] + w < dis[v]) {
dis[v] = dis[u] + w;
pq.push({dis[v], v});
}
}
}
return dis[t];
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j)
d[i][j] = INF;
for (int i = 1; i <= m; i++) {
int u, v, w, c; cin >> u >> v >> w >> c;
d[u][v] = min(d[u][v], w);
edges[i] = {u, v, w, c};
edges[i + m] = {v, u, w, c};
adj[u].insert(i);
}
for (int k = 1; k <= n; k++)
for (int u = 1; u <= n; u++)
for (int v = 1; v <= n; v++)
d[u][v] = min(d[u][v], d[u][k] + d[k][v]);
int res = INF;
int s = 1, t = n;
res = min(res, d[s][t] + d[t][s]);
for (int i = 1; i <= m; i++) {
int u, v, w, c; tie(u, v, w, c) = edges[i].init();
int from = min(d[s][t], d[s][v] + w + d[u][t]);
int to = min(d[t][s], d[t][v] + w + d[u][s]);
if (from + to + c < res) {
adj[u].erase(i); adj[v].insert(i + m);
res = min(res, c + dijkstra(s, t) + dijkstra(t, s));
adj[v].erase(i + m); adj[u].insert(i);
}
}
cout << (res == INF ? -1 : res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
5460 KB |
Output is correct |
2 |
Correct |
11 ms |
5380 KB |
Output is correct |
3 |
Correct |
12 ms |
5460 KB |
Output is correct |
4 |
Correct |
11 ms |
5424 KB |
Output is correct |
5 |
Correct |
4 ms |
5152 KB |
Output is correct |
6 |
Correct |
16 ms |
5388 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4988 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
15 ms |
5484 KB |
Output is correct |
11 |
Correct |
26 ms |
5468 KB |
Output is correct |
12 |
Correct |
12 ms |
5424 KB |
Output is correct |
13 |
Correct |
11 ms |
5484 KB |
Output is correct |
14 |
Correct |
11 ms |
5460 KB |
Output is correct |
15 |
Correct |
11 ms |
5460 KB |
Output is correct |
16 |
Correct |
11 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
10812 KB |
Output is correct |
2 |
Correct |
46 ms |
11724 KB |
Output is correct |
3 |
Correct |
39 ms |
11892 KB |
Output is correct |
4 |
Correct |
12 ms |
5588 KB |
Output is correct |
5 |
Correct |
11 ms |
5460 KB |
Output is correct |
6 |
Correct |
11 ms |
5332 KB |
Output is correct |
7 |
Correct |
11 ms |
5332 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
57 ms |
11964 KB |
Output is correct |
10 |
Correct |
53 ms |
11984 KB |
Output is correct |
11 |
Correct |
47 ms |
11956 KB |
Output is correct |
12 |
Correct |
39 ms |
11956 KB |
Output is correct |
13 |
Correct |
41 ms |
11944 KB |
Output is correct |
14 |
Correct |
42 ms |
11908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
5456 KB |
Output is correct |
2 |
Correct |
12 ms |
5396 KB |
Output is correct |
3 |
Correct |
36 ms |
10244 KB |
Output is correct |
4 |
Correct |
11 ms |
5384 KB |
Output is correct |
5 |
Correct |
38 ms |
11660 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
63 ms |
11696 KB |
Output is correct |
9 |
Correct |
76 ms |
11724 KB |
Output is correct |
10 |
Correct |
43 ms |
11724 KB |
Output is correct |
11 |
Correct |
44 ms |
11732 KB |
Output is correct |
12 |
Correct |
39 ms |
11724 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
42 ms |
11728 KB |
Output is correct |
20 |
Correct |
46 ms |
11716 KB |
Output is correct |
21 |
Correct |
42 ms |
11696 KB |
Output is correct |
22 |
Correct |
42 ms |
11844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
5460 KB |
Output is correct |
2 |
Correct |
11 ms |
5380 KB |
Output is correct |
3 |
Correct |
12 ms |
5460 KB |
Output is correct |
4 |
Correct |
11 ms |
5424 KB |
Output is correct |
5 |
Correct |
4 ms |
5152 KB |
Output is correct |
6 |
Correct |
16 ms |
5388 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4988 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
15 ms |
5484 KB |
Output is correct |
11 |
Correct |
26 ms |
5468 KB |
Output is correct |
12 |
Correct |
12 ms |
5424 KB |
Output is correct |
13 |
Correct |
11 ms |
5484 KB |
Output is correct |
14 |
Correct |
11 ms |
5460 KB |
Output is correct |
15 |
Correct |
11 ms |
5460 KB |
Output is correct |
16 |
Correct |
11 ms |
5460 KB |
Output is correct |
17 |
Correct |
34 ms |
10812 KB |
Output is correct |
18 |
Correct |
46 ms |
11724 KB |
Output is correct |
19 |
Correct |
39 ms |
11892 KB |
Output is correct |
20 |
Correct |
12 ms |
5588 KB |
Output is correct |
21 |
Correct |
11 ms |
5460 KB |
Output is correct |
22 |
Correct |
11 ms |
5332 KB |
Output is correct |
23 |
Correct |
11 ms |
5332 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
57 ms |
11964 KB |
Output is correct |
26 |
Correct |
53 ms |
11984 KB |
Output is correct |
27 |
Correct |
47 ms |
11956 KB |
Output is correct |
28 |
Correct |
39 ms |
11956 KB |
Output is correct |
29 |
Correct |
41 ms |
11944 KB |
Output is correct |
30 |
Correct |
42 ms |
11908 KB |
Output is correct |
31 |
Correct |
12 ms |
5456 KB |
Output is correct |
32 |
Correct |
12 ms |
5396 KB |
Output is correct |
33 |
Correct |
36 ms |
10244 KB |
Output is correct |
34 |
Correct |
11 ms |
5384 KB |
Output is correct |
35 |
Correct |
38 ms |
11660 KB |
Output is correct |
36 |
Correct |
3 ms |
4940 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
63 ms |
11696 KB |
Output is correct |
39 |
Correct |
76 ms |
11724 KB |
Output is correct |
40 |
Correct |
43 ms |
11724 KB |
Output is correct |
41 |
Correct |
44 ms |
11732 KB |
Output is correct |
42 |
Correct |
39 ms |
11724 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
3 ms |
4948 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
5032 KB |
Output is correct |
47 |
Correct |
3 ms |
4948 KB |
Output is correct |
48 |
Correct |
3 ms |
4948 KB |
Output is correct |
49 |
Correct |
42 ms |
11728 KB |
Output is correct |
50 |
Correct |
46 ms |
11716 KB |
Output is correct |
51 |
Correct |
42 ms |
11696 KB |
Output is correct |
52 |
Correct |
42 ms |
11844 KB |
Output is correct |
53 |
Correct |
37 ms |
11816 KB |
Output is correct |
54 |
Correct |
39 ms |
11756 KB |
Output is correct |
55 |
Correct |
39 ms |
11768 KB |
Output is correct |
56 |
Correct |
11 ms |
5484 KB |
Output is correct |
57 |
Correct |
12 ms |
5424 KB |
Output is correct |
58 |
Correct |
44 ms |
10444 KB |
Output is correct |
59 |
Correct |
41 ms |
10444 KB |
Output is correct |
60 |
Correct |
43 ms |
10504 KB |
Output is correct |
61 |
Correct |
44 ms |
10564 KB |
Output is correct |
62 |
Correct |
180 ms |
10572 KB |
Output is correct |
63 |
Correct |
328 ms |
10564 KB |
Output is correct |
64 |
Correct |
101 ms |
10956 KB |
Output is correct |
65 |
Correct |
260 ms |
10924 KB |
Output is correct |
66 |
Correct |
505 ms |
10900 KB |
Output is correct |
67 |
Correct |
44 ms |
10192 KB |
Output is correct |
68 |
Correct |
59 ms |
11868 KB |
Output is correct |
69 |
Correct |
53 ms |
11992 KB |
Output is correct |
70 |
Correct |
43 ms |
11952 KB |
Output is correct |
71 |
Correct |
48 ms |
11972 KB |
Output is correct |
72 |
Correct |
44 ms |
11868 KB |
Output is correct |
73 |
Correct |
51 ms |
11896 KB |
Output is correct |
74 |
Correct |
47 ms |
11988 KB |
Output is correct |