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 中的一个数 做法分析 先这样考 ...
随机推荐
- 7.SpringMVC注解优化
新建controller,每个人都可以建自己的controller,比较方便,不用再在web.xml中进行配置,value的请求地址一定要唯一. 优化: 1.上面示例中spring-annotatio ...
- 【leetcode】Anagrams (middle)
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- yum安装时出现:Cannot retrieve metalink for repository: epel. Please verify its path and try again
在CentOS 6.3 x86_64下安装php-mcrypt的时候出现了问题:Error: Cannot retrieve metalink for repository: epel. Please ...
- myeclipse破解
由于内容比较多,我就直接转载了 ,同时感谢原博主 http://blog.itpub.net/27042095/viewspace-1164998/
- IOS-Uikit框架介绍
•UIKit可识别三种类型的输入事件: –触摸事件 –运动(加速计)事件 –远程控制事件 IKit框架将触击信息封装为一个UIEvent对象,并派发给恰当的视图(有关UIKit如何将事件递送给您的视图 ...
- php数据访问(批量删除)
批量删除: 首先给每一行加上复选框,也就是在自增长列内加入checkbox.因为这里可以多选,也可以单选,所以在传值的时候需要传一个数组来进行处理,所以复选框name的值设定一个数组.传值都是传的va ...
- Hibernate简单分页
5.1 准备工作 建立项目,加入jar 建立hibernate.cfg.xml 建立pojo类和对应的映射文件 5.2 建立vo类PageEntity package org.guangsoft.vo ...
- 史上最全web.xml配置文件元素详解
一.web.xml配置文件常用元素及其意义预览 <web-app> <!--定义了WEB应用的名字--> <display-name></display-na ...
- ios wax热更新之安装wax(xcode7.3.1)
通过Finder浏览到你保存该项目的文件夹.创建三个新的文件夹:wax.scripts和Classes. 第一:首先,下载源代码的压缩包.Wax放在GitHub上(https://github.com ...
- [Android] adb shell dumpsys的使用
reference to :http://blog.csdn.net/g19920917/article/details/38032413 有两种方法可以查看service list: 1. adb ...