cf 20C Dijkstra?
带队列 dijkstra
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include<memory.h>
#include<algorithm>//reverse
using namespace std;
#define maxn 100002
#define INF 65
struct node
{
int u;
int w;
node(long long x,long long y)
{
u = x;
w = y;
}
bool operator < ( const node& p ) const
{ return w > p.w; }
};
vector<long long>g[maxn];
vector<long long>cost[maxn];
long long d[maxn];
long long par[maxn];
int dijk(int n)
{
memset(d, INF, sizeof(d));
memset(par, -, sizeof(par));
priority_queue<node>q;
q.push(node(,));
d[]=;
while(!q.empty())
{
node top = q.top();
q.pop();
int uu = top.u;
if(uu == n) return d[n];
for(int i = ;i < g[uu].size(); i++)
{
int v = g[uu][i];
if(d[uu] + cost[uu][i] < d[v])
{
d[v] = d[uu] + cost[uu][i];
par[v] = uu;
q.push(node(v,d[v]));
}
}
}
return -;
}
int main()
{
//freopen("input.txt","r",stdin);
int n,e,u,v;
long long w;
cin>>n>>e;
for(int i = ; i < e; i++)
{
cin>>u>>v>>w;
g[u].push_back(v);cost[u].push_back(w);
g[v].push_back(u);cost[v].push_back(w); }
w = dijk(n);
if(w == -) cout<<"-1"<<endl;
else
{
int t = n;
int s[maxn];
int k = ;
while(t != -)
{
s[k++] = t;
t = par[t];
}
for(int j = k - ; j >= ; j--)
cout<<s[j]<<" ";
cout<<endl;
}
}
cf 20C Dijkstra?的更多相关文章
- Codeforces 图论题板刷(2000~2400)
前言 首先先刷完这些在说 题单 25C Roads in Berland 25D Roads not only in Berland 9E Interestring graph and Apples ...
- 【Codeforces 20C】 Dijkstra?
[题目链接] 点击打开链接 [算法] dijkstra [代码] #include<bits/stdc++.h> using namespace std; typedef long lon ...
- CodeForces 【20C】Dijkstra?
解题思路 heap+Dijkstra就能过.注意边是双向边,要用long long. 附上代码 #include <iostream> #include <queue> #in ...
- 蒟蒻修养之cf橙名计划
因为太弱,蒟蒻我从来没有上过div1(这就是今年的最后愿望啊啊啊啊啊)已达成................打cf几乎每次都是fst...........所以我的cf成绩图出现了惊人了正弦函数图像.. ...
- [CF787D]遗产(Legacy)-线段树-优化Dijkstra(内含数据生成器)
Problem 遗产 题目大意 给出一个带权有向图,有三种操作: 1.u->v添加一条权值为w的边 2.区间[l,r]->v添加权值为w的边 3.v->区间[l,r]添加权值为w的边 ...
- cf自训4.10
cf933A dp题 一开始看错是连续子序列了,然后样例刚好能过.. 然后正解没想出来,网上看了题解:感觉正解是枚举2开始的位置,然后再枚举翻转的区间,pos左右两侧分别求出贡献最大的那个区间,左右两 ...
- hdu 2066 一个人的旅行(dijkstra)
一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- #HDU 3790 最短路径问题 【Dijkstra入门题】
题目: 最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Dijkstra 单源最短路径算法
Dijkstra 算法是一种用于计算带权有向图中单源最短路径(SSSP:Single-Source Shortest Path)的算法,由计算机科学家 Edsger Dijkstra 于 1956 年 ...
随机推荐
- 1.微信小程序里如何设置当地时间?
方法一: 1.效果图 2.wxml code: <!--pages/index/index.wxml--> <text>当前时间:{{time}}</text> & ...
- spark java.lang.OutOfMemoryError: unable to create new native thread
最近迁移集群,在hadoop-2.8.4 的yarn上跑 spark 程序 报了以下错误 java.lang.OutOfMemoryError: unable to create new native ...
- python3 pyinstaller生成exe文件过程问题解决记录
1.使用pip安装pyinstaller 2.在cmd打开需生成可执行文件的python文件所在文件夹 3.使用命令pyinstaller -F -w **.py (代码中有import其他模块的,只 ...
- echarts属性的设置(完整大全)
// 全图默认背景 // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87cefa','#da70d6','#32cd ...
- Spring MVC和Spring Boot的理解以及比较
Spring MVC是什么?(1)Spring MVC是Spring提供的一个强大而灵活的模块式web框架.通过Dispatcher Servlet, ModelAndView 和 View Reso ...
- Rocket MQ 1 - 用
参考 http://www.iocoder.cn/categories/RocketMQ/ ; https://www.jianshu.com/nb/16219849 首先上启动方法,分别启动name ...
- python中并发编程基础1
并发编程基础概念 1.进程. 什么是进程? 正在运行的程序就是进程.程序只是代码. 什么是多道? 多道技术: 1.空间上的复用(内存).将内存分为几个部分,每个部分放入一个程序,这样同一时间在内存中就 ...
- 通过windows远程桌面连接CentOS系统
前提: CentOS安装桌面,如果无桌面,请执行 # yum -y groups install "GNOME Desktop" # startx 1 2 配置源 # yum in ...
- 服务器解析慢,可以安装nscd解决
针对服务器解析慢,可以在服务器上安装nscd,就可以把解析缓存起来,不用每次都解析 安装nscd: yum -y install nscd chkconfig nscd on service nscd ...
- JS获取URL中文参数乱码的解决方法
浏览器URL参数值中带有汉字字符,在接收时直接获取会出现乱码,下面是解决方法(传递前不需要encodeURI): function getUrlVars() { var vars = [], hash ...