이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ST first
#define ND second
#define PB push_back
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 1000 + 10, INF = 1e9 + 10;
int n, m;
vi V[nax];
bool visited[nax];
vi dp[nax];
vi withme[nax];
int cost[nax];
void dfs(int x) {
visited[x] = true;
dp[x].PB(0);
withme[x].PB(INF);
dp[x].PB(INF);
withme[x].PB(INF);
for(int nbh : V[x]) if(!visited[nbh]) {
dfs(nbh);
int me = (int)dp[x].size(), he = (int)dp[nbh].size();
vi dp2(me + he - 1);
vi withme2(me + he - 1);
for(int i = 0; i < me + he - 1; ++i) {
if(i < me) {
dp2[i] = dp[x][i];
withme2[i] = withme[x][i];
} else {
dp2[i] = INF;
withme2[i] = INF;
}
}
for(int cnt1 = 0; cnt1 < me; ++cnt1) {
for(int cnt2 = 0; cnt2 < he; ++cnt2) {
withme2[cnt1 + cnt2] = min(withme2[cnt1 + cnt2], withme[x][cnt1] + min(withme[nbh][cnt2], dp[nbh][cnt2]));
if(cnt2 > 0 && cnt1 > 0) withme2[cnt1 + cnt2] = min(withme2[cnt1 + cnt2], dp[x][cnt1 - 1] + dp[nbh][cnt2 - 1] + cost[nbh] + cost[x]);
if(cnt1 > 0) withme2[cnt1 + cnt2] = min(withme2[cnt1 + cnt2], dp[x][cnt1 - 1] + withme[nbh][cnt2] + cost[x]);
if(cnt2 > 0) withme2[cnt1 + cnt2] = min(withme2[cnt1 + cnt2], withme[x][cnt1] + dp[nbh][cnt2 - 1] + cost[nbh]);
}
}
for(int cnt1 = 0; cnt1 < me; ++cnt1) {
for(int cnt2 = 0; cnt2 < he; ++cnt2) {
dp2[cnt1 + cnt2] = min(dp2[cnt1 + cnt2], dp[x][cnt1] + min(withme[nbh][cnt2], dp[nbh][cnt2]));
}
}
withme[nbh].clear();
dp[nbh].clear();
dp[x].swap(dp2);
withme[x].swap(withme2);
}
//cout << x << ": ";
//for(int y : dp[x]) cout << y << " ";
//cout << "\n";
//cout << x << ": ";
//for(int y : withme[x]) cout << y << " ";
//cout << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
cin >> cost[i];
}
for(int a, b, i = 1; i <= m; ++i) {
cin >> a >> b;
V[a].PB(b);
V[b].PB(a);
}
dfs(1);
for(int i = 0; i < (int)dp[1].size(); ++i) {
dp[1][i] = min(dp[1][i], withme[1][i]);
}
for(int i = (int)dp[1].size() - 2; i >= 0; --i) {
dp[1][i] = min(dp[1][i], dp[1][i + 1]);
}
int q;
cin >> q;
while(q--) {
int x;
cin >> x;
int l = 0, r = n, mid;
while(l <= r) {
mid = (l + r)/2;
if(dp[1][mid] <= x) {
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << l - 1 << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |