#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e12 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const ll MAX_N = 2e2 + 10, MAX_W = 1e5 + 10;
vector<pair<pair<int, int>, pair<ll, ll> > > edges;
bitset<MAX_W> off;
vector<pair<pair<int, ll>, int> > g[MAX_N];
ll dist[2][MAX_N];
int n, m;
void dij(ll ind, ll start) {
for(ll i = 0; i < MAX_N; i ++) {
dist[ind][i] = mod;
}
priority_queue<pair<ll, ll> > pq;
pq.push({0, start});
dist[ind][start] = 0;
while(!pq.empty()) {
auto curr = pq.top(); pq.pop();
curr.first *= -1;
if(curr.first != dist[ind][curr.second]) {continue;}
for(auto it : g[curr.second]) {
if(off[it.second]) {continue;}
ll nxt = it.first.first;
if(dist[ind][nxt] > curr.first + it.first.second) {
dist[ind][nxt] = curr.first + it.first.second;
pq.push({-dist[ind][nxt], nxt});
}
}
}
return;
cout << "Started from " << start << endl;
for(ll i = 1; i <= n; i ++) {
cout << dist[ind][i] << " ";
}
cout << endl;
}
ll distFull[MAX_N][MAX_N];
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
for(ll i = 0; i < MAX_N; i ++) {
for(ll j = 0; j < MAX_N; j ++) {
distFull[i][j] = mod;
}
}
for(ll i = 0; i < MAX_N; i ++) {
distFull[i][i] = 0;
}
cin >> n >> m;
edges.resize(m);
for(ll i = 0; i < m; i ++) {
cin >> edges[i];
}
random_shuffle(edges.begin(), edges.end());
for(int i = 0; i < m; i ++) {
off[2 * i] = false;
off[2 * i + 1] = true;
g[edges[i].first.first].push_back({{edges[i].first.second, edges[i].second.first}, 2 * i});
g[edges[i].first.second].push_back({{edges[i].first.first, edges[i].second.first}, 2 * i + 1});
chkmin(distFull[edges[i].first.first][edges[i].first.second], edges[i].second.first);
}
for(ll k = 1; k <= n; k ++) {
for(ll i = 1; i <= n; i ++) {
for(ll j = 1; j <= n; j ++) {
chkmin(distFull[i][j], distFull[i][k] + distFull[k][j]);
}
}
}
ll ret = distFull[1][n] + distFull[n][1];
vector<pair<ll, int> > indices;
for(int i = 0; i < m; i ++) {
ll u = edges[i].first.first, v = edges[i].first.second, cost = edges[i].second.first, price = edges[i].second.second;
ll firstHalf = min(distFull[1][v] + cost + distFull[u][n], (ll)distFull[1][n]);
ll secondHalf = min(distFull[n][v] + cost + distFull[u][1], (ll)distFull[n][1]);
indices.push_back({firstHalf + secondHalf + price, i});
}
sort(indices.begin(), indices.end());
for(ll i = 0; i < m; i ++) {
int ind = indices[i].second;
if(indices[i].first >= ret) {continue;}
off[2 * ind] = true;
off[2 * ind + 1] = false;
dij(0, 1);
dij(1, n);
chkmin(ret, edges[ind].second.second + dist[0][n] + dist[1][1]);
off[2 * ind] = false;
off[2 * ind + 1] = true;
}
if(ret < mod) {
cout << ret << endl;
} else {
cout << -1 << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
768 KB |
Output is correct |
2 |
Correct |
9 ms |
768 KB |
Output is correct |
3 |
Correct |
11 ms |
768 KB |
Output is correct |
4 |
Correct |
11 ms |
768 KB |
Output is correct |
5 |
Correct |
1 ms |
768 KB |
Output is correct |
6 |
Correct |
9 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
760 KB |
Output is correct |
10 |
Correct |
18 ms |
768 KB |
Output is correct |
11 |
Correct |
28 ms |
896 KB |
Output is correct |
12 |
Correct |
13 ms |
896 KB |
Output is correct |
13 |
Correct |
11 ms |
896 KB |
Output is correct |
14 |
Correct |
11 ms |
896 KB |
Output is correct |
15 |
Correct |
11 ms |
896 KB |
Output is correct |
16 |
Correct |
10 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
6548 KB |
Output is correct |
2 |
Correct |
43 ms |
6300 KB |
Output is correct |
3 |
Correct |
43 ms |
7408 KB |
Output is correct |
4 |
Correct |
12 ms |
1024 KB |
Output is correct |
5 |
Correct |
10 ms |
768 KB |
Output is correct |
6 |
Correct |
10 ms |
768 KB |
Output is correct |
7 |
Correct |
9 ms |
768 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
43 ms |
7536 KB |
Output is correct |
10 |
Correct |
43 ms |
7656 KB |
Output is correct |
11 |
Correct |
42 ms |
7572 KB |
Output is correct |
12 |
Correct |
42 ms |
7544 KB |
Output is correct |
13 |
Correct |
43 ms |
7544 KB |
Output is correct |
14 |
Correct |
42 ms |
8212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
768 KB |
Output is correct |
2 |
Correct |
9 ms |
768 KB |
Output is correct |
3 |
Correct |
31 ms |
5360 KB |
Output is correct |
4 |
Correct |
9 ms |
768 KB |
Output is correct |
5 |
Correct |
36 ms |
6256 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
38 ms |
6256 KB |
Output is correct |
9 |
Correct |
37 ms |
6420 KB |
Output is correct |
10 |
Correct |
37 ms |
6296 KB |
Output is correct |
11 |
Correct |
37 ms |
6452 KB |
Output is correct |
12 |
Correct |
38 ms |
6768 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
38 ms |
6640 KB |
Output is correct |
20 |
Correct |
37 ms |
6392 KB |
Output is correct |
21 |
Correct |
38 ms |
6252 KB |
Output is correct |
22 |
Correct |
38 ms |
6640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
768 KB |
Output is correct |
2 |
Correct |
9 ms |
768 KB |
Output is correct |
3 |
Correct |
11 ms |
768 KB |
Output is correct |
4 |
Correct |
11 ms |
768 KB |
Output is correct |
5 |
Correct |
1 ms |
768 KB |
Output is correct |
6 |
Correct |
9 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
760 KB |
Output is correct |
10 |
Correct |
18 ms |
768 KB |
Output is correct |
11 |
Correct |
28 ms |
896 KB |
Output is correct |
12 |
Correct |
13 ms |
896 KB |
Output is correct |
13 |
Correct |
11 ms |
896 KB |
Output is correct |
14 |
Correct |
11 ms |
896 KB |
Output is correct |
15 |
Correct |
11 ms |
896 KB |
Output is correct |
16 |
Correct |
10 ms |
896 KB |
Output is correct |
17 |
Correct |
42 ms |
6548 KB |
Output is correct |
18 |
Correct |
43 ms |
6300 KB |
Output is correct |
19 |
Correct |
43 ms |
7408 KB |
Output is correct |
20 |
Correct |
12 ms |
1024 KB |
Output is correct |
21 |
Correct |
10 ms |
768 KB |
Output is correct |
22 |
Correct |
10 ms |
768 KB |
Output is correct |
23 |
Correct |
9 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
43 ms |
7536 KB |
Output is correct |
26 |
Correct |
43 ms |
7656 KB |
Output is correct |
27 |
Correct |
42 ms |
7572 KB |
Output is correct |
28 |
Correct |
42 ms |
7544 KB |
Output is correct |
29 |
Correct |
43 ms |
7544 KB |
Output is correct |
30 |
Correct |
42 ms |
8212 KB |
Output is correct |
31 |
Correct |
10 ms |
768 KB |
Output is correct |
32 |
Correct |
9 ms |
768 KB |
Output is correct |
33 |
Correct |
31 ms |
5360 KB |
Output is correct |
34 |
Correct |
9 ms |
768 KB |
Output is correct |
35 |
Correct |
36 ms |
6256 KB |
Output is correct |
36 |
Correct |
1 ms |
640 KB |
Output is correct |
37 |
Correct |
1 ms |
640 KB |
Output is correct |
38 |
Correct |
38 ms |
6256 KB |
Output is correct |
39 |
Correct |
37 ms |
6420 KB |
Output is correct |
40 |
Correct |
37 ms |
6296 KB |
Output is correct |
41 |
Correct |
37 ms |
6452 KB |
Output is correct |
42 |
Correct |
38 ms |
6768 KB |
Output is correct |
43 |
Correct |
1 ms |
640 KB |
Output is correct |
44 |
Correct |
1 ms |
640 KB |
Output is correct |
45 |
Correct |
1 ms |
640 KB |
Output is correct |
46 |
Correct |
1 ms |
640 KB |
Output is correct |
47 |
Correct |
1 ms |
640 KB |
Output is correct |
48 |
Correct |
1 ms |
640 KB |
Output is correct |
49 |
Correct |
38 ms |
6640 KB |
Output is correct |
50 |
Correct |
37 ms |
6392 KB |
Output is correct |
51 |
Correct |
38 ms |
6252 KB |
Output is correct |
52 |
Correct |
38 ms |
6640 KB |
Output is correct |
53 |
Correct |
42 ms |
7188 KB |
Output is correct |
54 |
Correct |
42 ms |
7316 KB |
Output is correct |
55 |
Correct |
42 ms |
7408 KB |
Output is correct |
56 |
Correct |
11 ms |
896 KB |
Output is correct |
57 |
Correct |
15 ms |
896 KB |
Output is correct |
58 |
Correct |
35 ms |
6256 KB |
Output is correct |
59 |
Correct |
37 ms |
6256 KB |
Output is correct |
60 |
Correct |
36 ms |
6256 KB |
Output is correct |
61 |
Correct |
50 ms |
6256 KB |
Output is correct |
62 |
Correct |
138 ms |
6276 KB |
Output is correct |
63 |
Correct |
279 ms |
6276 KB |
Output is correct |
64 |
Correct |
94 ms |
6256 KB |
Output is correct |
65 |
Correct |
214 ms |
6260 KB |
Output is correct |
66 |
Correct |
402 ms |
6256 KB |
Output is correct |
67 |
Correct |
23 ms |
6052 KB |
Output is correct |
68 |
Correct |
42 ms |
7572 KB |
Output is correct |
69 |
Correct |
43 ms |
7700 KB |
Output is correct |
70 |
Correct |
41 ms |
7476 KB |
Output is correct |
71 |
Correct |
42 ms |
7408 KB |
Output is correct |
72 |
Correct |
42 ms |
7476 KB |
Output is correct |
73 |
Correct |
42 ms |
7700 KB |
Output is correct |
74 |
Correct |
43 ms |
7672 KB |
Output is correct |