提出 #485252


ソースコード 拡げる

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<utility>
#define N 2010
using namespace std;
struct edge{
	edge(int _to,int _w):to(_to),w(_w){}
	int to;
	int w;
};
priority_queue<pair<int,int> > bpq;
vector<edge> g[N];
vector<int> leaf[N];
int dep[N],par[N],sz[N],now[N],c[N];
long long add[N][N];
bool vis[N],del[N];
bool cmp(int i,int j){
	return dep[i]<dep[j];
}
void build(int u,int pre){
	dep[u]=pre;
	vis[u]=true;
	int cnt=0;
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i].to;
		if(vis[v]) continue;
		par[v]=u;
		build(v,pre+g[u][i].w);
		leaf[u].push_back(leaf[v].back());
		cnt++;
	}
	if(!cnt) leaf[u].push_back(u);
	else sort(leaf[u].begin(),leaf[u].end(),cmp);
}
void fix(int u){
	while(par[u]&&!del[u]){
		int p=par[u];
		del[u]=true;
		leaf[p].pop_back();
		if(!leaf[p].empty()) bpq.push(make_pair(dep[leaf[p].back()]-dep[p],leaf[p].back()));
		u=p;
	}
}
void solve1(int u){
	memset(vis,0,sizeof(vis));
	memset(del,0,sizeof(del));
	par[u]=0;
	build(u,0);
	int mx=leaf[u].back();
	bpq.push(make_pair(dep[mx],mx));
	while(!bpq.empty()){
		add[u][sz[u]++]=1LL*bpq.top().first*c[u];
		int tmp=bpq.top().second;
		bpq.pop();		
		fix(tmp);
	}
}
long long solve2(int n,int m){
	long long res=0;
	priority_queue<pair<long long,int> > pq;
	for(int i=1;i<=n;i++){
		if(now[i]<sz[i]) pq.push(make_pair(add[i][0],i)),now[i]++;
	}
	while(!pq.empty()&&m){
		res+=pq.top().first;
		int u=pq.top().second;
		pq.pop();
		if(now[u]<sz[u]){
			pq.push(make_pair(add[u][now[u]++],u));
		}
		m--;
	}
	return res;
}
int main(){
	int n,m,x,y,z;
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++){
		scanf("%d%d%d",&x,&y,&z);
		g[x].push_back(edge(y,z));
		g[y].push_back(edge(x,z));
	}
	for(int i=1;i<=n;i++) scanf("%d",&c[i]);
	for(int i=1;i<=n;i++) solve1(i);
	printf("%lld\n",solve2(n,m));
	return 0;
}

提出情報

提出日時
問題 D - Content Delivery
ユーザ NCTU_Thor
言語 C++11 (GCC 4.9.2)
得点 100
コード長 1928 Byte
結果 AC
実行時間 1745 ms
メモリ 48808 KiB

コンパイルエラー

./Main.cpp: In function ‘int main()’:
./Main.cpp:80:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&m);
                     ^
./Main.cpp:82:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d",&x,&y,&z);
                           ^
./Main.cpp:86:41: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;i++) scanf("%d",&c[i]);
                                         ^

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 60
セット名 テストケース
All 00_sample_00, 00_sample_01, 10_00_Random_0006, 10_01_Random_0037, 10_02_Random_0863, 10_03_Random_2000, 10_04_Random_0007, 10_05_Random_0054, 10_06_Random_0139, 10_07_Random_2000, 10_08_Random_0010, 10_09_Random_0084, 10_10_Random_0737, 10_11_Random_2000, 10_12_Random_0007, 10_13_Random_0062, 10_14_Random_0868, 10_15_Random_2000, 20_00_Star_0009, 20_01_Star_0014, 20_02_Star_0675, 20_03_Star_2000, 20_04_Star_2000, 20_05_Star_0004, 20_06_Star_0050, 20_07_Star_0969, 20_08_Star_2000, 20_09_Star_2000, 30_00_Line_0005, 30_01_Line_0011, 30_02_Line_0369, 30_03_Line_2000, 30_04_Line_2000, 30_05_Line_0009, 30_06_Line_0035, 30_07_Line_0217, 30_08_Line_2000, 30_09_Line_2000, 40_00_Caterpillar_0096, 40_01_Caterpillar_0167, 40_02_Caterpillar_2000, 40_03_Caterpillar_2000, 40_04_Caterpillar_0026, 40_05_Caterpillar_0775, 40_06_Caterpillar_2000, 40_07_Caterpillar_2000, 50_00_Skew_2000, 50_01_Skew_2000, 50_02_Skew_2000, 50_03_Skew_2000, 50_04_Skew_2000, 50_05_Skew_2000, 60_00_Binary_0995, 60_01_Binary_2000, 60_02_Binary_2000, 60_03_Binary_0476, 60_04_Binary_2000, 60_05_Binary_2000, 80_max_00, 90_teuch_00
ケース名 結果 実行時間 メモリ
00_sample_00 AC 26 ms 924 KiB
00_sample_01 AC 26 ms 932 KiB
10_00_Random_0006 AC 27 ms 928 KiB
10_01_Random_0037 AC 26 ms 1176 KiB
10_02_Random_0863 AC 183 ms 8224 KiB
10_03_Random_2000 AC 984 ms 28316 KiB
10_04_Random_0007 AC 27 ms 928 KiB
10_05_Random_0054 AC 26 ms 1172 KiB
10_06_Random_0139 AC 29 ms 1568 KiB
10_07_Random_2000 AC 722 ms 28708 KiB
10_08_Random_0010 AC 25 ms 848 KiB
10_09_Random_0084 AC 26 ms 1240 KiB
10_10_Random_0737 AC 108 ms 6820 KiB
10_11_Random_2000 AC 762 ms 28584 KiB
10_12_Random_0007 AC 28 ms 928 KiB
10_13_Random_0062 AC 27 ms 1184 KiB
10_14_Random_0868 AC 143 ms 8352 KiB
10_15_Random_2000 AC 724 ms 28964 KiB
20_00_Star_0009 AC 26 ms 936 KiB
20_01_Star_0014 AC 25 ms 924 KiB
20_02_Star_0675 AC 195 ms 9972 KiB
20_03_Star_2000 AC 971 ms 48528 KiB
20_04_Star_2000 AC 1666 ms 48552 KiB
20_05_Star_0004 AC 25 ms 928 KiB
20_06_Star_0050 AC 26 ms 1172 KiB
20_07_Star_0969 AC 236 ms 16036 KiB
20_08_Star_2000 AC 969 ms 48548 KiB
20_09_Star_2000 AC 1702 ms 48548 KiB
30_00_Line_0005 AC 26 ms 932 KiB
30_01_Line_0011 AC 24 ms 924 KiB
30_02_Line_0369 AC 51 ms 3104 KiB
30_03_Line_2000 AC 724 ms 28824 KiB
30_04_Line_2000 AC 993 ms 28324 KiB
30_05_Line_0009 AC 26 ms 924 KiB
30_06_Line_0035 AC 26 ms 1176 KiB
30_07_Line_0217 AC 34 ms 2072 KiB
30_08_Line_2000 AC 718 ms 29088 KiB
30_09_Line_2000 AC 1009 ms 28312 KiB
40_00_Caterpillar_0096 AC 29 ms 1316 KiB
40_01_Caterpillar_0167 AC 36 ms 2064 KiB
40_02_Caterpillar_2000 AC 1708 ms 48796 KiB
40_03_Caterpillar_2000 AC 1745 ms 48680 KiB
40_04_Caterpillar_0026 AC 27 ms 1060 KiB
40_05_Caterpillar_0775 AC 162 ms 11296 KiB
40_06_Caterpillar_2000 AC 1034 ms 48800 KiB
40_07_Caterpillar_2000 AC 1745 ms 48808 KiB
50_00_Skew_2000 AC 789 ms 33692 KiB
50_01_Skew_2000 AC 1165 ms 34212 KiB
50_02_Skew_2000 AC 764 ms 33312 KiB
50_03_Skew_2000 AC 1141 ms 32800 KiB
50_04_Skew_2000 AC 751 ms 32540 KiB
50_05_Skew_2000 AC 1124 ms 32676 KiB
60_00_Binary_0995 AC 255 ms 10780 KiB
60_01_Binary_2000 AC 651 ms 32792 KiB
60_02_Binary_2000 AC 1027 ms 32788 KiB
60_03_Binary_0476 AC 59 ms 4256 KiB
60_04_Binary_2000 AC 1029 ms 32808 KiB
60_05_Binary_2000 AC 1032 ms 32800 KiB
80_max_00 AC 290 ms 9180 KiB
90_teuch_00 AC 26 ms 800 KiB