最短路计数

#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + , M = 2e6 + ;
const int oo = ( << ); #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} struct Node {
int u, dis_;
bool operator < (Node a) const {
return dis_ > a.dis_;
}
};
struct Node_2 {int u, v, w, nxt;} G[M];
int head[N], dis[N], cnt;
int f[N];
int n, m, k, Mod;
bool vis[N];
priority_queue <Node> Q;
int Cnt[N]; inline void Link(int u, int v, int w) {
G[++ cnt].u = u; G[cnt].v = v; G[cnt].w = w; G[cnt].nxt = head[u]; head[u] = cnt;
} void Dij(int start) {
for(int i = ; i <= n; i ++) dis[i] = oo, vis[i] = , Cnt[i] = ;
Q.push((Node) {start, });
dis[start] = ;
Cnt[start] = ;
while(!Q.empty()) {
Node topp = Q.top();
Q.pop();
if(vis[topp.u]) continue;
vis[topp.u] = ;
for(int i = head[topp.u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(dis[v] > dis[topp.u] + G[i].w) {
Cnt[v] = Cnt[topp.u];
dis[v] = dis[topp.u] + G[i].w;
Q.push((Node) {v, dis[v]});
} else if(dis[v] == dis[topp.u] + G[i].w) {
(Cnt[v] += Cnt[topp.u]) %= Mod;
}
}
}
} int main() {
Mod = ;
n = read(), m = read();
for(int i = ; i <= n; i ++) head[i] = -;
cnt = ;
for(int i = ; i <= m; i ++) {
int u, v, w;
u = read(), v = read(), w = ;
Link(u, v, w); Link(v, u, w);
}
Dij();
for(int i = ; i <= n; i ++) {
cout << Cnt[i] % Mod << "\n";
}
return ;
}

luogu 1144的更多相关文章

  1. POJ 1144

    http://poj.org/problem?id=1144 题意:给你一些点,某些点直接有边,并且是无向边,求有多少个点是割点 割点:就是在图中,去掉一个点,无向图会构成多个子图,这就是割点 Tar ...

  2. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  3. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  4. ural 1144. The Emperor's Riddle

    1144. The Emperor's Riddle Time limit: 1.0 secondMemory limit: 4 MB Background In the olden times th ...

  5. poj 1144 Network 图的割顶判断模板

    Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8797   Accepted: 4116 Descripti ...

  6. POJ 1144 Network(Tarjan求割点)

    Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12707   Accepted: 5835 Descript ...

  7. poj 1144 Network(无向图求割顶数)

    题目链接:poj 1144 题意就是说有 n(标号为 1 ~ n)个网点连接成的一个网络,critical places 表示删去后使得图不连通的顶点,也就是割顶,求图中割顶的个数. 直接上大白书上的 ...

  8. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  9. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

随机推荐

  1. MySQL8.0新特性总览

    1.消除了buffer pool mutex (Percona的贡献) 2.数据字典全部采用InnoDB引擎存储,支持DDL原子性.crash safe.metadata管理更完善(可以利用ibd2s ...

  2. Android--文件存取

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundExc ...

  3. (转) 嵌入式 Linux 利用 udev 实现自动检测挂载U盘

    本文链接:https://blog.csdn.net/cfl927096306/article/details/95180940 udev配置文件是/etc/udev/udev.conf,也许长这样: ...

  4. QT-入门:创建项目时遇到工程工具集(Kit)找不到问题

    创建项目遇到了以下提示: Please add a kit in the options or via the maintenance tool of the SDK 解决方法: 在指定的工具链中设置 ...

  5. Luogu3824 [NOI2017]泳池 【多项式取模】【递推】【矩阵快速幂】

    题目分析: 用数论分块的思想,就会发现其实就是连续一段的长度$i$的高度不能超过$\lfloor \frac{k}{i} \rfloor$,然后我们会发现最长的非$0$一段不会超过$k$,所以我们可以 ...

  6. 偶数矩阵 Even Parity,UVa 11464

    题目描述 Description 给你一个n*n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.如图所示的矩阵至少要把3个 ...

  7. @app.route源码流程分析

    @app.route(), 是调用了flask.app.py文件里面的Flask类的route方法,route方法所做的事情和add_url_rule类似,是用来为一个URL注册一个视图函数,但是我们 ...

  8. display的属性

    在一般的CSS布局制作时候,我们常常会用到display对应值有block.none.inline这三个值.,display这个属性用于定义建立布局时元素生成的显示框类型.对于 HTML 等文档类型, ...

  9. Java 之 字符流

    一.字符流 当使用字节读取文本文件时,可能会有一个小问题,就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储.所以 Java 提供了一些字符类,以字符为单位读写数据, ...

  10. zabbix推送内存监控单应用shell

    利用top方式获取指定第三方的内存使用率 #!/bin/bash process=$1 if [ ! -z $process ];then cd /zabbix/zabbix-agent/script ...