博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[洛谷P4779]【模板】单源最短路径(标准版)
阅读量:7055 次
发布时间:2019-06-28

本文共 1381 字,大约阅读时间需要 4 分钟。

题目大意:单元最短路径(卡$SPFA$)

题解:$dijkstra$($\underline{\hspace{0.5em}}\underline{\hspace{0.5em}}gnu\underline{\hspace{0.5em}}pb\underline{\hspace{0.5em}}ds::priority\underline{\hspace{0.5em}}queue$优化)

卡点:

 

C++ Code:

#include 
#include
#define maxn 100010#define maxm 200010using namespace std;const int inf = 0x3f3f3f3f;int n, m, S;int dis[maxn];int head[maxn], cnt;struct Edge { int to, nxt, w;} e[maxm];void add(int a, int b, int c) { e[++cnt] = (Edge) {b, head[a], c}; head[a] = cnt;}struct cmp { inline bool operator () (const int &a, const int &b) const { return dis[a] > dis[b]; }};__gnu_pbds::priority_queue
q;__gnu_pbds::priority_queue
::point_iterator iter[maxn];void dijkstra(int S) { for (int i = 1; i <= n; i++) dis[i] = inf, iter[i] = q.push(i); dis[S] = 0; q.modify(iter[S], S); while (!q.empty()) { int u = q.top(); q.pop(); for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (dis[v] > dis[u] + e[i].w) { dis[v] = dis[u] + e[i].w; q.modify(iter[v], v); } } }}int main() { scanf("%d%d%d", &n, &m, &S); for (int i = 0; i < m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); add(a, b, c); } dijkstra(S); for (int i = 1; i <= n; i++) { printf("%d ", dis[i] == inf ? -1 : dis[i]); } puts(""); return 0;}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/9460759.html

你可能感兴趣的文章
java读取txt文件内容
查看>>
oracle比较一行的最大值或最小值
查看>>
Jquery中AJAX参数详细(1)-转
查看>>
Sqlserver数据库中的临时表详解
查看>>
centos更新源
查看>>
使用Postfix与Dovecot部署邮件系统
查看>>
UWP 应用获取各类系统、用户信息 (2) - 商店授权信息、零售演示模式信息、广告 ID、EAS 设备信息、硬件识别信息、移动网络信息...
查看>>
肾虚不妨少看片,人丑就该多读书
查看>>
深入理解net core中的依赖注入、Singleton、Scoped、Transient(三)
查看>>
Some Parameter Interpretation On Using Mininet
查看>>
sublime text 3创建新文件插件-AdvanceNewFile
查看>>
Inside i++
查看>>
linux bash Shell脚本经典 Fork炸弹演示及命令详解
查看>>
配置防盗链 访问控制Directory 访问控制FilesMatch
查看>>
翻译:SET Variable(已提交到MariaDB官方手册)
查看>>
JVM从理论到实践
查看>>
notepad把某个字母或者标定符号后面的内容全部删除
查看>>
KVO VS isa
查看>>
windows MSOCache删除
查看>>
【C#】C#线程_混合线程的同步构造
查看>>