結果

問題 No.1341 真ん中を入れ替えて門松列
ユーザー kotatsugamekotatsugame
提出日時 2021-01-15 22:02:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,817 bytes
コンパイル時間 1,016 ms
コンパイル使用メモリ 89,840 KB
実行使用メモリ 489,344 KB
最終ジャッジ日時 2024-05-04 23:50:23
合計ジャッジ時間 4,685 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 55 ms
5,760 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:8:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<algorithm>
using namespace std;
#line 1 "/home/kotatsugame/library/graph/MCF.cpp"
//Minimum Cost Flow O(FE log V)
//Minimum Cost Flow with negative cost O(NE+FE log V)
#line 4 "/home/kotatsugame/library/graph/MCF.cpp"
#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 tm=0;tm<n;tm++)
			{
				bool ch=false;
				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;
							ch=true;
						}
					}
				}
				if(!ch)break;
				assert(tm<n-1);//negative cycle
			}
		}
		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;}
};
#line 5 "a.cpp"
int N;
long M;
int A[3000],B[3000],C[3000];
main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)cin>>A[i]>>B[i]>>C[i];
	MCF<long>P(N*2+2);
	int st=N*2,go=st+1;
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)
	{
		int mi=min(A[j],C[j]);
		int mx=max(A[j],C[j]);
		if(B[i]<mi)P.add_edge(i,N+j,1,(int)1e9-mx);
		else if(mx<B[i])P.add_edge(i,N+j,1,(int)1e9-B[i]);
	}
	for(int i=0;i<N;i++)P.add_edge(st,i,1,0);
	for(int i=0;i<N;i++)P.add_edge(N+i,go,1,0);
	long ans=(long)1e9*N-P.min_cost_flow(st,go,N);
	if(P)
	{
		cout<<"YES"<<endl;
		if(ans>=M)cout<<"KADOMATSU!"<<endl;
		else cout<<"NO"<<endl;
	}
	else cout<<"NO"<<endl;
}
0