嗯...

题目链接:https://www.luogu.org/problem/P3371

没什么好说的,这是一个最短路的模板,这里用的dijkstra做的...

注意:

1.dijkstra和邻接表一块有点别扭,但还是可以遍历的...

2.dis数组不能初始化为2147483647,而要初始化0x3f3f,最后判一下还是不是0x3f3f即可

AC代码:

 #include<cstdio>
#include<cstring>
#include<iostream> using namespace std;
const int maxn = ;
int n, m, tot, s;
long long inf = 0x3f3f;
int vis[maxn], dis[maxn], e[maxn];
// vis -> 是否被访问过 dis -> 最短路 e -> 边的编号
struct node{
int next, to, from, val;
} g[maxn]; inline void add(int u, int v, int w){
g[++tot].from = u;
g[tot].next = e[u];
e[u] = tot;
g[tot].to = v;
g[tot].val = w;
}//邻接表 inline void dijkstra(int x){
memset(vis, , sizeof(vis));
for(int i = ; i <= n; i++) dis[i] = (i == x ? : inf);//初始化
for(int i = ; i <= n; i++){
int t = , y = inf;
for(int j = ; j <= n; j++) if(!vis[j] && dis[j] <= y) y = dis[t = j];
vis[t] = ;
for(int j = e[t]; j; j = g[j].next) dis[g[j].to] = min(dis[g[j].to], dis[t] + g[j].val);
}//松弛操作
for(int i = ; i <= n; i++) {if(dis[i] == 0x3f3f) printf("2147483647 "); else printf("%d ", dis[i]);}
} int main(){
memset(dis, 0x3f3f, sizeof(dis));
scanf("%d%d%d", &n, &m, &s);
for(int i = ; i <= m; i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);//单向图
}
dijkstra(s);
return ;
}

AC代码

最新文章

  1. C#GDI+图像处理
  2. ACM Binary String Matching
  3. ASP.NET MVC 4下 Code First 数据库迁移
  4. Navicat(连接) -1之Navicat Cloud
  5. Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey
  6. hdu 1034 Candy Sharing Game
  7. 国内流行的两大开源.net微信公众平台SDK对比分析
  8. javascript在一个字符串中每隔多少字符插入某个字符串
  9. 在一个RAC集群中最多支持多少节点
  10. leetcode 生成杨辉三角形, 118 119 Pascal&#39;s Triangle 1,2
  11. XamarinForm Effects 调用事件
  12. JS 设计模式一 -- 原型模式
  13. 如何让多个li居中于ul中间
  14. PTA——数组平移
  15. C#开发微信支付之企业向用户付款
  16. CF 1041 1042整理
  17. socket 编程中。 服务端用到多线程
  18. .net 程序加密
  19. Spring Security 中的加密BCryptPasswordEncoder
  20. grep和egrep正则表达式

热门文章

  1. STA之RC Corner
  2. Java8 Time API与老Date之间的转换
  3. Bugku - 好多压缩包 - Writeup
  4. 【转载】Mapreduce实现自定义的InputFormat
  5. codeforces A. Zoning Restrictions Again
  6. Java进阶学习(5)之设计原则(上)
  7. Bootstrap环境安装加使用---开启Bootstrap 之旅
  8. Java - Test - TestNG: testng.xml 简介
  9. jquery 获取 父级 iframe 里的控件对象
  10. queue 官方运用