【Codeforces 20C】 Dijkstra?
【题目链接】
【算法】
dijkstra
【代码】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e15;
const ll MAXN = 1e5; ll n,m,u,v,w,i,p,to,cost,x;
ll dist[MAXN+],vis[MAXN+],last[MAXN+];
priority_queue< pair<ll,ll> > q;
vector< pair<ll,ll> > E[MAXN+];
vector<ll> path; template <typename T> inline void read(T &x) {
ll f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < ) { x = -x; putchar('-'); }
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
} int main() { read(n); read(m);
for (i = ; i <= m; i++) {
read(u); read(v); read(w);
E[u].push_back(make_pair(v,w));
E[v].push_back(make_pair(u,w));
}
for (i = ; i <= n; i++) dist[i] = INF;
q.push(make_pair(,));
while (!q.empty()) {
x = q.top().second; q.pop();
if (vis[x]) continue;
vis[x] = ;
for (i = ; i < E[x].size(); i++) {
to = E[x][i].first;
cost = E[x][i].second;
if (dist[x] + cost < dist[to]) {
dist[to] = dist[x] + cost;
q.push(make_pair(-dist[to],to));
last[to] = x;
}
}
} if (dist[n] == INF) {
puts("-1");
return ;
} p = n;
while (p) {
path.push_back(p);
p = last[p];
} reverse(path.begin(),path.end()); for (i = ; i < path.size(); i++) {
write(path[i]);
if (i < path.size() - ) putchar(' ');
} return ;
}
【Codeforces 20C】 Dijkstra?的更多相关文章
- CodeForces 【20C】Dijkstra?
解题思路 heap+Dijkstra就能过.注意边是双向边,要用long long. 附上代码 #include <iostream> #include <queue> #in ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 496E】Distributing Parts
[题目链接]:http://codeforces.com/contest/496/problem/E [题意] 给你n个歌曲; 每个歌曲有一个需要声音的区间li,ri; 然后给你m个人; 每个人也有一 ...
- 【17.07%】【codeforces 583D】Once Again...
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【22.73%】【codeforces 606D】Lazy Student
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【30.43%】【codeforces 746C】Tram
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【Codeforces 498B】 B. Name That Tune (概率DP)
B. Name That Tune time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- RabbitMQ核心组件及应用场景
一.适用场景 1.解耦 2.最终一致性 3.广播 4.错峰与流控(秒杀业务用于流量削峰场景) 秒杀场景 二.核心组件,关键点(交换器.队列.绑定) AMPQ消息路由必要三部分:交换器.队列.绑定. J ...
- Go视频教程整理转
Go视频教程整理 [Go Web基础]01博客项目设计 |Go视频教程|Go语言基础 http://www.tudou.com/programs/view/gXZb9tGNsGU/ [Go Web基础 ...
- oracle内核学习总结
http://blog.csdn.net/bcbobo21cn/article/category/3092145/1
- String、StringBuffer和StringBuilder的差别
String.StringBuffer和StringBuilder的差别 1.String类是不可变类,即一旦一个String对象被创建后.包括这个对象中的字符序列是不可改变的 2.在字符串拼接的过程 ...
- 走入asp.net mvc不归路:[6]linq常见用法
asp.net mvc结合linq,先不说性能问题,对于增删查改的操作还是相当方便的.以下我们就来介绍一下linq在asp.net mvc的Controller中的常见用法. 1 首先来看看整个数据表 ...
- 服务器----1U、2U、3U、4U
U是一种表示服务器外部尺寸的单位,是unit的缩略语,详细的尺寸由作为业界团体的美国电子工业协会(EIA)所决定. 之所以要规定服务器的尺寸,是为了使服务器保持适当的尺寸以便放在铁质或铝质的机架上.机 ...
- 设计模式入门之訪问者模式Visitor
//訪问者模式定义:表示一个作用于某对象结构中的各个元素的操作,它使你能够在不改变各元素类的前提下定义作用于这些元素的新操作. //从定义上看.这个模式跟装饰模式的定义非常类似(动态地给一个对象加入一 ...
- npm的常用配置
第一次使用需要初始化 npm init -y npm-tut { "requires": true, "lockfileVersion": 1, "d ...
- C#高阶与初心:(二)P/Invoke平台调用
最近某个项目要采集交易终端的信息用于监管,主要厂商给出了API,C++版的...开启hard模式!!! C#调用C++的DLL基本就两种方法:加一个VC++项目包一层,或者使用P/Invoke(平台调 ...
- 文件宝局域网传输/播放功能使用帮助(Windows电脑用户)
使用局域网账户密码登录,可以访问电脑上所有文件 使用游客无账户密码登录,只能访问电脑上指定共享文件夹的文件. 1.怎么设置共享文件夹请参考: 方法1 1.在文件资源管理器中选择自己一个想共享的文件夹, ...