結果

問題 No.386 貪欲な領主
コンテスト
ユーザー horiesiniti
提出日時 2016-06-16 13:04:56
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 833 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 533 ms
コンパイル使用メモリ 96,276 KB
実行使用メモリ 37,792 KB
最終ジャッジ日時 2026-04-28 13:31:56
合計ジャッジ時間 4,373 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 TLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>
std::map<int,std::set<int> > tree;
std::vector<int> cost;

int f(int oya,int p,int g,int sum){
	if (p==g){
		return sum+cost[p];
	}else{
		std::set<int>::iterator it;
		for(it=tree[p].begin();it!=tree[p].end();it++){
			int p2=(*it);
			int res=0;
			if (oya!=p2) {
				res=f(p,p2,g,sum+cost[p]);
			}
			if (res>0){
				return res;
			}
		}
	}
	return 0;
}


int main() {
	// your code goes here
	int n,a,b;
	scanf("%d",&n);
	for(int i=0;i<n-1;i++){
		scanf("%d %d",&a,&b);
		tree[a].insert(b);
		tree[b].insert(a);
	}
	
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		cost.push_back(a);
	}
	int m,s,g,c,ans=0;
	scanf("%d",&m);
	for(int i=0;i<m;i++){
		scanf("%d %d %d",&s,&g,&c);
		int t=f(-1,s,g,0);
		ans+=t*c;
	}
	printf("%d\n",ans);
	return 0;
}
0