hdu5242 上海邀请赛 优先队列+贪心
题意是给你一棵树 n个点 n-1条边 起点是1 每一个点都有权值 每次能从根节点走到叶子节点 经行k次游戏 每次都是从1開始 拿过的点的权值不能拿第二次 问最大权值和。
開始看到题时也没想到什么方法 就依照常规的来 结果超时了 试着优化了好多次 最后过了 百度题讲解是树链剖分 醉了 还没学!
。。
说说我的做法吧 map【i】=a表示i节点的跟节点为a节点 从全部叶子节点開始入队(有点队列里有三个变量 各自是节点编号 权值 深度 优先级看代码 里面有点贪心的意思) 每次走根节点 假设根节点没走过 则走它 并把该店权值变为0 否则直接跳到1这个节点(假设一个个跳可能会超时)再入队 当出队的编号为1时而且拿的个数小于游戏次数 则拿 否则结束 在跑深度的时候有个优化
開始没有超时了 假设该节点深度已知了 则以后的根节点就不用跑了。!
!
详细看代码吧
- #include<stdio.h>
- #include<string.h>
- #include<queue>
- #include<iostream>
- using namespace std;
- int map[100010],mark[100010];
- int Deep[100010];
- __int64 num[100010];
- struct node
- {
- __int64 value;
- int ii;
- int deep;
- bool operator < (const node& x) const
- {
- return deep<x.deep||(deep==x.deep&&value<x.value);
- }
- }a;
- int main()
- {
- int T,i,j,n,k,r=1;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d%d",&n,&k);
- for(i=1;i<=n;i++)
- {
- scanf("%I64d",&num[i]);
- }
- memset(mark,0,sizeof(mark));
- for(i=1;i<n;i++)
- {
- int x,y;
- scanf("%d%d",&x,&y);
- mark[x]=1;
- map[y]=x;
- }
- priority_queue<node>Q;
- memset(Deep,0,sizeof(Deep));
- for(i=1;i<=n;i++)
- {
- if(mark[i]==0)
- {
- int x=map[i];
- int d=1;
- while(x!=1)
- {
- if(Deep[x]>0) {d+=Deep[x];break;}
- x=map[x];
- d++;
- }
- x=i;
- while(x!=1)
- {
- if(Deep[x]>0) break;
- Deep[x]=d;
- x=map[x];
- d--;
- }
- a.deep=Deep[i];
- a.value=num[i];
- a.ii=i;
- Q.push(a);
- }
- }
- //for(i=1;i<=n;i++)
- //printf("%d ^^^ %d\n",i,Deep[i]);
- __int64 sum=0;
- int cont=0;
- while(!Q.empty())
- {
- a=Q.top();
- Q.pop();
- int x=map[a.ii];
- /*while(num[x]==0&&x!=1)
- {
- x=map[x];
- }*/
- if(a.ii==1)
- {
- a.value+=num[1];
- num[1]=0;
- sum+=a.value;
- cont++;
- if(cont>=k) break;
- }
- else
- {
- if(num[x]==0)
- {
- a.ii=1;
- a.deep=0;
- }
- else
- {
- a.ii=x;
- a.deep=Deep[x];
- a.value+=num[x];
- num[x]=0;
- }
- Q.push(a);
- }
- }
- printf("Case #%d: %I64d\n",r++,sum);
- }
- return 0;
- }
hdu5242 上海邀请赛 优先队列+贪心的更多相关文章
- [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线
Description Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
- POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
- hdu3438 Buy and Resell(优先队列+贪心)
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)
Game with Pearls Problem DescriptionTom and Jerry are playing a game with tubes and pearls. The rule ...
- ZOJ-3410Layton's Escape(优先队列+贪心)
Layton's Escape Time Limit: 2 Seconds Memory Limit: 65536 KB Professor Layton is a renowned arc ...
- CodeForces - 853A Planning (优先队列,贪心)
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
- poj2431(优先队列+贪心)
题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...
- H - Expedition 优先队列 贪心
来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...
随机推荐
- mariadb的安装
mysql (分支 mariadb)1.安装mariadb -yum -源码编译安装 -下载rpm安装 yum和源码编译安装的区别? 1.路径区别-yum安装的软件是他自定义的,源码安装的软件./co ...
- Laravel5.1学习笔记20 EloquentORM 关系
Eloquent: Relationships Introduction Defining Relationships One To One One To Many Many To Many Has ...
- dropdownlist显示树形结构
/// <summary> /// 递归 /// </summary> /// <param name="deplist"></param ...
- React容器组件和展示组件
Presentational and Container Components 展示组件 - 只关心它们的样子. - 可能同时包含子级容器组件和展示组件,一般含DOM标签和自定的样式. ...
- Android项目实战_手机安全卫士splash界面
- 根据代码的类型组织包结构 1. 界面 com.hb.mobilesafe.activities 2. 服务 com.hb.mobilesafe.services 3. 业务逻辑 com.hb.mo ...
- JS——滚动条
1.核心思想与之前的拖拽盒子是一样的 2.完全将鼠标在盒子中的坐标给滚动条是错的,因为这样会使滚动条顶部立刻瞬间移动到鼠标位置 3.必须在鼠标按下事件时记住鼠标在滚动条内部的坐标,再将鼠标在盒子中的坐 ...
- JS——行内式注册事件
html中行内调用function的时候,是通过window调用的function,所以打印this等于打印window,所以在使用行内注册事件时务必传入参数this <!DOCTYPE htm ...
- Codeforces_718A
A. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 关于在win7旗舰版32位上 安装 net4.0 的闪退问题研究 和安装sqlserver2008问题
1.配置文件客户端[目标x86x64]的 可以安装 2.配置文件完全的目标x86x64的 出现闪退. 3.服务器核心的出现无法安装 安装 sqlserver 2008R2数据库 报错 \最后留下了它, ...
- day07补充-数据类型总结及拷贝
目录 数据类型总结 按照存一个值 OR 多个值来分 按照有序 OR 无序来分 按照可变 OR 不可变来分 拷贝 && 浅拷贝 && 深拷贝&& .cop ...