POJ - 2387 最短路
思路:用dijkstra算法,是无向图。
AC代码:
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1000 + 5;
int d[maxn];
bool vis[maxn];
struct Edge{
int from, to, dist;
Edge(){}
Edge(int u, int v, int d):from(u), to(v), dist(d) {}
};
vector<Edge>edge;
struct HeapNode{
int d, u;
HeapNode() {}
HeapNode(int d, int u):d(d), u(u) {}
bool operator < (const HeapNode &p) const {
return d > p.d;
}
};
vector<int>G[maxn];
void init(int n) {
edge.clear();
for(int i = 0; i <= n; ++i) G[i].clear();
}
void addEdge(int from, int to, int dist) {
edge.push_back(Edge(from, to, dist));
int m = edge.size();
G[from].push_back(m-1);
}
void dijkstra(int s) {
priority_queue<HeapNode>q;
memset(d, inf, sizeof(d));
d[s] = 0;
memset(vis, 0, sizeof(vis));
q.push(HeapNode(0, s));
while(!q.empty()) {
HeapNode p = q.top(); q.pop();
int u = p.u;
if(vis[u]) continue;
vis[u] = 1;
for(int i = 0; i < G[u].size(); ++i) {
Edge &e = edge[G[u][i]];
if(d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
q.push(HeapNode(d[e.to], e.to));
}
}
}
}
int main() {
int n, m;
while(scanf("%d%d", &m, &n) == 2) {
init(n);
int u, v, dis;
for(int i = 0; i < m; ++i) {
scanf("%d%d%d", &u, &v, &dis);
addEdge(u, v, dis);
addEdge(v, u, dis);
}
dijkstra(n);
printf("%d\n", d[1]);
}
return 0;
}
如有不当之处欢迎指出!
POJ - 2387 最短路的更多相关文章
- 链式前向星版DIjistra POJ 2387
链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
hdu 2544 求点1到点n的最短路 无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home
求1到N的最短路 注意有重边 跑一遍dijkstra就行 /* *********************************************** Author :Sun Yuefeng ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
随机推荐
- python_hello word!
什么是编程? --模拟现实世界,跨时间,地点,不受外界坏境干扰到的虚拟现实世界,数字化的可定制化的世界 编程语言有哪些? python ,java,php ,c++,等 python开发环境准备工作 ...
- 2017 .NET 開發者須知
筆記-Scott Hanselman 的 2017 .NET 開發者須知 转载http://blog.darkthread.net/post-2017-01-16-dotnet-dev-should- ...
- c++ singleton单例模式
方法1:加锁的经典懒汉实现: class singleton { public: static pthread_mutex_t mutex; static singleton* initance(); ...
- JMeter之断言 - 响应文本
1. 响应数据: 2. 添加响应断言: 3.设置响应断言,本例中 设置 响应文本 中 包括 success 字符串的 为真,即通过. 4.如果设置 响应文本 中 包括 error 字符串的 为真, ...
- 零基础实现node+express个性化聊天室
本篇文章使用node+express+jquery写一个个性化聊天室,一起来get一下~(源码地址见文章末尾) 效果图 项目结构 实现功能 登录检测 系统自动提示用户状态(进入/离开) 显示在线用户 ...
- AngularJS执行流程详解(转)
一.启动阶段 大家应该都知道,当浏览器加载一个HTML页面时,它会将HMTL页面先解析成DOM树,然后逐个加载DOM树中的每一个元素节点.我们可以把AngularJS当做一个类似jQuery的js库, ...
- oracle光标的使用
以下plsql程序用的scott用户的dept,emp表. 1.光标的使用: --查询并打印员工的姓名名和薪水 /* 光标属性: %found %notfound */ set serveroutpu ...
- jQuery中的DOM操作------复制及包裹节点
1.复制节点: 如果单击<li>元素后需要再复制一个<li>元素,可以用clone()方法来完成: $(this).clone().appendTo("ul" ...
- Keil中搭建自动化单元测试框架Unity
前言: 虽然一些C++的自动化单元测试框架也能用来C语言单元测试,但那样我们编写C语言程序时需要符合C++的标准,这样有一些C的特性是无法使用的,限制C的特性使用不太好,于是找了一个全部用C实现的自动 ...
- BZOJ 3239: Discrete Logging [BGSG]
裸题 求\(ind_{n,a}b\),也就是\(a^x \equiv b \pmod n\) 注意这里开根不能直接下取整 这个题少了一些特判也可以过... #include <iostream& ...