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 中的一个数 做法分析 先这样考 ...
随机推荐
- Appium+Robotframework实现Android应用的自动化测试-1:Appium在Windows中的安装
让我们开始在Windows中开始安装Appium吧,Appium在OS X中的具体安装后面的文章会介绍. 另外,官网上说先要装Node.js,还要装Apache Ant和Apache Maven,Gi ...
- Unity3d 检查哪些prefab引用了某个UIAtlas
适用情景:策划在用NGUI制作UI prefab时经常会使用一些临时的Atlas,然后再想改就不知道都哪些使用了.现在想修改下使用临时资源的GameObject 使用方式,先选中某个prefab或者某 ...
- 转一篇Xcode中利用target编译不同版本的文章
http://www.cocoachina.com/ios/20160331/15832.html 主要说的是,不用自己定义debug宏,而是在xcode的编译配置文件中,设定debug宏,这样,不用 ...
- FastReport里面正确调用函数的方法
FastReport里面正确调用函数的方法 错误: [FormatDateTime('yyyy-mm-dd',[frxDBDataset1."日期"])] --------- ...
- Python:IDLE清屏
清屏很简单,为IDLE增加一个清屏的扩展ClearWindow即可. 首先下载clearwindow.py(点击可直接下载,不能下载的可以右键保存,格式为py结尾), 将这个文件放到Python安装目 ...
- Effective C++ -----条款16:成对使用new和delete时要采取相同形式
如果你在new表达式中使用[],必须在相应的delete表达式中也使用[].如果你在new表达式中不使用[],一定不要在相应的delete表达式中使用[].
- Divide and conquer:Median(POJ 3579)
快速求两数距离的中值 题目大意:给你一个很大的数组,要你求两个数之间的距离的中值 二分法常规题,一个pos位就搞定的事情 #include <iostream> #include ...
- js验证手机号
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【leetcode】Reverse Nodes in k-Group (hard)☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- s:iterator,s:if与OGNL的嵌套使用
今天在写代码时,遇到个如下问题,要求当前登陆用户的id与系统参数类型代码所属维护人的id相同时,显示单选框.如下效果: 代码如下: <s:iterator value="vo.page ...