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 ...
随机推荐
- PHP的CI框架流程基本熟悉
CI框架是PHP的一个快速开发框架,我是目前的公司项目后台语言用的PHP,因为我做前端开发,需要用php去填充页面数据,所以就开始去了解这个框架,学习了一些php和数据库的东西,这篇文章先具体介绍CI ...
- Gsp
Gsp 接触到了groovy开发,自然就接触到了gsp.在grails开发中,gsp作为Grails的视图技术,事实上, 不过是标准 HTML 加上一些提供动态内容的 Grails 标记而已. gsp ...
- Python Selenium + phantomJS 模拟登陆教务管理系统 “抢课”
# _*_coding:utf-8_*_ from selenium import webdriver from selenium.webdriver.common.action_chains imp ...
- python编码的那些事
字符串编码在python里是经常会遇到的问题,特别是写文件或是网络传输调用某些函数的时候. 现在来看看python中的unicode编码和utf-8编码 字符串编码的历史 计算机只能处理数字,文本转换 ...
- ------- 软件调试——还原 QQ 过滤驱动对关键内核设施所做的修改 -------
-------------------------------------------------------------------------------- 在前一篇博文中,我们已经处理完最棘手的 ...
- Exchanger
Exchanger可以在两个线程之间交换数据,只能是2个线程,不支持更多的线程之间互换数据. 当线程A调用Exchange对象的exchange()方法后,他会进入阻塞状态,直到线程B也调用了exch ...
- 洛谷 [P1129] [ZJOI2007] 矩阵游戏
这竟然是一道二分图 乍一看,可能是用搜索做,但是这个数据范围,一定会T. 我们观察发现,无论怎样变换,同一行的一定在同一行,同一列的一定还在同一列.所以说,一行只能配一列.这样,我们的目标就是寻找是否 ...
- ES6字符串
1.unicode表示法: alert("\u0061"); alert("\uD842\uDFB7"); alert("\u20BB7") ...
- python进行各类API的使用
前言: 献上歌曲一首: 因为快要上学了,昨天晚上熬夜.然后今天早上起床 没有什么精神.吃完午饭后开始思考今天写什么好呢 然后逛着逛着逛到了一个API网站.感觉还不错就爬了 0x01: 环境:windo ...
- 定时跳转的两种方式(html + javaweb)
html方式 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...