思路:用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. 译-Web Service剖析: XML, SOAP 和WSDL 用于独立于平台的数据交换

    本文是翻译内容,原文参见: Anatomy of a Web Service: XML, SOAP and WSDL for Platform-independent Data Exchange We ...

  2. 邪恶的PLS

    今天碰到一个存储过程编译错误,提示PLS-00103错误,关于这个错误网上能搜到一大把,原因很多,我碰到的错误提示如下: Compilation errors for PROCEDURE ETL.PR ...

  3. sed的替换命令

    例1: [root@nhserver2 ~]# cat nagios.txt<TD ALIGN=LEFT valign=center CLASS='statusBGCRITICAL'>&l ...

  4. 怎样共享windows和linux之间的文件

    注:本文参考自:https://www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/,相当于是原文的翻译. 一. ...

  5. C# 关键字--virtual(转)

    C# 关键字--virtual 一.   virtual 关键字用于修饰方法.属性.索引器或事件声明,并使它们可以在派生类中被重写.虚拟成员的实现可由派生类中的重写成员更改,而非虚拟成员是无法由派生类 ...

  6. javaScript补充

    一.字符串常用的方法 obj.length 长度 obj.trim() 移除前后空白 obj.trimLeft() 移除前空白 obj.trimRight() 移除后空白 obj.charAt(n) ...

  7. JAVA设计模式---迭代器模式

    1.定义: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 2.实例:1)需求: 菜单(煎饼屋菜单.餐厅菜单和咖啡菜单)采用不同的集合存取(ArrayList,String[] ...

  8. [Sdoi2017]数字表格 [莫比乌斯反演]

    [Sdoi2017]数字表格 题意:求 \[ \prod_{i=1}^n \prod_{j=1}^m f[(i,j)] \] 考场60分 其实多推一步就推倒了... 因为是乘,我们可以放到幂上 \[ ...

  9. Go终端读写

    终端读写 操作终端相关文件句柄常量 os.Stdin:标准输入 os.Stdout:标准输出 os.Stderr:标准错误输出 终端读写实例: package main import ( " ...

  10. 从一个实例学习----FLASK-WTF

    本案例通过实现一个注册页面的编写,来带你了解FLASK-WTF的运用. 主要功能为表单基础的功能--手机号码必须为11位数,且通过数据库查找不能有已经注册的了,密码要求输入两遍且必须一样,且所有内容不 ...