JZYZOJ1525 HAOI2012道路 堆优化的dijkstra+pair
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define pa pair<int,int>
const int mymod=;
const int bign=;
int n,m;
struct wtff{
int y;
int next;
int zhi;
}wtf[];
int head[]={};
int tail=;
long long ans[]={};
long long a[]={};
long long b[]={};
int c[]={};
long long dis[]={};
bool vis[]={};
void init(int x,int y,int zhi){
wtf[++tail].next=head[x];
wtf[tail].zhi=zhi;
wtf[tail].y=y;
head[x]=tail;
}
void jiuming(int st){
priority_queue< pa,vector<pa>,greater<pa> >q;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
dis[i]=bign;
}
dis[st]=;
q.push(make_pair(,st));
int cn=;
while(!q.empty()){
int x=q.top().second;
q.pop();
if(vis[x]){
continue;
}
vis[x]=;
c[++cn]=x;
for(int i=head[x];i!=;i=wtf[i].next){
int y;
y=wtf[i].y;
if(dis[x]+wtf[i].zhi<dis[y]){
dis[y]=dis[x]+wtf[i].zhi;
q.push(make_pair(dis[y],y));
}
}
}
memset(a,,sizeof(a));
memset(b,,sizeof(b));
a[st]=;
for(int i=;i<=cn;i++){
b[c[i]]=;
}
for(int i=;i<=cn;i++){
for(int w=head[c[i]];w!=;w=wtf[w].next){
int y=wtf[w].y;
if(dis[c[i]]+wtf[w].zhi==dis[y]){
a[y]+=a[c[i]];
if(a[y]>mymod){
a[y]%=mymod;
}
}
}
}
for(int i=cn;i>=;i--){
for(int w=head[c[i]];w!=;w=wtf[w].next){
int y=wtf[w].y;
if(dis[c[i]]+wtf[w].zhi==dis[y]){
b[c[i]]+=b[y];
if(b[c[i]]>mymod){
b[c[i]]%=mymod;
}
}
}
}
for(int i=;i<=n;i++){
for(int w=head[i];w!=;w=wtf[w].next){
int y=wtf[w].y;
if(dis[i]+wtf[w].zhi==dis[y]){
ans[w]+=(a[i]*b[y]);
if(ans[w]>mymod){
ans[w]%=mymod;
}
}
}
}
}
int main(){
cin>>n>>m;
for(int i=;i<=m;i++){
int a1,b1,c1;
cin>>a1>>b1>>c1;
init(a1,b1,c1);
}
for(int i=;i<=n;i++){
jiuming(i);
}
for(int i=;i<=m;i++){
cout<<ans[i]<<endl;
}
return ;
}
JZYZOJ1525 HAOI2012道路 堆优化的dijkstra+pair的更多相关文章
- 堆优化的Dijkstra
SPFA在求最短路时不是万能的.在稠密图时用堆优化的dijkstra更加高效: typedef pair<int,int> pii; priority_queue<pii, vect ...
- POJ1797 Heavy Transportation (堆优化的Dijkstra变形)
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
- 朴素版和堆优化版dijkstra和朴素版prim算法比较
1.dijkstra 时间复杂度:O(n^2) n次迭代,每次找到距离集合S最短的点 每次迭代要用找到的点t来更新其他点到S的最短距离. #include<iostream> #inclu ...
- 学习笔记·堆优化$\mathscr{dijkstra}$
嘤嘤嘤今天被迫学了这个算法--其实对于学习图论来说我内心是拒绝的\(\mathscr{qnq}\) 由于发现关于这个\(\mathscr{SPFA}\)的时间复杂度\(O(kE)\)中的\(k \ap ...
- 堆优化的dijkstra算法
#include<bits/stdc++.h> using namespace std; #define ll long long #define P pair<int,int> ...
- 【模板】堆优化的dijkstra
生命算法,以防忘记 #include<bits/stdc++.h> using namespace std; int head[200005],dis[200005],n,m,s,f,g, ...
- 洛谷 P1462 通往奥格瑞玛的道路(二分答案,堆优化dijkstra)
传送门 解题思路 首先看题目问题,求经过的所有城市中最多的一次收取的费用的最小值是多少.一看“最大值最小”就想到了二分答案. 在读一遍题目,就是二分收取的费用,然后对于每一个二分的费用,跑一边最短路, ...
- POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)
昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...
- 【51nod1443】路径和树(堆优化dijkstra乱搞)
点此看题面 大致题意:给你一个无向联通图,要求你求出这张图中从u开始的权值和最小的最短路径树的权值之和. 什么是最短路径树? 从\(u\)开始到任意点的最短路径与在原图中相比不变. 题解 既然要求最短 ...
随机推荐
- JavaScript事件和方法
单击一个超链接触发事件 1.用a标签的onclick <a href="#" onclick="js代码"> 这种写法呢,存在一种弊端,就是点击后会 ...
- 查询PHP版本
查询php版本: phpinfo();
- 聊聊spring的那些扩展机制
1.背景 慎入:本文将会有大量代码出入. 在看一些框架源码的时候,可以看见他们很多都会和Spring去做结合.举个例子dubbo的配置: 很多人其实配置了也就配置了,没有去过多的思考:为什么这么配置s ...
- 【洛谷 P4542】 [ZJOI2011]营救皮卡丘(费用流)
题目链接 用最多经过\(k\)条经过\(0\)的路径覆盖所有点. 定义\(ds[i][j]\)表示从\(i\)到\(j\)不经过大于\(max(i,j)\)的点的最短路,显然可以用弗洛伊德求. 然后每 ...
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- 【shell】shell编程总结
总结一下在写shell脚本时的常见注意事项: 1.shell脚本中的命令最好用命令的全路径,如果不知道全路径可以用which cmd查找命令的全路径. 2.shell脚本中定义环境变量用export ...
- python之supervisor进程管理工具
supervisor是python写的一个管理进程运行的工具,可以很方便的监听.启动.停止.重启一个或多个进程:有了supervisor后,就不用字节写启动和监听的shell脚本了,非常方便. sup ...
- 64_o2
openrdf-sesame-queryrender-2.8.10-2.fc26.noarch..> 11-Feb-2017 18:38 52014 openrdf-sesame-queryre ...
- ps aux排序
按内存升序排列: ps aux --sort=+rss 按内存降序排列: ps aux --sort=-rss 按cpu升序排列: ps aux --sort=+%cpu 为按cpu降序排列. ps ...
- [ python ] 全局和局部作用域变量的引用
全局与局部变量的引用 (a)locals(b)globals 这里还需要在补充2个关键字一起比较学习,关键字:(c)nonlocal(d)global locals 和 globals locals: ...