#pragma GCC optimize "Ofast"
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
/*
ID: samikgo1
TASK:
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef complex<ll> point;
#define X real()
#define Y imag()
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
//#define f first
//#define s second
//#define x first
//#define y second
const ll INF = 1e17;
const ll sqrtn = 440;
const ll modulo = 1e9+7;
const ll siz = 262144;
const ll hashp = 923981238;
const ll hashm = 932439994;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int ll
struct edge{
int u,v,c,d;
};
edge edgelist[50001];
set<int> adj[201];
int dist[201][201];
int distd[201];
bool visited[201];
int n;
inline int dijkastra(){
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> q;
for(int&i:distd)i=INF;
for(bool&i:visited)i=false;
int c1 = INF;
q.emplace(0,1);
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.second])continue;
if(curr.second==n){c1=curr.first;break;}
visited[curr.second]=true;
for(auto&i:adj[curr.second])if(distd[edgelist[i].v]>edgelist[i].c+curr.first) {
distd[edgelist[i].v]=edgelist[i].c+curr.first;
q.emplace(edgelist[i].c + curr.first, edgelist[i].v);
}
}
while(!q.empty())q.pop();
for(int&i:distd)i=INF;
for(bool&i:visited)i=false;
int c2 = INF;
q.emplace(0,n);
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.second])continue;
if(curr.second==1){c2=curr.first;break;}
visited[curr.second]=true;
for(auto&i:adj[curr.second])if(distd[edgelist[i].v]>edgelist[i].c+curr.first) {
distd[edgelist[i].v]=edgelist[i].c+curr.first;
q.emplace(edgelist[i].c + curr.first, edgelist[i].v);
}
}
return c1+c2;
}
void solve() {
int m;
cin >> n >> m;
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j)dist[i][j]=INF;
for(int i=1;i<=m;i++){
cin >> edgelist[i].u >> edgelist[i].v >> edgelist[i].c >> edgelist[i].d;
adj[edgelist[i].u].insert(i);
dist[edgelist[i].u][edgelist[i].v] = min(dist[edgelist[i].u][edgelist[i].v],edgelist[i].c);
}
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];
for(int i=1;i<=m;i++){
auto curr = edgelist[i];
if(min(dist[1][n],dist[1][curr.v]+dist[curr.u][n]+curr.c)+min(dist[n][1],dist[n][curr.v]+dist[curr.u][1]+curr.c)+curr.d<ans){
adj[curr.u].erase(i);
adj[curr.v].insert(i);
swap(edgelist[i].u,edgelist[i].v);
ans = min(ans,dijkastra()+curr.d);
adj[curr.v].erase(i);
adj[curr.u].insert(i);
swap(edgelist[i].u,edgelist[i].v);
}
}
cout << (ans>=INF ? -1 : ans)<< '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("mock.txt","r",stdin);
// freopen(".out","w",stdout);
// int t;
// cin >> t;
// while(t--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
1112 KB |
Output is correct |
4 |
Correct |
8 ms |
880 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
876 KB |
Output is correct |
11 |
Correct |
22 ms |
860 KB |
Output is correct |
12 |
Correct |
9 ms |
820 KB |
Output is correct |
13 |
Correct |
8 ms |
860 KB |
Output is correct |
14 |
Correct |
8 ms |
860 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 |
26 ms |
4700 KB |
Output is correct |
2 |
Correct |
27 ms |
4700 KB |
Output is correct |
3 |
Correct |
26 ms |
4700 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
8 ms |
860 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
38 ms |
4700 KB |
Output is correct |
10 |
Correct |
30 ms |
4700 KB |
Output is correct |
11 |
Correct |
27 ms |
4688 KB |
Output is correct |
12 |
Correct |
27 ms |
4568 KB |
Output is correct |
13 |
Correct |
27 ms |
4700 KB |
Output is correct |
14 |
Correct |
27 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
816 KB |
Output is correct |
3 |
Correct |
23 ms |
3676 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
25 ms |
4536 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
37 ms |
4684 KB |
Output is correct |
9 |
Correct |
42 ms |
4700 KB |
Output is correct |
10 |
Correct |
28 ms |
4560 KB |
Output is correct |
11 |
Correct |
29 ms |
4960 KB |
Output is correct |
12 |
Correct |
26 ms |
4696 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
27 ms |
4696 KB |
Output is correct |
20 |
Correct |
28 ms |
4700 KB |
Output is correct |
21 |
Correct |
31 ms |
4700 KB |
Output is correct |
22 |
Correct |
30 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
1112 KB |
Output is correct |
4 |
Correct |
8 ms |
880 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
876 KB |
Output is correct |
11 |
Correct |
22 ms |
860 KB |
Output is correct |
12 |
Correct |
9 ms |
820 KB |
Output is correct |
13 |
Correct |
8 ms |
860 KB |
Output is correct |
14 |
Correct |
8 ms |
860 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
17 |
Correct |
26 ms |
4700 KB |
Output is correct |
18 |
Correct |
27 ms |
4700 KB |
Output is correct |
19 |
Correct |
26 ms |
4700 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
860 KB |
Output is correct |
22 |
Correct |
8 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
38 ms |
4700 KB |
Output is correct |
26 |
Correct |
30 ms |
4700 KB |
Output is correct |
27 |
Correct |
27 ms |
4688 KB |
Output is correct |
28 |
Correct |
27 ms |
4568 KB |
Output is correct |
29 |
Correct |
27 ms |
4700 KB |
Output is correct |
30 |
Correct |
27 ms |
4700 KB |
Output is correct |
31 |
Correct |
8 ms |
860 KB |
Output is correct |
32 |
Correct |
8 ms |
816 KB |
Output is correct |
33 |
Correct |
23 ms |
3676 KB |
Output is correct |
34 |
Correct |
8 ms |
604 KB |
Output is correct |
35 |
Correct |
25 ms |
4536 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
37 ms |
4684 KB |
Output is correct |
39 |
Correct |
42 ms |
4700 KB |
Output is correct |
40 |
Correct |
28 ms |
4560 KB |
Output is correct |
41 |
Correct |
29 ms |
4960 KB |
Output is correct |
42 |
Correct |
26 ms |
4696 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 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 |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
27 ms |
4696 KB |
Output is correct |
50 |
Correct |
28 ms |
4700 KB |
Output is correct |
51 |
Correct |
31 ms |
4700 KB |
Output is correct |
52 |
Correct |
30 ms |
4696 KB |
Output is correct |
53 |
Correct |
26 ms |
4444 KB |
Output is correct |
54 |
Correct |
26 ms |
4700 KB |
Output is correct |
55 |
Correct |
27 ms |
4496 KB |
Output is correct |
56 |
Correct |
8 ms |
1112 KB |
Output is correct |
57 |
Correct |
8 ms |
872 KB |
Output is correct |
58 |
Correct |
30 ms |
3932 KB |
Output is correct |
59 |
Correct |
30 ms |
4856 KB |
Output is correct |
60 |
Correct |
33 ms |
4700 KB |
Output is correct |
61 |
Correct |
32 ms |
4700 KB |
Output is correct |
62 |
Correct |
145 ms |
4696 KB |
Output is correct |
63 |
Correct |
299 ms |
4748 KB |
Output is correct |
64 |
Correct |
83 ms |
5160 KB |
Output is correct |
65 |
Correct |
213 ms |
5148 KB |
Output is correct |
66 |
Correct |
406 ms |
5596 KB |
Output is correct |
67 |
Correct |
17 ms |
4276 KB |
Output is correct |
68 |
Correct |
40 ms |
5720 KB |
Output is correct |
69 |
Correct |
34 ms |
5716 KB |
Output is correct |
70 |
Correct |
29 ms |
5724 KB |
Output is correct |
71 |
Correct |
31 ms |
5744 KB |
Output is correct |
72 |
Correct |
28 ms |
5724 KB |
Output is correct |
73 |
Correct |
29 ms |
5724 KB |
Output is correct |
74 |
Correct |
38 ms |
5840 KB |
Output is correct |