ccpc20190823
04
http://acm.hdu.edu.cn/showproblem.php?pid=6705
分析;先把每条边以 形式放进堆,堆按路径权值从小到大排序,然后每次取出堆顶,用v的出边扩展 新的路径。但是一个点的出度可能会非常大(如菊花图),可以发现,将出边排序之后,
每次只需要扩 展当前点最小的出边,和扩展到当前点的边的下一条边即可。堆中需要记录当前结点,当前距离,上一 节点距离,扩展到当前节点时下一条应该扩展的边。
(注意,如果一次性扩展当前点连出去的所有权值 相同的边,是会TLE的,实际上也是没有必要的。)
复杂度:O(k*log(m+k))
#include<queue>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
#define pb push_back
const int M=1e5+;
struct node{
ll cost;
int u,id;
node(ll costt=,int uu=,int idd= ){
cost=costt;
u=uu;
id=idd;
}
bool operator < ( const node &b)const{
return cost>b.cost;
}
};
#define pli pair<ll,int>
vector<pli> e[M];
ll ans[M];
int a[M];
priority_queue<node> que;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++)
e[i].clear();
while(!que.empty())
que.pop();
for(int i=;i<=m;i++){
int u,v;
ll w;
scanf("%d%d%lld",&u,&v,&w);
e[u].pb(pli(w,v));
}
int maxxk=;
for(int i=;i<=q;i++){
scanf("%d",&a[i]);
maxxk=max(maxxk,a[i]);
}
for(int i=;i<=n;i++)
sort(e[i].begin(),e[i].end());
for(int i=;i<=n;i++)
if(e[i].size())
que.push(node(e[i][].first,i,));
int tot=;
while(!que.empty()){
node now=que.top();
que.pop();
int u=now.u;
int id=now.id;
ll Cost=now.cost;
if(Cost)
ans[++tot]=Cost;
if(tot==maxxk)
break;
if(id<(int)e[u].size()-)
que.push(node(Cost-e[u][id].first+e[u][id+].first,u,id+));
int v=e[u][id].second;
if(e[v].size())
que.push(node(Cost+e[v][].first,v,));
}
for(int i=;i<=q;i++)
printf("%lld\n",ans[a[i]]);
}
return ;
}
ccpc20190823的更多相关文章
随机推荐
- java8中的map 和reduce
map 1.使用map让集合里面的数字翻倍. List<Integer> numbers = Lists.newArrayList(1,2,3,4,5);List<Integer&g ...
- Leetcode -- 两数之和Ⅰ
1. 两数之和 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 示例:给定 nums = [2, 7, 11, 15 ...
- vivado下创建基本时序周期约束
创建基本时钟周期约束.(验证我们的设计能否在期望的频率上运行) (学习记录,晚一点会做实验传上来的.) 时钟基本概念:https://blog.csdn.net/wordwarwordwar/arti ...
- Golang的学习方法
- gradle配置多个代码仓库repositories
repositories { mavenCentral() maven { url "https://jitpack.io" } maven { url "http:// ...
- 插曲 强大的神器 vmware
电脑到了 这连天给电脑配环境变量 真的想死 发现用 虚拟机 直接跑别人配置好的镜像文件 多快好省超级开心 比较毒瘤的 clion 不仅要配置c++ 环境 还要走cmake 等一堆东西 ...
- ERNIE:知识图谱结合BERT才是「有文化」的语言模型
自然语言表征模型最近受到非常多的关注,很多研究者将其视为 NLP 最重要的研究方向之一.例如在大规模语料库上预训练的 BERT,它可以从纯文本中很好地捕捉丰富的语义模式,经过微调后可以持续改善不同 N ...
- VuePress 中增加用户登录功能
在 VuePress 中增加用户登录 VuePress 是 Vuejs 官方提供的一个快速建设文档站点的工具,在简单配置好功能后,需要做的事情就剩下写好一个个 Markdown 文档. 因为 VueP ...
- 12 Spring Data JPA:orm思想和hibernate以及jpa的概述和jpa的基本操作
spring data jpa day1:orm思想和hibernate以及jpa的概述和jpa的基本操作 day2:springdatajpa的运行原理以及基本操作 day3:多表操作,复杂查询 d ...
- PAT Advanced 1145 Hashing – Average Search Time (25) [哈希映射,哈希表,平⽅探测法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...