#include<bits/stdc++.h> using namespace std; int n,k; vector < vector <int> > neighbours; int wina[5000000]; pair <int,int> getCost[5000000]; void bfs() { queue<int> tops; tops.push(1); while(!tops.empty()) { int x = tops.front(); tops.pop(); if (neighbours[x].size() == 0) continue; getCost[neighbours[x][0]].first = getCost[x].first; getCost[neighbours[x][0]].second = getCost[x].second+1; getCost[neighbours[x][1]].first = getCost[x].first+1; getCost[neighbours[x][1]].second = getCost[x].second; tops.push(neighbours[x][0]); tops.push(neighbours[x][1]); } } int minValue() { int result = wina[1]; for (int i=1; i<(n-1)*(n-1); ++i) { if (k >= getCost[i].first*getCost[i].second - 1) { result = min(result, wina[i]); } } return result; } int main() { scanf("%d%d",&n,&k); neighbours.resize(n*n+2); for(int row = 0; row < n; ++row) { for(int column=0; column < row+1; ++column) { int value; scanf("%d",&value); wina[(row)*(row+1)/2+column+1] = value; } } for (int row = 0; row < n-1; ++row) { for (int column = 0; column < row+1; ++column) { neighbours[(row)*(row+1)/2+column+1].push_back((row+1)*(row+2)/2+column+1); neighbours[(row)*(row+1)/2+column+1].push_back((row+1)*(row+2)/2+column+2); } } getCost[1] = {1,1}; bfs(); printf("%d", minValue()); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include<bits/stdc++.h> using namespace std; int n,k; vector < vector <int> > neighbours; int wina[5000000]; pair <int,int> getCost[5000000]; void bfs() { queue<int> tops; tops.push(1); while(!tops.empty()) { int x = tops.front(); tops.pop(); if (neighbours[x].size() == 0) continue; getCost[neighbours[x][0]].first = getCost[x].first; getCost[neighbours[x][0]].second = getCost[x].second+1; getCost[neighbours[x][1]].first = getCost[x].first+1; getCost[neighbours[x][1]].second = getCost[x].second; tops.push(neighbours[x][0]); tops.push(neighbours[x][1]); } } int minValue() { int result = wina[1]; for (int i=1; i<(n-1)*(n-1); ++i) { if (k >= getCost[i].first*getCost[i].second - 1) { result = min(result, wina[i]); } } return result; } int main() { scanf("%d%d",&n,&k); neighbours.resize(n*n+2); for(int row = 0; row < n; ++row) { for(int column=0; column < row+1; ++column) { int value; scanf("%d",&value); wina[(row)*(row+1)/2+column+1] = value; } } for (int row = 0; row < n-1; ++row) { for (int column = 0; column < row+1; ++column) { neighbours[(row)*(row+1)/2+column+1].push_back((row+1)*(row+2)/2+column+1); neighbours[(row)*(row+1)/2+column+1].push_back((row+1)*(row+2)/2+column+2); } } getCost[1] = {1,1}; bfs(); printf("%d", minValue()); return 0; } |