#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
const int N = 203;
vector<ar<ll,3>> adj[N];
ll dis[N];
vector<ar<ll,4>> v;
void Dijkstra(ll e , int idx){
priority_queue< pair<ll,ll>, vector <pair<ll,ll>> , greater<pair<ll,ll>> > pq;
dis[e]=0;
pq.push({0,e});
while(!pq.empty()){
ll node = pq.top().second,ww=pq.top().first;
pq.pop();
if(dis[node] < ww)continue;
for(auto i : adj[node]){
if(i[2] == idx)continue;
ll w = i[1];
if(dis[i[0]] > dis[node] + w){
dis[i[0]] = dis[node] + w;
pq.push({dis[i[0]],i[0]});
}
}
if(node == v[idx][1]){
ll w = v[idx][2];
if(dis[v[idx][0]] > dis[node] + w){
dis[v[idx][0]] = dis[node] + w;
pq.push({dis[v[idx][0]],v[idx][0]});
}
}
}
}
void solve(){
int n , m;
cin >> n >> m;
ll fw[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == j)fw[i][j] = 0;
else fw[i][j] = 1e18;
}
}
for(int i = 0; i < m; i++){
ll a , b , c , d;
cin >> a >> b >> c >> d;
a--;
b--;
adj[a].push_back({b,c,i});
v.push_back({a,b,c,d});
fw[a][b] = min(fw[a][b] , c);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int k =0 ; k < n; k++){
fw[j][k] = min(fw[j][k] , fw[j][i] + fw[i][k]);
}
}
}
ll ans = fw[0][n-1] + fw[n-1][0];
for(int i = 0; i < m; i++){
ll res = min(fw[0][n-1] , fw[0][v[i][1]] + fw[v[i][0]][n-1]) + min(fw[n-1][0] , fw[n-1][v[i][1]] + fw[v[i][0]][0]) + v[i][3] + v[i][2];
if(res < ans){
for(int j = 0; j < n; j++)dis[j] = 1e18;
Dijkstra(0 , i);
res = dis[n-1];
for(int j = 0; j < n; j++)dis[j] = 1e18;
Dijkstra(n-1 , i);
res += dis[0];
res += v[i][3];
ans = min(ans , res);
}
}
cout << (ans >= 1e18 ? -1 : ans) << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
712 KB |
Output is correct |
2 |
Correct |
10 ms |
588 KB |
Output is correct |
3 |
Correct |
8 ms |
716 KB |
Output is correct |
4 |
Correct |
9 ms |
732 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
8 ms |
588 KB |
Output is correct |
7 |
Correct |
0 ms |
216 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
11 ms |
716 KB |
Output is correct |
11 |
Correct |
18 ms |
716 KB |
Output is correct |
12 |
Correct |
8 ms |
716 KB |
Output is correct |
13 |
Correct |
8 ms |
716 KB |
Output is correct |
14 |
Correct |
12 ms |
728 KB |
Output is correct |
15 |
Correct |
12 ms |
728 KB |
Output is correct |
16 |
Correct |
12 ms |
732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5132 KB |
Output is correct |
2 |
Correct |
26 ms |
5112 KB |
Output is correct |
3 |
Correct |
27 ms |
5240 KB |
Output is correct |
4 |
Correct |
9 ms |
716 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
11 ms |
696 KB |
Output is correct |
7 |
Correct |
9 ms |
588 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
35 ms |
5308 KB |
Output is correct |
10 |
Correct |
28 ms |
5308 KB |
Output is correct |
11 |
Correct |
26 ms |
5272 KB |
Output is correct |
12 |
Correct |
26 ms |
5316 KB |
Output is correct |
13 |
Correct |
27 ms |
5388 KB |
Output is correct |
14 |
Correct |
26 ms |
5556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
720 KB |
Output is correct |
2 |
Correct |
8 ms |
588 KB |
Output is correct |
3 |
Correct |
20 ms |
4672 KB |
Output is correct |
4 |
Correct |
8 ms |
588 KB |
Output is correct |
5 |
Correct |
29 ms |
4932 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
27 ms |
5048 KB |
Output is correct |
9 |
Correct |
29 ms |
4996 KB |
Output is correct |
10 |
Correct |
25 ms |
5000 KB |
Output is correct |
11 |
Correct |
24 ms |
5052 KB |
Output is correct |
12 |
Correct |
26 ms |
5068 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
30 ms |
5200 KB |
Output is correct |
20 |
Correct |
25 ms |
5052 KB |
Output is correct |
21 |
Correct |
28 ms |
5096 KB |
Output is correct |
22 |
Correct |
24 ms |
4924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
712 KB |
Output is correct |
2 |
Correct |
10 ms |
588 KB |
Output is correct |
3 |
Correct |
8 ms |
716 KB |
Output is correct |
4 |
Correct |
9 ms |
732 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
8 ms |
588 KB |
Output is correct |
7 |
Correct |
0 ms |
216 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
11 ms |
716 KB |
Output is correct |
11 |
Correct |
18 ms |
716 KB |
Output is correct |
12 |
Correct |
8 ms |
716 KB |
Output is correct |
13 |
Correct |
8 ms |
716 KB |
Output is correct |
14 |
Correct |
12 ms |
728 KB |
Output is correct |
15 |
Correct |
12 ms |
728 KB |
Output is correct |
16 |
Correct |
12 ms |
732 KB |
Output is correct |
17 |
Correct |
28 ms |
5132 KB |
Output is correct |
18 |
Correct |
26 ms |
5112 KB |
Output is correct |
19 |
Correct |
27 ms |
5240 KB |
Output is correct |
20 |
Correct |
9 ms |
716 KB |
Output is correct |
21 |
Correct |
8 ms |
724 KB |
Output is correct |
22 |
Correct |
11 ms |
696 KB |
Output is correct |
23 |
Correct |
9 ms |
588 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
35 ms |
5308 KB |
Output is correct |
26 |
Correct |
28 ms |
5308 KB |
Output is correct |
27 |
Correct |
26 ms |
5272 KB |
Output is correct |
28 |
Correct |
26 ms |
5316 KB |
Output is correct |
29 |
Correct |
27 ms |
5388 KB |
Output is correct |
30 |
Correct |
26 ms |
5556 KB |
Output is correct |
31 |
Correct |
8 ms |
720 KB |
Output is correct |
32 |
Correct |
8 ms |
588 KB |
Output is correct |
33 |
Correct |
20 ms |
4672 KB |
Output is correct |
34 |
Correct |
8 ms |
588 KB |
Output is correct |
35 |
Correct |
29 ms |
4932 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
27 ms |
5048 KB |
Output is correct |
39 |
Correct |
29 ms |
4996 KB |
Output is correct |
40 |
Correct |
25 ms |
5000 KB |
Output is correct |
41 |
Correct |
24 ms |
5052 KB |
Output is correct |
42 |
Correct |
26 ms |
5068 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
0 ms |
204 KB |
Output is correct |
45 |
Correct |
0 ms |
320 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
30 ms |
5200 KB |
Output is correct |
50 |
Correct |
25 ms |
5052 KB |
Output is correct |
51 |
Correct |
28 ms |
5096 KB |
Output is correct |
52 |
Correct |
24 ms |
4924 KB |
Output is correct |
53 |
Correct |
33 ms |
5044 KB |
Output is correct |
54 |
Correct |
31 ms |
5056 KB |
Output is correct |
55 |
Correct |
26 ms |
4920 KB |
Output is correct |
56 |
Correct |
10 ms |
716 KB |
Output is correct |
57 |
Correct |
9 ms |
724 KB |
Output is correct |
58 |
Correct |
28 ms |
4836 KB |
Output is correct |
59 |
Correct |
24 ms |
4780 KB |
Output is correct |
60 |
Correct |
24 ms |
4808 KB |
Output is correct |
61 |
Correct |
24 ms |
4784 KB |
Output is correct |
62 |
Correct |
47 ms |
4792 KB |
Output is correct |
63 |
Correct |
75 ms |
4868 KB |
Output is correct |
64 |
Correct |
77 ms |
4796 KB |
Output is correct |
65 |
Correct |
172 ms |
4800 KB |
Output is correct |
66 |
Correct |
293 ms |
4888 KB |
Output is correct |
67 |
Correct |
14 ms |
4116 KB |
Output is correct |
68 |
Correct |
34 ms |
5296 KB |
Output is correct |
69 |
Correct |
30 ms |
5316 KB |
Output is correct |
70 |
Correct |
31 ms |
5192 KB |
Output is correct |
71 |
Correct |
28 ms |
5300 KB |
Output is correct |
72 |
Correct |
30 ms |
5096 KB |
Output is correct |
73 |
Correct |
27 ms |
5136 KB |
Output is correct |
74 |
Correct |
28 ms |
5224 KB |
Output is correct |