#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e3;
const long long INF=1e18L+7;
struct Dp
{
int s;
vector<long long> t[3]; // 0-off,1-on not counted,2-on counted
vector<long long>& operator[](int x)
{
return t[x];
}
};
vector<int> e[N+10];
bool vis[N+10];
int val[N+10];
Dp merge_disjoint(Dp a,Dp b)
{
Dp c;
c.s=a.s+b.s;
for(int i=0;i<3;i++)
{
c[i].resize(c.s+1);
fill(c[i].begin(),c[i].end(),INF);
}
for(int i=0;i<=b.s;i++)
b[0][i]=min({b[0][i],b[1][i],b[2][i]});
for(int i=0;i<=a.s;i++)
{
for(int j=0;j<=b.s;j++)
c[0][i+j]=min(c[0][i+j],a[0][i]+b[0][j]);
}
for(int i=0;i<=c.s;i++)
c[1][i]=c[2][i]=c[0][i];
return c;
}
Dp merge(Dp a,Dp b,int x)
{
Dp c;
c.s=a.s+b.s;
for(int i=0;i<3;i++)
{
c[i].resize(c.s+1);
fill(c[i].begin(),c[i].end(),INF);
}
for(int i=0;i<=a.s;i++)
{
for(int j=0;j<=b.s;j++)
{
// 0
c[0][i+j]=min(c[0][i+j],a[0][i]+b[0][j]);
c[0][i+j]=min(c[0][i+j],a[0][i]+b[2][j]);
// 2
c[2][i+j]=min(c[2][i+j],a[2][i]+b[0][j]);
if(i+j+1<=c.s)
c[2][i+j+1]=min(c[2][i+j+1],a[2][i]+b[1][j]);
c[2][i+j]=min(c[2][i+j],a[2][i]+b[2][j]);
if(i+j+1<=c.s)
c[2][i+j+1]=min(c[2][i+j+1],a[0][i]+val[x]+b[2][j]);
if(i+j+2<=c.s)
c[2][i+j+2]=min(c[2][i+j+2],a[0][i]+val[x]+b[1][j]);
}
}
for(int i=0;i<=c.s;i++)
c[1][i]=min(c[1][i],c[0][i]+val[x]);
return c;
}
Dp dfs(int x)
{
vis[x]=true;
Dp ans;
ans.s=1;
ans[0]={0,INF};
ans[1]={val[x],INF};
ans[2]={INF,INF};
for(auto v:e[x])
{
if(!vis[v])
ans=merge(ans,dfs(v),x);
}
//cerr<<x<<":\n";
//for(int j=0;j<3;j++)
//{
// for(int i=0;i<=ans.s;i++)
// cerr<<ans[j][i]<<" \n"[i==ans.s];
//}
//cerr<<"\n";
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>val[i];
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
e[a].push_back(b);
e[b].push_back(a);
}
Dp ans;
ans.s=0;
for(int i=0;i<3;i++)
ans[i]={0};
for(int i=1;i<=n;i++)
{
if(!vis[i])
ans=merge_disjoint(ans,dfs(i));
}
//cerr<<"ans:\n";
//for(int i=0;i<=n;i++)
// cerr<<ans[0][i]<<" \n"[i==n];
map<long long,int> que;
for(int i=0;i<=n;i++)
que[ans[0][i]]=i;
int q;
cin>>q;
while(q--)
{
long long x;
cin>>x;
cout<<prev(que.upper_bound(x))->se<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
844 KB |
Output is correct |
2 |
Incorrect |
10 ms |
956 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
844 KB |
Output is correct |
2 |
Incorrect |
10 ms |
956 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
1232 KB |
Output is correct |
2 |
Incorrect |
44 ms |
2216 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
844 KB |
Output is correct |
2 |
Incorrect |
10 ms |
956 KB |
Output isn't correct |