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 ...
随机推荐
- 提示让IE8以下版本的浏览器去更新浏览器
需求: 强制让IE进行页面渲染时候,用最新的方式渲染,并提示让IE8以下的版本去更新浏览器并给一个连接地址 那,如何满足? <!DOCTYPE html> <html lang=&q ...
- 国家语言,语言代码,locale id对应表
国家语言,语言代码,locale id对应表.比如 en_US对应的id为1033, 中文的locale=zh_CN,id=2052. Locale Languagecode LCIDstring L ...
- Linux指令--wget
Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...
- android webview加载网络连接
webview = (WebView) findViewById(R.id.webview); WebSettings webSettings = webview.getSettings(); //设 ...
- 【转】城市CORS系统建设
随着GPS技术的飞速进步和应用普及,它在城市测量中的作用已越来越重要.当前,利用多基站网络RTK技术建立的连续运行卫星定位服务综合系统(Continuous Operational Reference ...
- 【转】Linux方向职业分析
引言: 据了解,Linux普通网络管理人员的月薪大约5000元左右,负责编程的Linux软件工程师月薪大约在8000元到12000元之间,Linux嵌入式软件开发人员的月薪大约在12000元以上. 影 ...
- Zabbix3.4部署
Zabbix简介 zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活 ...
- 解决mysql、vsftp远程连接速度慢的问题
以 centOS 6.3(其他操作系统类似,同样适用)说明: 当我们的服务都配置正常的情况下,有时会出现连接速度慢而导致连接失败的问题 问题分析:这些情况一般都是DNS解析惹的祸 mysql连接速度慢 ...
- The Go Programming Language. Notes.
Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...
- [Sdoi2017]数字表格 [莫比乌斯反演]
[Sdoi2017]数字表格 题意:求 \[ \prod_{i=1}^n \prod_{j=1}^m f[(i,j)] \] 考场60分 其实多推一步就推倒了... 因为是乘,我们可以放到幂上 \[ ...