带队列  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?的更多相关文章

  1. Codeforces 图论题板刷(2000~2400)

    前言 首先先刷完这些在说 题单 25C Roads in Berland 25D Roads not only in Berland 9E Interestring graph and Apples ...

  2. 【Codeforces 20C】 Dijkstra?

    [题目链接] 点击打开链接 [算法] dijkstra [代码] #include<bits/stdc++.h> using namespace std; typedef long lon ...

  3. CodeForces 【20C】Dijkstra?

    解题思路 heap+Dijkstra就能过.注意边是双向边,要用long long. 附上代码 #include <iostream> #include <queue> #in ...

  4. 蒟蒻修养之cf橙名计划

    因为太弱,蒟蒻我从来没有上过div1(这就是今年的最后愿望啊啊啊啊啊)已达成................打cf几乎每次都是fst...........所以我的cf成绩图出现了惊人了正弦函数图像.. ...

  5. [CF787D]遗产(Legacy)-线段树-优化Dijkstra(内含数据生成器)

    Problem 遗产 题目大意 给出一个带权有向图,有三种操作: 1.u->v添加一条权值为w的边 2.区间[l,r]->v添加权值为w的边 3.v->区间[l,r]添加权值为w的边 ...

  6. cf自训4.10

    cf933A dp题 一开始看错是连续子序列了,然后样例刚好能过.. 然后正解没想出来,网上看了题解:感觉正解是枚举2开始的位置,然后再枚举翻转的区间,pos左右两侧分别求出贡献最大的那个区间,左右两 ...

  7. hdu 2066 一个人的旅行(dijkstra)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. #HDU 3790 最短路径问题 【Dijkstra入门题】

    题目: 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. Dijkstra 单源最短路径算法

    Dijkstra 算法是一种用于计算带权有向图中单源最短路径(SSSP:Single-Source Shortest Path)的算法,由计算机科学家 Edsger Dijkstra 于 1956 年 ...

随机推荐

  1. win10安装mysql一直卡在最后一步进行不下去

    新买的电脑,mysql的win10一直安装不了,一直卡在最后一步.仔细阅读下面文章解决. https://blog.csdn.net/fpga_zy/article/details/80689265

  2. jquery与原生JS实现增加、减小字号功能

    预览效果: 实现代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  3. Signals的使用(通知)

    https://docs.djangoproject.com/en/2.1/topics/signals/

  4. ffmpeg使用经验

    1.工作要使用ffmpeg将视频转换成H264格式,网上查到的很多使用方法都是如下: ffmpeg -i input.mov -c:v libx264 -crf output.mov -i后面表示输入 ...

  5. 在delphi中XLSReadWriteII.组件的应用实例(1)

    第三方组件:XLSReadWriteII.v.5.20.67_XE3 实例源码如下: unit Unit1; interface uses Winapi.Windows, Winapi.Message ...

  6. 常用的stm32库函数

    //初始化的方式:先定义初始化机构体.再打开时钟使能.在对每一组GPIO口进行初始化. GPIO_InitTypeDef LED_GPIO; RCC_APB2PeriphClockCmd(RCC_AP ...

  7. ajax----tomact服务器运行

    一.菜鸟教程的代码本地运行 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  8. python安装scrapy等库需要c++ 14.0 下载链接放这里

    百度网盘下载地址:https://pan.baidu.com/s/1zZ7oKSuniABh1y7p0YahgA 或扫描二维码:

  9. C++ is_same

    is_same template< class T, class U > struct is_same; 如果T与U具有同一const-volatile限定的相同类型,则is_same&l ...

  10. linux grep (linux查找关键字在php出现的次数)

    http://www.th7.cn/system/lin/201508/127681.shtml 查找CleverCode在当前目录以及子目录,所有的php出现大于0的次数. # find -type ...