結果

問題 No.1288 yuki collection
ユーザー kotatsugamekotatsugame
提出日時 2020-11-13 22:11:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,824 ms / 5,000 ms
コード長 2,712 bytes
コンパイル時間 979 ms
コンパイル使用メモリ 93,548 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 03:01:51
合計ジャッジ時間 15,571 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 390 ms
4,380 KB
testcase_14 AC 389 ms
4,380 KB
testcase_15 AC 301 ms
4,376 KB
testcase_16 AC 293 ms
4,380 KB
testcase_17 AC 385 ms
4,380 KB
testcase_18 AC 412 ms
4,376 KB
testcase_19 AC 401 ms
4,376 KB
testcase_20 AC 425 ms
4,380 KB
testcase_21 AC 638 ms
4,380 KB
testcase_22 AC 630 ms
4,380 KB
testcase_23 AC 619 ms
4,376 KB
testcase_24 AC 406 ms
4,380 KB
testcase_25 AC 412 ms
4,380 KB
testcase_26 AC 402 ms
4,376 KB
testcase_27 AC 113 ms
4,376 KB
testcase_28 AC 211 ms
4,380 KB
testcase_29 AC 239 ms
4,376 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 12 ms
4,376 KB
testcase_32 AC 12 ms
4,380 KB
testcase_33 AC 1,036 ms
4,380 KB
testcase_34 AC 1,824 ms
4,376 KB
testcase_35 AC 737 ms
4,376 KB
testcase_36 AC 1,137 ms
4,376 KB
testcase_37 AC 1,307 ms
4,376 KB
testcase_38 AC 29 ms
4,376 KB
testcase_39 AC 28 ms
4,376 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:67:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   67 | 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;
		while(f>0)
		{
			queue<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.front();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;
string s;
int V[2020];
main()
{
	cin>>N>>s;
	for(int i=0;i<N;i++)cin>>V[i];
	vector<pair<pair<int,int>,pair<int,long> > >E;
	int pre[4]={-1,-1,-1,-1};
	int st=N,go=N+1;
	for(int i=N;i--;)
	{
		if(s[i]=='y')
		{
			E.push_back(make_pair(make_pair(st,i),make_pair(1,0)));
			if(pre[1]!=-1)
			{
				E.push_back(make_pair(make_pair(i,pre[1]),make_pair(1,-V[i])));
			}
			pre[0]=i;
		}
		else if(s[i]=='u')
		{
			if(pre[1]!=-1)
			{
				E.push_back(make_pair(make_pair(i,pre[1]),make_pair(N,0)));
			}
			if(pre[2]!=-1)
			{
				E.push_back(make_pair(make_pair(i,pre[2]),make_pair(1,-V[i])));
			}
			pre[1]=i;
		}
		else if(s[i]=='k')
		{
			if(pre[2]!=-1)
			{
				E.push_back(make_pair(make_pair(i,pre[2]),make_pair(N,0)));
			}
			if(pre[3]!=-1)
			{
				E.push_back(make_pair(make_pair(i,pre[3]),make_pair(1,-V[i])));
			}
			pre[2]=i;
		}
		else
		{
			if(pre[3]!=-1)
			{
				E.push_back(make_pair(make_pair(i,pre[3]),make_pair(N,0)));
			}
			E.push_back(make_pair(make_pair(i,go),make_pair(1,-V[i])));
			pre[3]=i;
		}
	}
	MCF<long>P(N+2);
	for(pair<pair<int,int>,pair<int,long> >e:E)P.add_edge(e.first.first,e.first.second,e.second.first,e.second.second);
	long ans=0;
	for(int L=1;;L++)
	{
		long T=P.min_cost_flow(st,go,1);
		if(T>=0)break;
		ans-=T;
	}
	cout<<ans<<endl;
}
0