【题目链接】

点击打开链接

【算法】

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

  1. CodeForces 【20C】Dijkstra?

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

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 496E】Distributing Parts

    [题目链接]:http://codeforces.com/contest/496/problem/E [题意] 给你n个歌曲; 每个歌曲有一个需要声音的区间li,ri; 然后给你m个人; 每个人也有一 ...

  4. 【17.07%】【codeforces 583D】Once Again...

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【22.73%】【codeforces 606D】Lazy Student

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【30.43%】【codeforces 746C】Tram

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【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 ...

  8. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  9. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

随机推荐

  1. 高性能mysql之schema与数据类型优化

    1.数据类型 http://www.cnblogs.com/YDDMAX/p/4937770.html

  2. ueditor整合之后前段不显示高亮

    自己整合ueditor插件后,发现在前段显示的时候并没有高亮处理 在网上看了一些方法后,自己尝试了下. 1.在页面上引入相关的js和css文件 主要是以下两个文件 <script src=&qu ...

  3. IDEA中lombok插件的安装

    1.在pom.xml中添加依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId&g ...

  4. springBoot 跨域处理

    首先喝水不忘挖井人,博客参考:https://www.cnblogs.com/nananana/p/8492185.html 方式一:新增一个configration类 或 在Application中 ...

  5. Tar压缩文件

    [root@test /root]# tar [-zxcvfpP] filename   [root@test /root]# tar -N 'yyyy/mm/dd' /path -zcvf targ ...

  6. 一步步走向国际乱码大赛-- 恶搞C语言

    大家都一直强调规范编码.可是这个世界上有个大师们娱乐的竞赛--国际乱码大赛. 能写出来的都是对语言深入了解的master.我从没想自己也能"恶搞"C,一直都是老老实实编码.就在前几 ...

  7. 若菜acmer感觉自己智商全然被碾压了QAQ~~

    题目大意是:输入n,m,给出n*m(n.m<=100)的不是正规的布满棋子的棋盘,求最少改几个棋子能够使得棋盘正规,正规的棋盘必须是每一个相邻的棋子颜色都不同(仅仅有黑白两种,用0,1取代) 比 ...

  8. C# 实体类序列化与反序列化一 (XmlSerializer)

    /// <summary> /// 实体类序列化的反序列化的类 /// </summary> /// <typeparam name="T">& ...

  9. 向量空间模型实现文档查询(Vector Space Model to realize document query)

    xml中文档(query)的结构: <topic> <number>CIRB010TopicZH006</number> <title>科索沃難民潮&l ...

  10. Javascript中没有引用传递,只有按值传递

    很多人,包括我,受书本知识消化不彻底的影响,认为 JS 中参数有两种传递方式:数字.字符串等按值传递:数组.对象等按地址(引用)传递.对此种观点,我们要谨慎. var v1 = [] var v2 = ...