带队列  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. Mysql 和 SQLServer 使用SQL差异比较

    查询前100条数据 #mysql ; #sqlserver * from table_name ; 从数据库.表 定位表 #mysql写法:库名.表名 select password from Inf ...

  2. threading join用法

    join():在子线程完成运行之前,这个子线程的父线程将一直被阻塞 import threading #线程import time def Beijing(n): print('Beijing tim ...

  3. JavaScript学习-4——DOM对象、事件

    本章目录 --------window对象 --------document对象 --------事件 一.window对象 函数调用: 自己封装的函数只写:函数名(): 数学函数Math 例:绝对值 ...

  4. 第二篇*1、Python基本数据类型

    数据类型: 变量可以处理不同类型的值,基本的类型是数和字符串.使用变量时只需要给它们赋一个值.不需要声明或定义数据类型.Python3 中有六个标准的数据类型:Number(数字),String(字符 ...

  5. 尚硅谷springboot学习30-docker安装mysql示例

    docker pull mysql 错误的启动示例 错误日志:需要设置密码 正确的启动 但还不能直接使用,因为没有做端口映射,外界无法连接 可用的启动 连接成功 几个高级的操作 指定配置文件 dock ...

  6. Mybatis控制台打印sql

    mybatis-config.xml配置如下: <configuration> <settings> <setting name="lazyLoadingEna ...

  7. 前端 跨Area后Cookie无法访问

    创建两个区域,一个是User,一个是Manage. User区域有两个页面,index1,和index2 User区域: index1:负责写入cookie index2:负责读取cookie Man ...

  8. html迪士尼网页实现代码

    html body>     <div>         <!-- 导航设置 -->         <header>             <nav ...

  9. 如何利用sql 读取辅表的最大max 和第二最大max。。。。

    SELECT `主表`.id, `主表`.title, `辅表`.* FROM tableB AS `辅表` INNER JOIN tableA AS `主表` ON `主表`.id = `辅表`.f ...

  10. PhoenixFD插件流体模拟——UI布局【Resimulation】详解

    Liquid Resimulation 流体再(重)渲染 本文主要讲解Resimulation折叠栏中的内容 主要内容 Overview 综述 Parameters 参数 综述 Liquid Resi ...