# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
679798 | ByeWorld | Bosses (BOI16_bosses) | C++14 | 1486 ms | 98656 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("01")
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 5e3+10;
const int MAXA = 4e4+10;
const int LOG = 20;
const int VOD = 0;
const int MOD = 1e9+7;
const int MODD = 1e9+6;
const ll INF = 2e18;
typedef pair<int,int> pii;
typedef pair<pii,pii> ipii;
int k[MAXN];
vector <int> adj[MAXN];
int dis[MAXN][MAXN];//dari j ke i, i <- j
queue <pii> q;
void bfs(int sta, int DIS){
q.push({sta, DIS});
while(!q.empty()){
int nw = q.front().fi; int dist = q.front().se;
q.pop();
if(dis[nw][sta] != -1) continue;
dis[nw][sta] = dist;
for(auto nx : adj[nw]){
q.push({nx, dist+1});
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; scanf("%d", &n);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++) dis[i][j] = -1;
}
for(int i=1; i<=n; i++){
scanf("%d", &k[i]);
for(int j=1; j<=k[i]; j++){
int to; scanf("%d", &to);
adj[i].pb(to);
}
}
for(int i=1; i<=n; i++){
bfs(i, 0);
}
/*for(int i=1; i<=n; i++){
cout << i << '\n';
for(int j=1; j<=n; j++) cout << dis[i][j] << ' ';
cout << '\n';
}*/
ll ans = INF;
for(int i=1; i<=n; i++){
ll sum = 0; bool b = 1;
for(int j=1; j<=n; j++){
if(dis[i][j] == -1){b=0; break;}
sum += dis[i][j];
}
if(!b) continue;
ans = min(ans, sum);
//if(sum == 3) cout << i << '\n';
}
printf("%d", ans+n);
printf("\n");
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |