結果

問題 No.1324 Approximate the Matrix
ユーザー kotatsugamekotatsugame
提出日時 2020-12-21 06:07:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 790 ms / 2,000 ms
コード長 2,490 bytes
コンパイル時間 1,170 ms
コンパイル使用メモリ 87,884 KB
実行使用メモリ 8,872 KB
最終ジャッジ日時 2023-10-21 11:28:00
合計ジャッジ時間 10,267 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 545 ms
8,872 KB
testcase_04 AC 542 ms
8,872 KB
testcase_05 AC 548 ms
8,872 KB
testcase_06 AC 545 ms
8,872 KB
testcase_07 AC 561 ms
8,872 KB
testcase_08 AC 25 ms
5,968 KB
testcase_09 AC 21 ms
4,388 KB
testcase_10 AC 54 ms
5,180 KB
testcase_11 AC 128 ms
6,232 KB
testcase_12 AC 18 ms
4,372 KB
testcase_13 AC 10 ms
4,372 KB
testcase_14 AC 143 ms
7,024 KB
testcase_15 AC 39 ms
5,704 KB
testcase_16 AC 4 ms
4,372 KB
testcase_17 AC 70 ms
4,576 KB
testcase_18 AC 19 ms
4,372 KB
testcase_19 AC 10 ms
4,372 KB
testcase_20 AC 9 ms
4,372 KB
testcase_21 AC 6 ms
4,372 KB
testcase_22 AC 10 ms
5,440 KB
testcase_23 AC 42 ms
4,372 KB
testcase_24 AC 194 ms
6,760 KB
testcase_25 AC 81 ms
4,696 KB
testcase_26 AC 79 ms
4,868 KB
testcase_27 AC 24 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 2 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,372 KB
testcase_33 AC 2 ms
4,372 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 2 ms
4,372 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 781 ms
8,608 KB
testcase_38 AC 788 ms
8,608 KB
testcase_39 AC 790 ms
8,608 KB
testcase_40 AC 780 ms
8,608 KB
testcase_41 AC 780 ms
8,608 KB
testcase_42 AC 16 ms
7,288 KB
testcase_43 AC 16 ms
7,288 KB
testcase_44 AC 16 ms
7,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:95:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   95 | main()
      | ^~~~

ソースコード

diff #

#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_,0),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
	{
		T ret=0;
		bool fst=true;
		while(f>0)
		{
			fill(d.begin(),d.end(),numeric_limits<T>::max());
			d[s]=0;
			if(fst)
			{
				fst=false;
				while(true)
				{
					bool ch=false;
					for(int i=0;i<G.size();i++)if(d[i]<numeric_limits<T>::max())
					{
						for(int j=0;j<G[i].size();j++)
						{
							edge&e=G[i][j];
							if(e.cap>0&&d[e.to]>d[i]+e.cost)
							{
								d[e.to]=d[i]+e.cost;
								pv[e.to]=i;
								pe[e.to]=j;
								ch=true;
							}
						}
					}
					if(!ch)break;
				}
			}
			else
			{
				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],B[200];
int P[200][200];
int ans=0;
main()
{
	cin>>N>>K;
	for(int i=0;i<N;i++)cin>>A[i];
	for(int i=0;i<N;i++)cin>>B[i];
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)
	{
		cin>>P[i][j];
		ans+=P[i][j]*P[i][j];
	}
	MCF<int>Q(N*N+2*N+2);
	int st=N*N+2*N,go=st+1;
	for(int i=0;i<N;i++)
	{
		Q.add_edge(st,N*N+i,A[i],0);
		Q.add_edge(N*N+N+i,go,B[i],0);
	}
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)
	{
		Q.add_edge(N*N+i,i*N+j,K,-2*P[i][j]);
		for(int k=0;k<A[i];k++)Q.add_edge(i*N+j,N*N+N+j,1,2*k+1);
	}
	cout<<ans+Q.min_cost_flow(st,go,K)<<endl;
}
0