【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; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- AC日记——[USACO08DEC]干草出售Hay For Sale 洛谷 P2925
题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...
- PAT (Advanced Level) 1086. Tree Traversals Again (25)
入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- Jetson TK1 一:调整屏幕分辨率
先点击分辨率调整的那个对话框,就是1024×768之类的那个,选定一个分辨率,我选的是1366x768(16:9)的那个,然后连续按7下tab按键,然后回车,然后再点击弹出的选项右边一个就能保存了.
- TreeView 与 ListView
ListView: viewStyle icon 大图标 list 列表,单列 report 报表 smallIcon 小图标 largeImage 与icon对应 smallImage 与saml ...
- Jquery表单序列化和json操作
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jmeter自定义并发用户数图形插件介绍
Stepping Thread Group马上要被废弃了,废弃原因不知道,官方推荐使用 BlazeMeter Inc.公司贡献的插件Concurrency Thread Group,配合 Throug ...
- 王立平--Gallery:实现图片的左右滑动
<span style="font-size:18px;color:#330033;">package com.main; import android.app.Act ...
- GBDT调参
gbm算法流程图: gbdt 参数:参考scikit-learn The overall parameters can be divided into 3 categories: Tree-Speci ...
- MapReduce编程实战之“调试”和"调优"
本篇内容 在上一篇的"初识"环节,我们已经在本地和Hadoop集群中,成功的执行了几个MapReduce程序,对MapReduce编程,已经有了最初的理解. 在本篇文章中,我们对M ...
- android的armeabi和armeabi-v7a
在ANE中如果SDK调用了so库,则需要把so库放到ANE下Android-ARM/lib/armeabi (调试模式)或者 armeabi-v7a(发行模式)下. 可以贴个ADT代码说明问题: // ...