結果

問題 No.417 チューリップバブル
ユーザー myantamyanta
提出日時 2017-05-31 03:42:39
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,612 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 69,408 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-21 18:07:05
合計ジャッジ時間 5,693 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,696 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 80 ms
4,348 KB
testcase_09 AC 357 ms
4,348 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:102:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  102 |                 for(int i=0;i<n;i++) scanf("%d", &u[i]);
      |                                      ~~~~~^~~~~~~~~~~~~
main.cpp:108:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  108 |                         scanf("%d%d%d", &a, &b, &c);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include<cstdio>
#include<vector>
#include<queue>
#include<map>


using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vpii=vector<pii>;
using vvpii=vector<vpii>;


struct q_t
{
	int p, r, c;
	vvi f;
	vi fv;
	bool operator<(const q_t& rhs) const
	{
		return r<rhs.r;
	}
};


class pq_t
{
	priority_queue<q_t> q;
public:
	void push(int p, int r, int c, vvi& f, vi& fv)
	{
		q.push((q_t){p, r, c, f, fv});
	}
	int pop(int&p, int&r, int&c, vvi&f, vi&fv)
	{
		if(q.empty()) return 0;
		auto t=q.top();
		q.pop();
		p=t.p;
		r=t.r;
		c=t.c;
		f=t.f;
		fv=t.fv;
		return 1;
	}
};


int solve(vvpii& v, vi& u, int m)
{
	int n=v.size();
	vvi f;
	vi fv;
	int p=0, r=u[0], c=0, ret=-1;
	pq_t q;

	fv.resize(n);
	f.resize(n);
	for(int i=0;i<n;i++) f[i].resize(v[i].size());

	fv[0]=1;
	q.push(p, r, c, f, fv);
	while(q.pop(p, r, c, f, fv))
	{
//printf("p=%d r=%d c=%d\n", p, r, c);
		if(p==0)
		{
			if(ret<r) ret=r;
		}
		for(int i=0;i<v[p].size();i++)
		{
			if(f[p][i]) continue;
			int np, nr, nc;
			nc=c+v[p][i].second;
			if(nc>m) continue;

			f[p][i]=1;
			np=v[p][i].first;
			nr=((fv[np])?r:r+u[np]);
			fv[np]++;
			q.push(np, nr, nc, f, fv);
			fv[np]--;
			f[p][i]=0;
		}
	}
	return ret;
}


int main(void)
{
	int n, m;
	vi u;
	vvpii v;

	while(scanf("%d%d", &n, &m)==2)
	{
		v.clear();
		v.resize(n);
		u.resize(n);
		for(int i=0;i<n;i++) scanf("%d", &u[i]);

		for(int i=0;i<n-1;i++)
		{
			int a, b, c;

			scanf("%d%d%d", &a, &b, &c);
			v[a].push_back(make_pair(b, c));
			v[b].push_back(make_pair(a, c));
		}
		printf("%d\n", solve(v, u, m));
	}

	return 0;
}
0