結果

問題 No.386 貪欲な領主
ユーザー kotamanegikotamanegi
提出日時 2016-07-02 00:23:43
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,917 bytes
コンパイル時間 1,057 ms
コンパイル使用メモリ 92,320 KB
実行使用メモリ 28,284 KB
最終ジャッジ日時 2024-04-20 21:23:30
合計ジャッジ時間 4,989 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
18,816 KB
testcase_01 AC 10 ms
13,056 KB
testcase_02 AC 10 ms
13,056 KB
testcase_03 AC 9 ms
13,184 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:35:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   35 |                 scanf("%d%d",&a,&b);
      |                 ~~~~~^~~~~~~~~~~~~~
main.cpp:40:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   40 |                 scanf("%d", &kazu[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~~
main.cpp:63:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   63 |                 scanf("%d%d%d", &a, &b, &c);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include<map>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <iomanip>
using namespace std;
#define MAX_MOD 1000000007
#define REP(i,n) for(int i = 0;i < n;++i)
vector<int> sectors_first[200001];
vector<int> sectors_cal[200001];
int kazu[200001] = {};
long long score[200001] = {};
int main() {
	int n;
	cin >> n;
	for (int i = 0;i < n-1;++i) {
		int a, b;
		scanf("%d%d",&a,&b);
		sectors_first[a].push_back(b);
		sectors_first[b].push_back(a);
	}
	for (int i = 0;i < n;++i) {
		scanf("%d", &kazu[i]);
	}
	score[150000] = 1;
	score[0] = kazu[0]+1;//1+
	queue<int> next_go;
	next_go.push(0);
	while (next_go.empty() == false) {
		int hoge = next_go.front();
		next_go.pop();
		for (int q = 0;q < sectors_first[hoge].size();++q) {
			if (score[sectors_first[hoge][q]] == 0) {
				sectors_cal[sectors_first[hoge][q]].push_back(hoge);
				score[sectors_first[hoge][q]] = kazu[sectors_first[hoge][q]] + score[hoge];
				next_go.push(sectors_first[hoge][q]);
			}
		}
	}
	sectors_cal[0].push_back(150000);
	long long ans = 0;
	int m;
	cin >> m;
	for (int i = 0;i < m;++i) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		long long pre_ans = score[a] + score[b] - 2;
		bool ok[200000] = {};
		queue<int> hogee;
		hogee.push(a);
		hogee.push(b);
		while (hogee.empty() == false) {
			int wow = hogee.front();hogee.pop();
			if (ok[wow] == false) {
				ok[wow] = true;
				if (wow != 0) {
					hogee.push(sectors_cal[wow][0]);
				}
			}else{
				pre_ans -= score[wow] - 1+score[sectors_cal[wow][0]]-1;
				goto ok;
			}
		}
	ok:;
		ans += pre_ans * c;
	}
	cout << ans << endl;
}
0