dij模板
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
struct edge
{
int to,val;
};
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
vector<edge>e[2505];
int dis[2505];
int vis[2505];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
edge tmp;
tmp.to=y;
tmp.val=z;
e[x].push_back(tmp);
tmp.to=x;
tmp.val=z;
e[y].push_back(tmp);
}
for(int i=1;i<=n;i++)
{
dis[i]=2147483647;
}
dis[1]=0;
q.push(make_pair(0,1));
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(vis[x]==1)
continue;
vis[x]=1;
for(int i=0;i<e[x].size();i++)
{
int y=e[x][i].to;
if(dis[x]+e[x][i].val<dis[y])
{
dis[y]=dis[x]+e[x][i].val;
q.push(make_pair(dis[y],y));
}
}
}
printf("%d\n",dis[n]);
return 0;
}
dij模板的更多相关文章
- 堆优DIJ模板
Dij:贪心思想的单源最短路,时间复杂度O(n^2). Dij算法流程: d数组记录源点s到每个点的距离,若无边则设为inf,标记源点: 选出d数组中未标记的最小值,该节点记为k,并标记k为已求出最短 ...
- noip考前模板大整理
//归并排序求逆序对 #include<bits/stdc++.h> #define ll long long using namespace std; ]; ll ans; ]; voi ...
- hdu 2112 HDU Today(map与dijkstra的结合使用)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【POJ3377】Ferry Lanes 最短路
我仅仅是贴一下手写堆优化的dij模板.尽管.它.TLE了--**** #include <cstdio> #include <cstring> #include <ios ...
- BZOJ4152 The Captain(dijkstra+巧妙建图)
BZOJ4152 The Captain 题面很简洁: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 很明显 ...
- POJ1797 Heavy Transportation (堆优化的Dijkstra变形)
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
- The Shortest Statement CodeForces - 1051F 最小生成树+并查集+LCA
题目描述 You are given a weighed undirected connected graph, consisting of n vertices and mm edges. You ...
- dij单源最短路纯模板
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> ...
- UVA 10801 Dij最短路(改模板)
题意:有n个电梯,目的地是第K层(起点是第0层),给出每个电梯的速度,以及每个电梯能到达的层数,如果中途需要换电梯的话,时间需要+60,求到达目的地的最短时间: 思路:Dij求最短路.如果是另一条路比 ...
随机推荐
- enum 枚举一般用法 dotnet
public enum Demo { [Description("Moning描述")] Moning = , [Description("Afternoon描述&quo ...
- 网站运行编译器错误CS1617: 选项“6”对 /langversion 无效;必须是 ISO-1、ISO-2、3、4、5 或 Default
运行winform程序时提示, CS1617: 选项“6”对 /langversion 无效:必须是 ISO-1.ISO-2.3.4.5 或 Default 找到网站的web.config配置文件,找 ...
- js基础知识总结:函数
函数内部的属性: arguments 和this是函数内部的两个特殊对象 arguments: function recursion(num){ if(num<=1){ return 1; }e ...
- Elevated privileges for Delphi applications
BY CRAIG CHAPMAN · PUBLISHED 2015-06-08 · UPDATED 2015-06-08 One of my customers recently asked th ...
- Oracle emca on linux
http://blog.csdn.net/haibusuanyun/article/details/16338591 bash-3.2$ lsnrctl status LSNRCTL for Lin ...
- PyCharm安装MicroPython插件
转载请注明文章来源,更多教程可自助参考docs.tpyboard.com,QQ技术交流群:157816561,公众号:MicroPython玩家汇 前言 PyCharm可以说是当今最流行的一款Pyth ...
- 解决SpringBoot多模块发布时99%的问题?SpringBoot发布的8个原则和4个问题的解决方案
如果使用 SpringBoot 多模块发布到外部 Tomcat,可能会遇到各种各样的问题.本文归纳了以下 8 个原则和发布时经常出现的 4 个问题的解决方案,掌握了这些原则和解决方案,几乎可以解决绝大 ...
- 安装使用Cloudera Impala
安装与使用Cloudera Impala Cloudera Impala提供快速的.交互式的SQL查询方式,直接基于Apache Hadoop存储在HDFS或HBase中的数据进行查询.除了使用与Ap ...
- Python处理图片
# -*- coding: UTF-8 -*- from PIL import Image import os import sys reload(sys) sys.setdefaultencodin ...
- ECMAScript6 语法
声明变量 var let const var(声明局部变量,有局部外可用缺陷) 代码 <!DOCTYPE html> <html lang="en"> &l ...