题面

既然是模板, 那就直接贴代码?

两种思路

1.迪杰斯特拉

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = ;
int head[N], cnt, n, m, s;
long long dis[N];
bool vis[N];
struct node {
int next, to;
long long w;
}e[N];
void add(int x, int y, long long z) {
e[++cnt].next = head[x];
e[cnt].to = y;
e[cnt].w = z;
head[x] = cnt;
}
void dijkstra(int s) {
for(int i = ; i <= n; i++) dis[i] = ;
dis[s] = ;
for(int i = ; i <= n; i++) {
int k = , maxn = ;
for(int j = ; j <= n; j++)
if(!vis[j] && dis[j] <= maxn)
k = j, maxn = dis[j];
vis[k] = ;
for(int j = head[k]; j; j = e[j].next)
if(dis[e[j].to] > dis[k] + e[j].w)
dis[e[j].to] = dis[k] + e[j].w;
}
}
int main () {
scanf("%d%d%d", &n, &m, &s);
for(int i = ; i <= m; i++) {
int x, y;
long long z;
scanf("%d%d%lld", &x, &y, &z);
add(x, y, z);
}
dijkstra(s);
for(int i = ; i <= n; i++)
printf("%lld ", dis[i]);
return ;
}

2.spfa

#include <iostream>
#include <cstdio>
#include <queue>
#define N 500005
#define inf 2147483647
using namespace std;
int n, m, s, cnt;
int dis[N], vis[N], head[N];
struct node {
int next, to, w;
}tr[N];
void add (int x, int y, int z) {
tr[++cnt].to = y;
tr[cnt].next = head[x];
tr[cnt].w = z;
head[x] = cnt;
}
void spfa () {
queue<int> q;
for (int i = ; i <= n; i++)
dis[i] = inf;
vis[s] = ;
q.push(s);
dis[s] = ;
while (!q.empty()) {
int he = q.front();
q.pop();
vis[he] = ;
for (int i = head[he]; i ;i = tr[i].next) {
if (dis[tr[i].to] > dis[he] + tr[i].w) {
dis[tr[i].to] = dis[he] + tr[i].w;
if (!vis[tr[i].to]) {
vis[tr[i].to] = ;
q.push(tr[i].to);
}
}
}
}
}
int main () {
scanf ("%d%d%d", &n, &m, &s);
for (int i = ; i <= m; i++) {
int a, b, c;
scanf ("%d%d%d", &a, &b, &c);
add (a, b, c);
}
spfa ();
for (int i = ; i <= n; i++)
if (s == i) printf ("0 ");
else printf ("%d ", dis[i]);
return ;
}

add:2019.8.15

增加堆优化后的迪杰斯特拉算法;

用来切标准版

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = ;
int n, m, s, cnt, head[N], dis[N];
bool vis[N];
struct node{
int next, to, w;
}e[N];
int read() {
int s = , w = ;
char ch = getchar();
while(!isdigit(ch)){if(ch == '-') w = -;ch = getchar();}
while(isdigit(ch)){s = s * + ch - '';ch = getchar();}
return s * w;
}
void add(int x, int y, int z) {
e[++cnt].next = head[x];
e[cnt].to = y;
e[cnt].w = z;
head[x] = cnt;
}
struct Node {
int u, v;
bool operator<(const Node &b) const {
return u > b.u;
}
};
void dijikstra(int s) {
priority_queue <Node> q;
memset(dis, 0x3f3f3f3f, sizeof(dis));
dis[s] = ;
Node o;
o.u = ;
o.v = s;
q.push(o);
while(!q.empty()) {
int u = q.top().v;
int d = q.top().u;
q.pop();
if(d != dis[u])continue;
for(int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
int w = e[i].w;
if(dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
Node p;
p.u = dis[v], p.v = v;
q.push(p);
}
}
}
}
int main () {
n = read();
m = read();
s = read();
while(m--) {
int x, y, z;
x = read();
y = read();
z = read();
add (x, y, z);
}
dijikstra(s);
for(int i = ; i <= n; i++)
printf("%d ", dis[i]);
return ;
}

最新文章

  1. java面向对象三大特性之继承
  2. City Skyline
  3. WallsEveryDay 必应桌面壁纸
  4. 转载:CPU的位数和操作系统的位数
  5. linux备份mysql数据库
  6. C#Redis列表List
  7. Ubuntu安装MongoDB和PHP扩展
  8. IOS开发中如何判断程序第一次启动(根据判断结果决定是否显示新手操作引导)
  9. LeetCode 11. Container With Most Water (装最多水的容器)
  10. Linxu指令--crond
  11. 纯 Html 5的 简单 TreeView 保存下思路以后有机会再完善。
  12. postMan用法
  13. Oracle数据库创建表空间
  14. (转)FFmpeg源代码简单分析:avformat_open_input()
  15. The way to Go(3): 安装Go环境
  16. ubantu下安装软件
  17. 不要怂,就是GAN (生成式对抗网络) (二):数据读取和操作
  18. Oracle——系统数据字典常用命令(查看表所属空间层目录等)
  19. 关于Safe DOG的文件上传bypass
  20. [QT] Tab键切换焦点顺序

热门文章

  1. C# - List.Sort()自定义排序方法
  2. SQL Server中临时表是在什么schema下的(转载)
  3. ASP.NET 异步编程之Async await
  4. 2019 中钢网java面试笔试题 (含面试题解析)
  5. PHP面试题2019年腾讯工程师面试题和答案
  6. Django---图书管理系统,一对多(外键设置),__str__和__repr__的区别,进阶版项目说明简介.模版语言if ... else ..endif
  7. FineReport连接SSAS多维数据库
  8. 关于UDP协议
  9. hibernate Criteria中多个or和and的用法 and ( or or)
  10. word,excel,ppt转pdf