#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
const ll inf = 1e18;
struct edge{
ll x, y, z, d, id;
};
struct simple{
ll x, y, id;
};
ll n, m;
edge e[50009];
vector<simple> out[209];
void dijkstra1(ll beg, ll used[], vector<simple> g[], ll end){
for(ll i = 0; i < m; ++i)
used[i] = 0;
vector<pll> prt(n+5);
vector<ll> dis(n+5, inf);
dis[beg] = 0;
priority_queue<pair<pll, pll>, vector<pair<pll, pll>>, greater<pair<pll, pll>>> pq;
pq.push({{0, beg}, {-1, -1}});
while(!pq.empty()){
ll d = pq.top().ff.ff;
ll v = pq.top().ff.ss;
ll comeid = pq.top().ss.ff;
ll comev = pq.top().ss.ss;
pq.pop();
if(dis[v] < d) continue;
if(comeid != -1)
prt[v] = {comeid, comev};
for(auto u : g[v]){
if(dis[u.x] > u.y + d){
dis[u.x] = u.y + d;
pq.push({{dis[u.x], u.x}, {u.id , v}});
}
}
}
if(dis[end] >= inf) return;
ll cur = end;
while(cur != beg){
used[prt[cur].ff] = 1;
cur = prt[cur].ss;
}
}
ll dijkstra2(ll beg, ll dont, vector<simple> g[], ll end){
vector<ll> dis(n+5, inf);
dis[beg] = 0;
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push({0, beg});
while(!pq.empty()){
ll d = pq.top().ff;
ll v = pq.top().ss;
pq.pop();
if(dis[v] < d) continue;
for(auto u : g[v]){
if(u.id == dont) continue;
if(dis[u.x] > u.y + d){
dis[u.x] = u.y + d;
pq.push({dis[u.x], u.x});
}
}
}
return dis[end];
}
ll used[50009];
ll ans[50009];
ll dis[209][209];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n >> m;
for(ll i = 1; i <= n; ++i)
for(ll j = 1; j <= n; ++j)
if(i != j)
dis[i][j] = inf;
for(ll i = 0; i < m; ++i){
cin >> e[i].x >> e[i].y >> e[i].z >> e[i].d;
e[i].id = i;
out[e[i].x].pb({e[i].y, e[i].z, e[i].id});
dis[e[i].x][e[i].y] = min(dis[e[i].x][e[i].y], e[i].z);
ans[i] += e[i].d;
}
for(ll k = 1; k <= n; ++k)
for(ll i = 1; i <= n; ++i)
for(ll j = 1; j <= n; ++j)
dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);
ll fin = dijkstra2(1, -1, out, n)+dijkstra2(n, -1, out, 1);
dijkstra1(1, used, out, n);
for(ll i = 0; i < m; ++i){
if(used[i])
ans[i] += dijkstra2(1, i, out, n);
else
ans[i] += min(dis[1][n], dis[1][e[i].y]+e[i].z+dis[e[i].x][n]);
}
dijkstra1(n, used, out, 1);
for(ll i = 0; i < m; ++i){
if(used[i])
ans[i] += dijkstra2(n, i, out, 1);
else
ans[i] += min(dis[n][1], dis[n][e[i].y]+e[i].z+dis[e[i].x][1]);
}
for(ll i = 0; i < m; ++i)
fin = min(fin, ans[i]);
if(fin >= inf)
cout << -1;
else
cout << fin;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
768 KB |
Output is correct |
2 |
Correct |
13 ms |
768 KB |
Output is correct |
3 |
Correct |
14 ms |
768 KB |
Output is correct |
4 |
Correct |
14 ms |
768 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
15 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
28 ms |
768 KB |
Output is correct |
11 |
Correct |
28 ms |
768 KB |
Output is correct |
12 |
Correct |
28 ms |
768 KB |
Output is correct |
13 |
Correct |
16 ms |
896 KB |
Output is correct |
14 |
Correct |
14 ms |
768 KB |
Output is correct |
15 |
Correct |
14 ms |
768 KB |
Output is correct |
16 |
Correct |
14 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
5420 KB |
Output is correct |
2 |
Correct |
40 ms |
5496 KB |
Output is correct |
3 |
Correct |
38 ms |
5752 KB |
Output is correct |
4 |
Correct |
14 ms |
1024 KB |
Output is correct |
5 |
Correct |
14 ms |
896 KB |
Output is correct |
6 |
Correct |
13 ms |
768 KB |
Output is correct |
7 |
Correct |
14 ms |
768 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
36 ms |
5632 KB |
Output is correct |
10 |
Correct |
39 ms |
5624 KB |
Output is correct |
11 |
Correct |
40 ms |
5500 KB |
Output is correct |
12 |
Correct |
38 ms |
5496 KB |
Output is correct |
13 |
Correct |
49 ms |
5752 KB |
Output is correct |
14 |
Correct |
43 ms |
5752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
928 KB |
Output is correct |
2 |
Correct |
13 ms |
768 KB |
Output is correct |
3 |
Correct |
29 ms |
4472 KB |
Output is correct |
4 |
Correct |
15 ms |
768 KB |
Output is correct |
5 |
Correct |
35 ms |
5496 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
35 ms |
5632 KB |
Output is correct |
9 |
Correct |
34 ms |
5632 KB |
Output is correct |
10 |
Correct |
36 ms |
5624 KB |
Output is correct |
11 |
Correct |
35 ms |
5624 KB |
Output is correct |
12 |
Correct |
34 ms |
5400 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
34 ms |
5496 KB |
Output is correct |
20 |
Correct |
36 ms |
5368 KB |
Output is correct |
21 |
Correct |
37 ms |
5624 KB |
Output is correct |
22 |
Correct |
35 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
768 KB |
Output is correct |
2 |
Correct |
13 ms |
768 KB |
Output is correct |
3 |
Correct |
14 ms |
768 KB |
Output is correct |
4 |
Correct |
14 ms |
768 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
15 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
28 ms |
768 KB |
Output is correct |
11 |
Correct |
28 ms |
768 KB |
Output is correct |
12 |
Correct |
28 ms |
768 KB |
Output is correct |
13 |
Correct |
16 ms |
896 KB |
Output is correct |
14 |
Correct |
14 ms |
768 KB |
Output is correct |
15 |
Correct |
14 ms |
768 KB |
Output is correct |
16 |
Correct |
14 ms |
768 KB |
Output is correct |
17 |
Correct |
61 ms |
5420 KB |
Output is correct |
18 |
Correct |
40 ms |
5496 KB |
Output is correct |
19 |
Correct |
38 ms |
5752 KB |
Output is correct |
20 |
Correct |
14 ms |
1024 KB |
Output is correct |
21 |
Correct |
14 ms |
896 KB |
Output is correct |
22 |
Correct |
13 ms |
768 KB |
Output is correct |
23 |
Correct |
14 ms |
768 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
36 ms |
5632 KB |
Output is correct |
26 |
Correct |
39 ms |
5624 KB |
Output is correct |
27 |
Correct |
40 ms |
5500 KB |
Output is correct |
28 |
Correct |
38 ms |
5496 KB |
Output is correct |
29 |
Correct |
49 ms |
5752 KB |
Output is correct |
30 |
Correct |
43 ms |
5752 KB |
Output is correct |
31 |
Correct |
15 ms |
928 KB |
Output is correct |
32 |
Correct |
13 ms |
768 KB |
Output is correct |
33 |
Correct |
29 ms |
4472 KB |
Output is correct |
34 |
Correct |
15 ms |
768 KB |
Output is correct |
35 |
Correct |
35 ms |
5496 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
35 ms |
5632 KB |
Output is correct |
39 |
Correct |
34 ms |
5632 KB |
Output is correct |
40 |
Correct |
36 ms |
5624 KB |
Output is correct |
41 |
Correct |
35 ms |
5624 KB |
Output is correct |
42 |
Correct |
34 ms |
5400 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
4 ms |
384 KB |
Output is correct |
48 |
Correct |
4 ms |
384 KB |
Output is correct |
49 |
Correct |
34 ms |
5496 KB |
Output is correct |
50 |
Correct |
36 ms |
5368 KB |
Output is correct |
51 |
Correct |
37 ms |
5624 KB |
Output is correct |
52 |
Correct |
35 ms |
5368 KB |
Output is correct |
53 |
Correct |
39 ms |
6144 KB |
Output is correct |
54 |
Correct |
38 ms |
6268 KB |
Output is correct |
55 |
Correct |
40 ms |
6136 KB |
Output is correct |
56 |
Correct |
14 ms |
768 KB |
Output is correct |
57 |
Correct |
17 ms |
768 KB |
Output is correct |
58 |
Correct |
98 ms |
4984 KB |
Output is correct |
59 |
Correct |
105 ms |
5112 KB |
Output is correct |
60 |
Correct |
106 ms |
5112 KB |
Output is correct |
61 |
Correct |
81 ms |
4984 KB |
Output is correct |
62 |
Correct |
100 ms |
4992 KB |
Output is correct |
63 |
Correct |
105 ms |
4992 KB |
Output is correct |
64 |
Correct |
670 ms |
5884 KB |
Output is correct |
65 |
Correct |
613 ms |
5988 KB |
Output is correct |
66 |
Correct |
582 ms |
5872 KB |
Output is correct |
67 |
Correct |
22 ms |
4468 KB |
Output is correct |
68 |
Correct |
41 ms |
6520 KB |
Output is correct |
69 |
Correct |
41 ms |
6520 KB |
Output is correct |
70 |
Correct |
40 ms |
6528 KB |
Output is correct |
71 |
Correct |
42 ms |
6520 KB |
Output is correct |
72 |
Correct |
38 ms |
6264 KB |
Output is correct |
73 |
Correct |
40 ms |
6520 KB |
Output is correct |
74 |
Correct |
41 ms |
6264 KB |
Output is correct |