Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)
题目链接:http://codeforces.com/problemset/problem/20/C
思路:需要用优化过的dijkstra,提供两种写法。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define TR(iter, c) for (__typeof(c.begin()) iter = c.begin(); iter != c.end(); ++iter)
using namespace std; const int MAX_N = (100000 + 100);
const long long inf = 1LL << 60;
int N, M, pre[MAX_N];
long long dist[MAX_N]; struct cmp {
bool operator() (const int &p1, const int &p2) const {
return dist[p1] <= dist[p2];
}
};
vector<pair<int, int > > g[MAX_N];
set<int, cmp> q; void Dijkstra()
{
dist[1] = 0;
q.insert(1);
while (!q.empty()) {
int u = *q.begin();
q.erase(q.begin());
REP(i, 0, (int)g[u].size()) {
int v = g[u][i].first, w = g[u][i].second;
if (dist[u] + w < dist[v]) {
q.erase(v);
dist[v] = dist[u] + w;
pre[v] = u;
q.insert(v);
}
}
}
} void dfs(int u)
{
if (u != 1) dfs(pre[u]);
printf("%d ", u);
}
int main()
{
cin >> N >> M;
FOR(i, 1, N) dist[i] = inf;
FOR(i, 1, M) {
int u, v, w; cin >> u >> v >> w;
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
Dijkstra();
if (dist[N] >= inf) { puts("-1"); return 0; }
dfs(N);
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (100000 + 100);
const long long inf = 1LL << 60;
int N, M, pre[MAX_N], vis[MAX_N];
long long dist[MAX_N]; struct cmp {
bool operator() (const pair<long long, int>&p, const pair<long long, int>&q) const {
return p.first >= q.first;
}
};
vector<pair<int, int > > g[MAX_N];
priority_queue<pair<long long, int>, vector<pair<long long, int> >, cmp> pq; void Dijkstra()
{
pq.push(make_pair(dist[1] = 0, 1));
while (!pq.empty()) {
pair<long long, int> p = pq.top();
pq.pop();
if (vis[p.second]) continue;
vis[p.second] = 1;
REP(i, 0, (int)g[p.second].size()) {
int v = g[p.second][i].first, w = g[p.second][i].second;
if (p.first + w < dist[v]) {
dist[v] = p.first + w;
pre[v] = p.second;
if (!vis[v]) pq.push(make_pair(dist[v], v));
}
} }
} void dfs(int u)
{
if (u != 1) dfs(pre[u]);
printf("%d ", u);
}
int main()
{
cin >> N >> M;
FOR(i, 1, N) dist[i] = inf, vis[i] = 0;
FOR(i, 1, M) {
int u, v, w; cin >> u >> v >> w;
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
Dijkstra();
if (dist[N] >= inf) { puts("-1"); return 0; }
dfs(N);
return 0;
}
Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)的更多相关文章
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2) E. Number With The Given Amount Of Divisors 反素数
E. Number With The Given Amount Of Divisors time limit per test 2 seconds memory limit per test 256 ...
- Codeforces Beta Round #32 (Div. 2, Codeforces format)
Codeforces Beta Round #32 (Div. 2, Codeforces format) http://codeforces.com/contest/32 A #include< ...
- Codeforces Beta Round #31 (Div. 2, Codeforces format)
Codeforces Beta Round #31 (Div. 2, Codeforces format) http://codeforces.com/contest/31 A #include< ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format)
Codeforces Beta Round #29 (Div. 2, Codeforces format) http://codeforces.com/contest/29 A #include< ...
- Educational Codeforces Round 20
Educational Codeforces Round 20 A. Maximal Binary Matrix 直接从上到下从左到右填,注意只剩一个要填的位置的情况 view code //#pr ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
随机推荐
- Unity3d 保存和使用地形高度
TerrainHeightProcesser 地形高度存储工具 TerrainHeightData 地形高度数据 // class TerrainHeightProcesser using Unity ...
- 终端ssh登录mac用shell打包ipa报错:replacing existing signature
终端ssh登录mac用shell打包ipa报错:replacing existing signature 报错原因:login.keychain被锁定,ssh登录的没有访问权限 解决方法:终端敲入 s ...
- MongoDB 数据库管理(不定时更新)
之前的几篇文章大致说了副本集的搭建.副本集的管理,现在说下MongoDB数据库的管理.数据库管理包括:备份.还原.导入.导出.服务器管理等. 一:查看服务器状态,查看命令行参数.db.serverSt ...
- Effective C++ -----条款42:了解typename的双重意义
声明template参数时,前缀关键字class和typename可互换. 请使用关键字typename标识嵌套从属类型名称:但不得在base class lists(基类列)或member init ...
- 将jquery和公共样式缓存到localStorage,可以减少Http请求,从而优化页面加载时间
以下代码: //入口函数 if (window.localStorage) { initJs(); initCss("css", "/gfdzp201508257998/ ...
- SuperIndicator 专做轮播图库,没有之一,支持轮播图无限循环
github地址:https://github.com/hejunlin2013/SuperIndicator SuperIndicator a superindicatorlibray for vi ...
- 【leetcode】Wildcard Matching(hard) ★ 大神太牛了
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 【leetcode】Jump Game I & II (hard)
Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...
- 【leetcode】atoi (hard) ★
虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过 “ 123” ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...