結果

問題 No.417 チューリップバブル
ユーザー myantamyanta
提出日時 2017-05-31 08:33:38
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,633 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 61,300 KB
実行使用メモリ 814,008 KB
最終ジャッジ日時 2023-10-21 19:00:56
合計ジャッジ時間 4,931 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,348 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 MLE -
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:108:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  108 |                 for(int i=0;i<n;i++) scanf("%d", &u[i]);
      |                                      ~~~~~^~~~~~~~~~~~~
main.cpp:114:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  114 |                         scanf("%d%d%d", &a, &b, &c);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘int solve(vvpii&, vi&, int)’:
main.cpp:88:39: warning: ‘nr’ may be used uninitialized [-Wmaybe-uninitialized]
   88 |                                 q.push(np, nr, nc, f);
      |                                 ~~~~~~^~~~~~~~~~~~~~~

ソースコード

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;
	vi f;
	bool operator<(const q_t& rhs) const
	{
		return c<rhs.c;
	}
};


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


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

	f.assign(n, 0);

	f[0]=1;
	q.push(p, r, c, f);
	while(q.pop(p, r, c, f))
	{
//printf("p=%d r=%d c=%d\n", p, r, c);
		if(c<0) break;
		if(ret<r) ret=r;

		for(int i=0;i<v[p].size();i++)
		{
			int np, nr, nc;
			np=v[p][i].first;
			nc=c-v[p][i].second*2;
			if(f[np]) continue;

			f[np]=1;
			nr=r+u[np];
			q.push(np, nr, nc, f);
			f[np]=0;
		}

		if(p!=0)
		{
			for(int i=0;i<v[0].size();i++)
			{
				int np, nr, nc;
				np=v[0][i].first;
				nc=c-v[0][i].second*2;
				if(f[np]) continue;

				f[np]=1;
				q.push(np, nr, nc, f);
				f[np]=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