QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#243943 | #1269. New Equipments | PhantomThreshold# | AC ✓ | 1364ms | 4880kb | C++20 | 3.0kb | 2023-11-08 19:30:55 | 2023-11-08 19:30:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
namespace min_cost_max_flow{
const int MAXN=6411,MAXM=50333;
struct edge{
int v,f,w;
edge *next,*rev;
}*h[MAXN],pool[MAXM*2];
int top;
inline void addedge(int u,int v,int c,int w){
// cerr << "add : " << u << " " << v << " " << c << " " << w << endl;
edge *tmp=&pool[top++];tmp->v=v;tmp->f=c;tmp->w=w;tmp->next=h[u];h[u]=tmp;
edge *pmt=&pool[top++];pmt->v=u;pmt->f=0;pmt->w=-w;pmt->next=h[v];h[v]=pmt;
tmp->rev=pmt;pmt->rev=tmp;
}
deque<int> q;
int S,T;
int last[MAXN],inq[MAXN];
long long dis[MAXN],maxf[MAXN];
edge *laste[MAXN];
int totcost,totflow;
bool SPFA(){
memset(dis,0x3f,sizeof(dis));
memset(last,0,sizeof(last));
memset(maxf,0,sizeof(maxf));
memset(inq,0,sizeof(inq));
while(!q.empty())q.pop_front();
q.push_front(S);
maxf[S]=0x3f3f3f3f3f3f3f3fll;
inq[S]=1;
dis[S]=0;
while (!q.empty()){
int u=q.front();q.pop_front();
inq[u]=0;
for (edge *tmp=h[u];tmp;tmp=tmp->next){
if (tmp->f && dis[tmp->v]>dis[u]+tmp->w){
dis[tmp->v]=dis[u]+tmp->w;
last[tmp->v]=u;
maxf[tmp->v]=max(maxf[tmp->v],min(maxf[u],(long long)tmp->f));
laste[tmp->v]=tmp;
if (!inq[tmp->v]){
if (q.empty() || dis[q.front()]>tmp->v) q.push_front(tmp->v);
else q.push_back(tmp->v);
inq[tmp->v]=1;
}
}
}
}
if (dis[T]>=0x3f3f3f3f3f3f3f3fll) return false;
int u=T;
while (last[u]){
laste[u]->f-=maxf[T];
laste[u]->rev->f+=maxf[T];
u=last[u];
}
totcost+=dis[T]*maxf[T];
totflow+=maxf[T];
return true;
}
void init(int _S,int _T){
S=_S;
T=_T;
top=0;
memset(h,0,sizeof(h));
totcost=0;
totflow=0;
}
}
typedef long long ll;
const ll lim=51;
ll n,m;
ll a[55],b[55],c[55];
signed main(){
ios_base::sync_with_stdio(false);
int Tcase=1;
cin >> Tcase;
for (;Tcase--;){
cin >> n >> m;
for (int i=1;i<=n;i++) cin >> a[i] >> b[i] >> c[i];
map<ll,ll> dict;
for (int i=1;i<=n;i++){
ll l=1,r=m;
ll pos=-b[i]/(2*a[i]);
pos=max(pos,l);
pos=min(pos,r);
l=max(l,pos-lim);
r=min(r,pos+lim);
for (ll j=l;j<=r;j++) dict[j]=0;
}
ll id=0;
for (auto &[x,y]:dict) y=++id;
int _S=n+id+1;
int _T=n+id+2;
min_cost_max_flow::init(_S,_T);
for (int i=1;i<=n;i++) min_cost_max_flow::addedge(_S,i,1,0);
for (int j=1;j<=id;j++) min_cost_max_flow::addedge(j+n,_T,1,0);
for (int i=1;i<=n;i++){
ll l=1,r=m;
ll pos=-b[i]/(2*a[i]);
pos=max(pos,l);
pos=min(pos,r);
l=max(l,pos-lim);
r=min(r,pos+lim);
for (int j=l;j<=r;j++){
ll numj=dict[j];
min_cost_max_flow::addedge(i,numj+n,1,a[i]*j*j+b[i]*j+c[i]);
}
}
vector<long long> ans;
ans.push_back(0LL);
for (int i=1;i<=n;i++){
min_cost_max_flow::SPFA();
// cerr << min_cost_max_flow::totflow << " " << min_cost_max_flow::totcost << endl;
ans.push_back(min_cost_max_flow::totcost);
}
for (int i=1;i<=n;i++) cout << ans[i] << " \n"[i==n];
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3684kb
input:
1 3 5 2 3 10 2 -3 10 1 -1 4
output:
4 15 37
result:
ok single line: '4 15 37'
Test #2:
score: 0
Accepted
time: 1364ms
memory: 4880kb
input:
10 50 50 2 -16 79 8 -21 54 8 -1 3 1 -7 47 5 -20 89 1 -2 47 2 -10 26 10 31 28 2 -16 37 6 -16 44 2 -8 100 3 -26 65 3 -6 91 10 -33 56 2 -7 22 2 -12 74 1 -3 7 7 -30 51 1 -4 8 1 -10 62 2 -5 5 1 -3 38 7 -32 57 4 -24 65 1 -8 97 7 -28 71 5 -13 71 2 -14 49 6 -33 100 2 7 69 8 -22 38 5 -23 88 7 20 57 7 -11 83 ...
output:
2 4 9 14 24 40 72 117 170 239 327 445 592 771 972 1202 1467 1772 2117 2506 2954 3461 4035 4690 5415 6223 7137 8145 9272 10499 11858 13366 15003 16798 18736 20810 23058 25517 28172 31062 34194 37566 41209 45136 49371 53924 58847 64143 69813 75884 4 9 17 25 33 43 57 78 107 146 194 266 356 461 590 744 ...
result:
ok 10 lines