結果

問題 No.1678 Coin Trade (Multiple)
ユーザー kotatsugamekotatsugame
提出日時 2021-09-10 22:00:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,184 ms / 5,000 ms
コード長 2,262 bytes
コンパイル時間 961 ms
コンパイル使用メモリ 89,096 KB
実行使用メモリ 14,420 KB
最終ジャッジ日時 2023-09-02 17:33:22
合計ジャッジ時間 20,100 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 103 ms
9,828 KB
testcase_04 AC 568 ms
12,396 KB
testcase_05 AC 234 ms
13,812 KB
testcase_06 AC 175 ms
13,304 KB
testcase_07 AC 517 ms
10,620 KB
testcase_08 AC 342 ms
11,332 KB
testcase_09 AC 257 ms
14,072 KB
testcase_10 AC 110 ms
7,300 KB
testcase_11 AC 399 ms
12,124 KB
testcase_12 AC 96 ms
7,112 KB
testcase_13 AC 897 ms
13,368 KB
testcase_14 AC 285 ms
9,728 KB
testcase_15 AC 304 ms
12,204 KB
testcase_16 AC 81 ms
11,736 KB
testcase_17 AC 262 ms
14,120 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 1 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 2 ms
4,368 KB
testcase_25 AC 2 ms
4,372 KB
testcase_26 AC 1 ms
4,368 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 2 ms
4,368 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 2 ms
4,368 KB
testcase_31 AC 2 ms
4,368 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 2 ms
4,368 KB
testcase_34 AC 1 ms
4,372 KB
testcase_35 AC 2 ms
4,368 KB
testcase_36 AC 2 ms
4,368 KB
testcase_37 AC 2 ms
4,372 KB
testcase_38 AC 2 ms
4,368 KB
testcase_39 AC 2 ms
4,372 KB
testcase_40 AC 2 ms
4,368 KB
testcase_41 AC 2 ms
4,368 KB
testcase_42 AC 2 ms
4,372 KB
testcase_43 AC 2 ms
4,372 KB
testcase_44 AC 2 ms
4,368 KB
testcase_45 AC 2 ms
4,372 KB
testcase_46 AC 2 ms
4,368 KB
testcase_47 AC 2 ms
4,372 KB
testcase_48 AC 1,157 ms
14,244 KB
testcase_49 AC 1,171 ms
14,188 KB
testcase_50 AC 1,146 ms
14,236 KB
testcase_51 AC 1,184 ms
14,124 KB
testcase_52 AC 1,165 ms
14,188 KB
testcase_53 AC 1,173 ms
14,176 KB
testcase_54 AC 1,145 ms
14,156 KB
testcase_55 AC 1,158 ms
14,420 KB
testcase_56 AC 1,157 ms
14,120 KB
testcase_57 AC 1,147 ms
14,128 KB
testcase_58 AC 418 ms
11,480 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:92:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   92 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//Minimum Cost Flow O(FE log V)
//Minimum Cost Flow with negative cost O(NE+FE log V)
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<limits>
#include<cassert>
template<typename T>
struct MCF{
	struct edge{
		int to,rev,cap;
		T cost;
	};
	int n;
	bool negedge,ok;
	vector<vector<edge> >G;
	vector<T>h,d;
	vector<int>pv,pe;
	MCF(int n_=0):n(n_),negedge(false),G(n_),h(n_),d(n_),pv(n_),pe(n_){}
	void add_edge(int from,int to,int cap,T cost)
	{
		if(cost<0)negedge=true;
		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
	{
		ok=false;
		if(negedge)
		{
			fill(h.begin(),h.end(),numeric_limits<T>::max());
			h[s]=0;
			for(int i=0;i<n;i++)if(h[i]<numeric_limits<T>::max())
			{
				for(const edge&e:G[i])
				{
					if(e.cap>0&&h[e.to]>h[i]+e.cost)
					{
						h[e.to]=h[i]+e.cost;
					}
				}
			}
		}
		T ret=0;
		while(f>0)
		{
			priority_queue<pair<T,int>,vector<pair<T,int> >,greater<pair<T,int> > >P;
			fill(d.begin(),d.end(),numeric_limits<T>::max());
			d[s]=0;
			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;
			}
		}
		ok=true;
		return ret;
	}
	operator bool()const{return ok;}
};
using namespace std;
int N,K;
int A[1<<17];
main()
{
	cin>>N>>K;
	MCF<long>P(N+1);
	for(int i=1;i<=N;i++)
	{
		int M;
		P.add_edge(i-1,i,K,0);
		cin>>A[i]>>M;
		for(int j=0;j<M;j++)
		{
			int u;cin>>u;
			P.add_edge(u,i,1,A[u]-A[i]);
		}
	}
	cout<<-P.min_cost_flow(0,N,K)<<endl;
}
0