# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
646245 |
2022-09-29T10:01:01 Z |
fatemetmhr |
Cities (BOI16_cities) |
C++17 |
|
6000 ms |
26916 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define pb push_back
#define fi first
#define se second
#define mp make_pair
const int maxn5 = 2e5 + 10;
const int maxn3 = 1e3 + 10;
const ll inf = 1e18;
int n;
vector <int> imp;
vector <pair<int, ll>> adj[maxn5];
set <pair<ll, int>> av;
ll dis[5][maxn5], dis2[maxn3][maxn3];
inline void dij(int r, int id){
av.clear();
for(int i = 0; i < n; i++)
dis[id][i] = inf;
dis[id][r] = 0;
for(int i = 0; i < n; i++)
av.insert({dis[id][i], i});
while(av.size()){
int v = av.begin() -> se;
av.erase(av.begin());
for(auto p : adj[v]){
int u = p.fi;
ll w = p.se;
if(dis[id][u] < dis[id][v] + w)
continue;
av.erase({dis[id][u], u});
dis[id][u] = dis[id][v] + w;
av.insert({dis[id][u], u});
}
}
return;
}
inline void dij2(int r, int id){
av.clear();
for(int i = 0; i < n; i++)
dis2[id][i] = inf;
dis2[id][r] = 0;
for(int i = 0; i < n; i++)
av.insert({dis2[id][i], i});
while(av.size()){
int v = av.begin() -> se;
av.erase(av.begin());
for(auto p : adj[v]){
int u = p.fi;
ll w = p.se;
if(dis2[id][u] < dis2[id][v] + w)
continue;
av.erase({dis2[id][u], u});
dis2[id][u] = dis2[id][v] + w;
av.insert({dis2[id][u], u});
}
}
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int k, m; cin >> n >> k >> m;
for(int i = 0; i < k; i++){
int a; cin >> a; a--;
imp.pb(a);
}
for(int i = 0; i < m; i++){
int a, b, c; cin >> a >> b >> c;
a--; b--;
adj[a].pb({b, c});
adj[b].pb({a, c});
}
for(int i = 0; i < k; i++)
dij(imp[i], i);
if(k == 2){
cout << dis[0][imp[1]] << endl;
return 0;
}
if(k == 3){
ll ans = dis[0][imp[1]] + dis[1][imp[2]];
for(int i = 0; i < n; i++)
ans = min(ans, dis[0][i] + dis[1][i] + dis[2][i]);
return cout << ans << endl, 0;
}
for(int i = 0; i < n; i++)
dij2(i, i);
ll ans = inf;
//cout << "check " << endl;
int a0 = imp[0], a1 = imp[1], a2 = imp[2], a3 = imp[3];
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){
ans = min(ans, dis2[a0][i] + dis2[a1][i] + dis2[a2][j] + dis2[a3][j] + dis2[i][j]);
ans = min(ans, dis2[a0][i] + dis2[a1][j] + dis2[a2][i] + dis2[a3][j] + dis2[i][j]);
ans = min(ans, dis2[a0][i] + dis2[a1][j] + dis2[a2][j] + dis2[a3][i] + dis2[i][j]);
//cout << i << ' ' << j << ' ' << ans << endl;
}
//for(auto u : imp)
// cout << u << ' ' << dis2[u][0] << endl;
//cout << dis2[0][1] << ' ' << dis2[0][2] << ' ' << dis2[0][3] << ' ' << dis2[0][4] << ' ' << dis2[0][5] << endl;
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
593 ms |
23876 KB |
Output is correct |
2 |
Correct |
567 ms |
23108 KB |
Output is correct |
3 |
Correct |
302 ms |
19204 KB |
Output is correct |
4 |
Correct |
59 ms |
14124 KB |
Output is correct |
5 |
Correct |
398 ms |
22988 KB |
Output is correct |
6 |
Correct |
61 ms |
14060 KB |
Output is correct |
7 |
Correct |
5 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
480 ms |
13144 KB |
Output is correct |
2 |
Correct |
474 ms |
13128 KB |
Output is correct |
3 |
Correct |
307 ms |
13092 KB |
Output is correct |
4 |
Correct |
148 ms |
9056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
6043 ms |
25776 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
6044 ms |
26916 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |