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的更多相关文章

随机推荐

  1. 数组分组(DP)

    一个长度为n的数组a,我们可以把它分成任意组,每一组是一段连续的区间. 比如数组1,2,3,4,5可以分成(1,2),(3,4,5)两个组.每个分组都有一个权值,这个权值就是分组里面每个数的乘积对10 ...

  2. 斐波那契数列 yield 和list 生成

    def fab_demo4(max): a,n,b = 0,0,1 while n < max: yield b # 生成器走到这一步返回b,需要再次调用才能继续执行 a,b = b,a+b n ...

  3. Python创建命令行应用的工具 tools for command line application in python

    工具1:Docopt 地址:http://docopt.org/ 这个工具是根据模块的文档注释来确定参数的.注释分为两部分:Usage, option. \``` Usage: naval_fate ...

  4. 移动端— position: fixed;固定定位解决方案

    这里有个关键的东西叫做viewport,你经常在页面的头部里可以见到它: <meta name="viewport" content="width=device-w ...

  5. 数据类型操作简单对比(R和Python)

    一.R方面 R中类型:向量(vector).数据框.矩阵.列表 数据处理转换时:数值型num.因子(factor).字符型等等 1)matrix feature:1.二维数组2.每个元素必须有相同的数 ...

  6. h5-localStorage储存的使用

    <!-- localStorage的使用: 1.存储的内容大概20mb 2.不同浏览器不能共享数据,但是在同意浏览器的不同窗口中可以共享数据 3.永久生效,他的数据是储存在硬盘上,并不会随着页面 ...

  7. Python实现Collatz序列(考拉兹猜想)

    考拉兹猜想(英语:Collatz conjecture),又称为奇偶归一猜想.3n+1猜想.冰雹猜想.角谷猜想.哈塞猜想.乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1, ...

  8. javaweb学习——会话技术(二)

    文中部分借鉴了:https://www.cnblogs.com/xdp-gacl/p/3855702.html https://blog.csdn.net/p744174529/article/det ...

  9. 吴裕雄--天生自然 JAVA开发学习:Character 类

    char ch = 'a'; // Unicode 字符表示形式 char uniChar = '\u039A'; // 字符数组 char[] charArray ={ 'a', 'b', 'c', ...

  10. Spring Cloud Hystrix熔断器隔离方案

      Hystrix组件提供了两种隔离的解决方案:线程池隔离和信号量隔离.两种隔离方式都是限制对共享资源的并发访问量,线程在就绪状态.运行状态.阻塞状态.终止状态间转变时需要由操作系统调度,占用很大的性 ...