結果
| 問題 | No.1324 Approximate the Matrix |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-21 06:23:54 |
| 言語 | C++14 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 81 ms / 2,000 ms |
| コード長 | 2,325 bytes |
| 記録 | |
| コンパイル時間 | 973 ms |
| コンパイル使用メモリ | 87,212 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-21 12:41:13 |
| 合計ジャッジ時間 | 3,258 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
コンパイルメッセージ
main.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
87 | main()
| ^~~~
ソースコード
#include<iostream>
using namespace std;
//Minimum Cost Flow O(FE log V)
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<limits>
template<typename T>
struct MCF{
struct edge{
int to,rev,cap;
T cost;
};
vector<vector<edge> >G;
vector<T>h,d;
vector<int>pv,pe;
MCF(int n_=0):G(n_),h(n_),d(n_),pv(n_),pe(n_){}
void add_edge(int from,int to,int cap,T cost)
{
G[from].push_back({to,(int)G[to].size(),cap,cost});
G[to].push_back({from,(int)G[from].size()-1,0,-cost});
}
T min_cost_flow(int s,int t,int f)//ans or -1
{
fill(h.begin(),h.end(),numeric_limits<T>::max());
h[s]=0;
for(int tm=0;tm<G.size();tm++)
{
bool ch=false;
for(int i=0;i<G.size();i++)if(h[i]<numeric_limits<T>::max())
{
for(int j=0;j<G[i].size();j++)
{
edge&e=G[i][j];
if(e.cap>0&&h[e.to]>h[i]+e.cost)
{
h[e.to]=h[i]+e.cost;
ch=true;
}
}
}
if(!ch)break;
if(tm==G.size()-1)return -1;//negative cycle
}
T ret=0;
while(f>0)
{
fill(d.begin(),d.end(),numeric_limits<T>::max());
d[s]=0;
priority_queue<pair<T,int>,vector<pair<T,int> >,greater<pair<T,int> > >P;
P.push(make_pair(0,s));
while(!P.empty())
{
pair<T,int>p=P.top();P.pop();
if(d[p.second]<p.first)continue;
for(int i=0;i<G[p.second].size();i++)
{
edge&e=G[p.second][i];
if(e.cap>0&&d[e.to]>d[p.second]+e.cost+h[p.second]-h[e.to])
{
d[e.to]=d[p.second]+e.cost+h[p.second]-h[e.to];
pv[e.to]=p.second;
pe[e.to]=i;
P.push(make_pair(d[e.to],e.to));
}
}
}
if(d[t]==numeric_limits<T>::max())return -1;
for(int u=0;u<G.size();u++)h[u]+=d[u];
int d=f;
for(int u=t;u!=s;u=pv[u])d=min(d,G[pv[u]][pe[u]].cap);
f-=d;
ret+=d*h[t];
for(int u=t;u!=s;u=pv[u])
{
G[pv[u]][pe[u]].cap-=d;
G[u][G[pv[u]][pe[u]].rev].cap+=d;
}
}
return ret;
}
};
int N,K;
int A[200];
int ans=0;
main()
{
cin>>N>>K;
MCF<int>Q(2*N+2);
int st=2*N,go=st+1;
for(int i=0;i<N;i++)
{
cin>>A[i];
Q.add_edge(st,i,A[i],0);
}
for(int i=0;i<N;i++)
{
int B;
cin>>B;
Q.add_edge(N+i,go,B,0);
}
for(int i=0;i<N;i++)for(int j=0;j<N;j++)
{
int P;
cin>>P;
ans+=P*P;
for(int k=1;k<=A[i];k++)Q.add_edge(i,N+j,1,(P-k)*(P-k)-(P-k+1)*(P-k+1));
}
cout<<ans+Q.min_cost_flow(st,go,K)<<endl;
}