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求最短路.如果是另一条路比 ...
随机推荐
- java模拟post请求发送json数据
import com.alibaba.fastjson.JSONObject; import org.apache.http.client.methods.CloseableHttpResponse; ...
- TIME WINAPI
GetDynamicTimeZoneInformation https://msdn.microsoft.com/en-us/library/windows/desktop/ms724318(v=vs ...
- Linux下的软件安装
在线安装 APT:advanced packaging Tool,Debian及其派生的发行版的软件包管理工具,包含以apt-开头的多个工具,如apt-get,apt-cache,apt-cdrom ...
- Linux使用技巧:linux下将命令值赋给shell变量
很多小伙伴在写shell脚本的时候需要把命令输出的值赋给一些变量,使得脚本在运行过程中能够顺利使用这些变量.例如:很多时候我们就需要获取当前目录的绝对路径,pwd这个命令大家在熟悉不过,可是要把这个命 ...
- 深入浅出RPC——浅出篇(转载)
本文转载自这里是原文 近几年的项目中,服务化和微服务化渐渐成为中大型分布式系统架构的主流方式,而 RPC 在其中扮演着关键的作用. 在平时的日常开发中我们都在隐式或显式的使用 RPC,一些刚入行的程序 ...
- python requests模块session的使用建议及整个会话中的所有cookie的方法
话不多说,直接上代码 测试代码 服务端 下面是用flask做的一个服务端,用来设置cookie以及打印请求时的请求头 # -*- coding: utf-8 -*- from flask import ...
- Spark之SparkSql
-- Spark SQL 以编程方式指定模式 val sqlContext = new org.apache.spark.sql.SQLContext(sc) val employee = sc.te ...
- hgoi#20190519
更好的阅读体验 来我的博客观看 T1-求余问题 Abu Tahun很喜欢回文. 一个数组若是回文的,那么它从前往后读和从后往前读都是一样的,比如数组{1},{1,1,1},{1,2,1},{1,3,2 ...
- HBase —— 单机环境搭建
一.安装前置条件说明 1.1 JDK版本说明 HBase 需要依赖JDK环境,同时HBase 2.0+ 以上版本不再支持JDK 1.7 ,需要安装JDK 1.8+ .JDK 安装方式见本仓库: Lin ...
- 带你手写基于 Spring 的可插拔式 RPC 框架(一)介绍
概述 首先这篇文章是要带大家来实现一个框架,听到框架大家可能会觉得非常高大上,其实这和我们平时写业务员代码没什么区别,但是框架是要给别人使用的,所以我们要换位思考,怎么才能让别人用着舒服,怎么样才能让 ...