#include<bits/stdc++.h>
#define int long long
#define nl endl
using namespace std;
multiset<pair<int,int> > graph[201];
int Dijkstra(int u, int v){
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
vector<int> vis(201, 0);
vector<int> dist(201, 1e18);
dist[u] = 0;
pq.push(make_pair(0, u));
while(!pq.empty()){
int p = pq.top().second;
int dis = pq.top().first;
pq.pop();
if(vis[p]) continue;
vis[p] = 1;
for(auto ch : graph[p]){
if(dis + ch.second < dist[ch.first]){
dist[ch.first] = dis + ch.second;
pq.push(make_pair(dist[ch.first], ch.first));
}
}
}
return dist[v];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int n, m;
cin>>n>>m;
vector<vector<int> > dist(n+1, vector<int>(n+1, 1e18));
int U[m], V[m], c[m], d[m];
for(int i=0; i<m; i++){
cin>>U[i]>>V[i]>>c[i]>>d[i];
graph[U[i]].insert(make_pair(V[i], c[i]));
dist[U[i]][V[i]] = min(c[i], dist[U[i]][V[i]]);
}
for(int i=1; i<=n; i++) dist[i][i] = 0;
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int ans = dist[1][n] + dist[n][1];
//cout<<ans<<nl;
for(int i=0; i<m; i++){
int u = U[i], v = V[i];
if(min(dist[1][n], dist[1][v] + c[i] + dist[u][n]) + min(dist[n][1], dist[n][v] + c[i] + dist[u][1]) + d[i] >= ans) continue;
graph[u].erase(graph[u].find(make_pair(v, c[i])));
graph[v].insert(make_pair(u, c[i]));
ans = min(ans, Dijkstra(1, n) + Dijkstra(n, 1) + d[i]);
//cout<<i<<" "<<Dijkstra(1, n) + Dijkstra(n, 1) + d[i]<<nl;
graph[u].insert(make_pair(v, c[i]));
graph[v].erase(graph[v].find(make_pair(u, c[i])));
}
if(ans >= 1e18) cout<<-1;
else cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
860 KB |
Output is correct |
4 |
Correct |
7 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
908 KB |
Output is correct |
11 |
Correct |
24 ms |
908 KB |
Output is correct |
12 |
Correct |
9 ms |
856 KB |
Output is correct |
13 |
Correct |
8 ms |
856 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
6128 KB |
Output is correct |
2 |
Correct |
31 ms |
6188 KB |
Output is correct |
3 |
Correct |
28 ms |
6072 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
8 ms |
860 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
46 ms |
6604 KB |
Output is correct |
10 |
Correct |
45 ms |
6488 KB |
Output is correct |
11 |
Correct |
32 ms |
6892 KB |
Output is correct |
12 |
Correct |
31 ms |
6548 KB |
Output is correct |
13 |
Correct |
32 ms |
6524 KB |
Output is correct |
14 |
Correct |
36 ms |
6584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
820 KB |
Output is correct |
3 |
Correct |
24 ms |
4844 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
34 ms |
6256 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
51 ms |
6244 KB |
Output is correct |
9 |
Correct |
74 ms |
6232 KB |
Output is correct |
10 |
Correct |
32 ms |
6392 KB |
Output is correct |
11 |
Correct |
30 ms |
6096 KB |
Output is correct |
12 |
Correct |
31 ms |
6168 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
34 ms |
6380 KB |
Output is correct |
20 |
Correct |
36 ms |
6224 KB |
Output is correct |
21 |
Correct |
34 ms |
6236 KB |
Output is correct |
22 |
Correct |
31 ms |
6224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
860 KB |
Output is correct |
4 |
Correct |
7 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
908 KB |
Output is correct |
11 |
Correct |
24 ms |
908 KB |
Output is correct |
12 |
Correct |
9 ms |
856 KB |
Output is correct |
13 |
Correct |
8 ms |
856 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
17 |
Correct |
27 ms |
6128 KB |
Output is correct |
18 |
Correct |
31 ms |
6188 KB |
Output is correct |
19 |
Correct |
28 ms |
6072 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
860 KB |
Output is correct |
22 |
Correct |
7 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
46 ms |
6604 KB |
Output is correct |
26 |
Correct |
45 ms |
6488 KB |
Output is correct |
27 |
Correct |
32 ms |
6892 KB |
Output is correct |
28 |
Correct |
31 ms |
6548 KB |
Output is correct |
29 |
Correct |
32 ms |
6524 KB |
Output is correct |
30 |
Correct |
36 ms |
6584 KB |
Output is correct |
31 |
Correct |
8 ms |
860 KB |
Output is correct |
32 |
Correct |
7 ms |
820 KB |
Output is correct |
33 |
Correct |
24 ms |
4844 KB |
Output is correct |
34 |
Correct |
7 ms |
604 KB |
Output is correct |
35 |
Correct |
34 ms |
6256 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
51 ms |
6244 KB |
Output is correct |
39 |
Correct |
74 ms |
6232 KB |
Output is correct |
40 |
Correct |
32 ms |
6392 KB |
Output is correct |
41 |
Correct |
30 ms |
6096 KB |
Output is correct |
42 |
Correct |
31 ms |
6168 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
464 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
34 ms |
6380 KB |
Output is correct |
50 |
Correct |
36 ms |
6224 KB |
Output is correct |
51 |
Correct |
34 ms |
6236 KB |
Output is correct |
52 |
Correct |
31 ms |
6224 KB |
Output is correct |
53 |
Correct |
31 ms |
6224 KB |
Output is correct |
54 |
Correct |
30 ms |
6228 KB |
Output is correct |
55 |
Correct |
29 ms |
6236 KB |
Output is correct |
56 |
Correct |
10 ms |
904 KB |
Output is correct |
57 |
Correct |
8 ms |
1112 KB |
Output is correct |
58 |
Correct |
33 ms |
5372 KB |
Output is correct |
59 |
Correct |
33 ms |
5264 KB |
Output is correct |
60 |
Correct |
36 ms |
5324 KB |
Output is correct |
61 |
Correct |
36 ms |
5212 KB |
Output is correct |
62 |
Correct |
155 ms |
5208 KB |
Output is correct |
63 |
Correct |
331 ms |
5212 KB |
Output is correct |
64 |
Correct |
89 ms |
5772 KB |
Output is correct |
65 |
Correct |
223 ms |
5968 KB |
Output is correct |
66 |
Correct |
423 ms |
5752 KB |
Output is correct |
67 |
Correct |
36 ms |
5016 KB |
Output is correct |
68 |
Correct |
48 ms |
6492 KB |
Output is correct |
69 |
Correct |
45 ms |
6480 KB |
Output is correct |
70 |
Correct |
31 ms |
6492 KB |
Output is correct |
71 |
Correct |
35 ms |
6492 KB |
Output is correct |
72 |
Correct |
34 ms |
6484 KB |
Output is correct |
73 |
Correct |
32 ms |
6480 KB |
Output is correct |
74 |
Correct |
34 ms |
6492 KB |
Output is correct |