“玲珑杯”ACM比赛 Round #18--最后你还是AK了(搜索+思维)



#define OPENSTACK
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn=1e5+;
vector<pair<int,int> > v[maxn];
int c[maxn], cn[maxn], tmp, n;
LL ans; int dfs(int p,int f)
{
int count=;
for(int i=; i<v[p].size(); i++)
{
pair<int,int>pa=v[p][i];
if(pa.first==f) continue; int t=dfs(pa.first,p);
cn[tmp]=min(t,n-t);
ans+=(LL)cn[tmp]*(LL)pa.second;
tmp++;
count+=t;
}
return count+;
} int main()
{
#ifdef OPENSTACK
long long size = << ; // 64MB
char *p = (char*)malloc(size) + size;
#if (defined _WIN64) or (defined __unix)
__asm__("movq %0, %%rsp\n" :: "r"(p));
#else
__asm__("movl %0, %%esp\n" :: "r"(p));
#endif
#endif int T,k; cin>>T;
while(T--)
{
for(int i=;i<maxn;i++) v[i].clear();
scanf("%d%d",&n,&k);
for(int i=;i<n;i++)
{
int u1,u2,w;
scanf("%d%d%d",&u1,&u2,&w);
pair<int,int>pa1=make_pair(u1,w);
pair<int,int>pa2=make_pair(u2,w);
v[u1].push_back(pa2);
v[u2].push_back(pa1);
}
for(int i=;i<k;i++) scanf("%d",&c[i]);
ans=; tmp=;
int res=dfs(,-);
//cout<<"点数-----"<<res<<endl;
//cout<<" -----"<<ans<<endl;
sort(cn,cn+tmp);
sort(c,c+k);
for(int i=tmp-, j=k-; j>=; i--, j--)
{
ans+=(LL)c[j]*(LL)cn[i];
}
printf("%lld\n",ans);
}
#ifdef OPENSTACK
exit();
#else
return ;
#endif
}
“玲珑杯”ACM比赛 Round #18--最后你还是AK了(搜索+思维)的更多相关文章
- “玲珑杯”ACM比赛 Round #18
“玲珑杯”ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time ...
- “玲珑杯”ACM比赛 Round #18 1147 - 最后你还是AK了(思维,边的贡献)
题目链接:http://www.ifrog.cc/acm/problem/1147 题解:这题很容易想到的是边的贡献也就是每条边最多被取到几次,和点的贡献类似,那些加边只要加在边贡献大的边上就行.然后 ...
- “玲珑杯”ACM比赛 Round #18 C -- 图论你先敲完模板(和题目一点关系都没有,dp)
题目链接:http://www.ifrog.cc/acm/problem/1146?contest=1020&no=2 题解:显然知道这是一道dp而且 dp[i]=min(dp[j]+2^(x ...
- 玲珑杯”ACM比赛 Round #18 A -- 计算几何你瞎暴力(瞎暴力)
题目链接:http://www.ifrog.cc/acm/problem/1143?contest=1020&no=0 题解:就是瞎暴力具体多暴力看一下代码就知道了. #include < ...
- “玲珑杯”ACM比赛 Round #18 A 前缀预处理 D dp
DESCRIPTION 今天HHHH 考完了期末考试,他在教学楼里闲逛,他看着教学楼里一间间的教室,于是开始思考: 如果从一个坐标为 (x1,y1,z1)(x1,y1,z1) 的教室走到(x2,y2, ...
- “玲珑杯”ACM比赛 Round #1
Start Time:2016-08-20 13:00:00 End Time:2016-08-20 18:00:00 Refresh Time:2017-11-12 19:51:52 Public ...
- “玲珑杯”ACM比赛 Round #12题解&源码
我能说我比较傻么!就只能做一道签到题,没办法,我就先写下A题的题解&源码吧,日后补上剩余题的题解&源码吧! A ...
- “玲珑杯”ACM比赛 Round #19题解&源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】
A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT ...
- “玲珑杯”ACM比赛 Round #19 B -- Buildings (RMQ + 二分)
“玲珑杯”ACM比赛 Round #19 Start Time:2017-07-29 14:00:00 End Time:2017-07-29 16:30:00 Refresh Time:2017-0 ...
随机推荐
- [HDU1004] Let the balloon rise - 让气球升起来
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- linux下部署php项目-Apache、php、mysql关联
linux下部署php项目环境可以分为两种,一种使用Apache,php,mysql的压缩包安装,一种用yum命令进行安装. 使用三种软件的压缩包进行安装,需要手动配置三者之间的关系.apache和p ...
- 一天搞定CSS(扩展):CSS Hack
做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...
- 论文笔记 Network In Network
这篇论文思路简单.易实现.效果好,是一篇难得的佳作.从实现的角度理解,就是做了以下两个替换: 将线性卷积替换为多层感知机(某种程度上,线性卷积可以认为识一层感知机). 将全连接层用global ave ...
- NodeMCU入门(4):搭建Web服务器,配置网络连接
准备工作 1.NodeMCU模块 2.ESPlorer v0.2.0-rc6 3.NodeMCU-HTTP-Server 搭建web服务器 下载https://github.com/wangzexi/ ...
- openjdk7之编译和debug
大家也可以看我的博客: openjdk7之编译和debug,这里格式更好. 为了更好的学习JDK.HotSpot等源码,需要能debug JDK.HotSpot等源码.本文主要讲述,怎么编译open ...
- 常用SHELL命令
1.查看版本号cat /proc/version; uname -a; uname -r 2.查看用户组cat /etc/group 3.查看当前用户组 groups 4.查看当前用户 whoami ...
- “java.lang.IllegalArgumentException: Failed to evaluate expression ‘ROLE_USER’”报错的解决
这个问题出现在Spring Security的相关配置中,找到原来的这一行: <security:intercept-url pattern="/**" access=&qu ...
- 【Android Developers Training】 98. 获取联系人列表
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 0.搭建myeclipse开发环境
1.配置jdk 2.myeclipse集成tomcat 选择默认jdk