思路:用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 最短路的更多相关文章

  1. 链式前向星版DIjistra POJ 2387

    链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...

  2. 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 ...

  3. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  4. 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 ...

  5. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  6. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

  7. 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 ...

  8. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home

    求1到N的最短路 注意有重边 跑一遍dijkstra就行 /* *********************************************** Author :Sun Yuefeng ...

  9. 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 ...

随机推荐

  1. OpenCv结构和内容

    OpenCv的结构和内容 OpenCv源码组成结构其中包括cv, cvauex, cxcore, highgui, ml这5个模块 CV:图像处理和视觉算法 MLL:统计分类器 HighGui:GUI ...

  2. Linux指令--nl

    原文出处:http://www.cnblogs.com/peida/archive/2012/11/01/2749048.html nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件 ...

  3. Javascript的异步和回调

    介绍JavaScript的一些同步.异步.单线程多线程,回调基本概念:https://segmentfault.com/a/1190000002999668

  4. 刚买个炼狱蝰蛇1800dpi的下完驱动提示没有发现鼠标

    2017-02-19补充:可以下载下面的程序 ,也可以访问 http://cn.razerzone.com/synapse/  下载雷云 也可解决问题 ------------------------ ...

  5. ImageMagick命令行工具

    [ convert | identify | mogrify | composite | montage | compare | display | animate | import |conjure ...

  6. 2017年总结的前端文章——CSS高级技巧汇总

    1. 页面顶部阴影 body:before{ content: ""; position: fixed; top:-10px; left:; width: 100%; height ...

  7. RHEL7.2安装

    先在系统启动的时候按下Del键(有些系统是F2键)进入BIOS,设置从光盘启动. 系统只有2个USB口时,1个要接光驱,另外1个口不能同时接键盘和鼠标,可以接1个USB集线器,键盘和鼠标同时接入到集线 ...

  8. 使Eclipse下支持编写HTML/JS/CSS/JSP页面的自动提示

    1.打开eclipse→Windows→Preferences→Java→Editor→Content Assist 修改Auto Activation triggers for java的值为:zj ...

  9. Spring整合JMS(三)——MessageConverter介绍

    原文链接:http://haohaoxuexi.iteye.com/blog/1900937 1.4     消息转换器MessageConverter MessageConverter的作用主要有两 ...

  10. web前端性能优化问题

    常用的几大优化解决: 页面内容的优化 减少http请求 途径: 1>启用http/2--越来越多的浏览器都开始支持 HTTP/2.HTTP/2 为同一服务器的并发连接问题带来了很多好处.换句话说 ...