POJ 2387 Til the Cows Come Home (dijkstra模板题)
Description
Farmer John's field has N (2 <= N <= 1000) landmarks in
it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree
grove in which Bessie stands all day is landmark N. Cows travel in the
field using T (1 <= T <= 2000) bidirectional cow-trails of various
lengths between the landmarks. Bessie is not confident of her
navigation ability, so she always stays on a trail from its start to its
end once she starts it.
Given the trails between the landmarks, determine the minimum
distance Bessie must walk to get back to the barn. It is guaranteed
that some such route exists.
Input
* Lines 2..T+1: Each line describes a trail as three
space-separated integers. The first two integers are the landmarks
between which the trail travels. The third integer is the length of the
trail, range 1..100.
Output
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90 理解dijkstra,对于所有未标号的点中选取一个d[x]最小的点,记为点x。将x标记
对于所有x出发的边(x,y),更新d[y]=min(d[y],d[x]+mapp[x][y])
其实本质上是一个用优先队列优化的bfs(优先访问最短边的终点),在bfs中加上松弛操作就行了,同时保证访问过的点集之间的最短路是知道的
每次访问一个没有访问过的新点时,我们是要把它加入访问过的点集中去的对吧!加入的前提就是它与和它相连的访问过的所有点
做一次松弛操作,更新下状态。那么与它相邻但是没有访问过的点,我们加入优先队列。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn =;
vector <int>G[maxn];//存每个点连的边的编号
bool done[maxn];//标记某点是否访问过
int p[maxn];//最短路中某个点的上一条路是几号
int d[maxn];//原点到某点的距离
int t,n;
struct Edge{ //边
int from,to,dis;
Edge(int u,int v,int d):from(u),to(v),dis(d){}
};
vector <Edge>edges;
struct HeapNode {//堆点,用于优化
int d,u;
bool operator <(const HeapNode& x) const {//优先考虑距离最小
return d>x.d;
}
};
void addEdge(int u,int v,int dis){//加边函数
edges.push_back(Edge(u,v,dis));
int m=edges.size();
G[u].push_back(m-);
}
void dijkstra (int s) {
priority_queue<HeapNode> q;
memset(d,inf,sizeof d);
d[s]=;
memset(done,false,sizeof done);
q.push(HeapNode{,s});
while (!q.empty()){
HeapNode x=q.top();
q.pop();
int u=x.u;
if (done[u])
continue;
done[u]=true;
for (int i=;i<G[u].size();++i){
Edge& e=edges[G[u][i]];
if (d[e.to]>d[u]+e.dis){//当e.to这个点是done过的点时,功能是得到
d[e.to]=d[u]+e.dis;
p[e.to]=G[u][i];
q.push((HeapNode){d[e.to],e.to});
}
}
}
}
void init(){
for (int i=;i<n;++i)
G[i].clear();
edges.clear();
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&t,&n)){
init();
for (int i=;i<t;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
addEdge(x,y,z);
addEdge(y,x,z);
}
dijkstra();
printf("%d\n",d[n]);
}
return ;
}
POJ 2387 Til the Cows Come Home (dijkstra模板题)的更多相关文章
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- POJ 2387 Til the Cows Come Home (Dijkstra)
传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
- 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 ...
- 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
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
随机推荐
- spring-boot整合mongodb多数据源的案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- Docker容器数据卷-Volume详解
Docker中的数据可以存储在类似于虚拟机磁盘的介质中,在Docker中称为数据卷(Data Volume).数据卷可以用来存储Docker应用的数据,也可以用来在Docker容器间进行数据共享.数据 ...
- MDX members使用
Members (Set) 函数返回该指定层次结构内所有成员(不包括计算成员)的集: Members (String) 函数返回已指定名称的单个成员. 通常,将 Members (String) 函数 ...
- zabbix真的很简单 (安装篇)
系统环境: Centos 6.4 一直觉得 zabbix 很简单,但是还是有好多人看了好多文档都搞不明白怎么用,我从2013年使用到现在也小有心得,如果时间允许,很高兴与大家一起分享我在使用过程中的一 ...
- 关于Ext4 extraParams 不能传递动态参数的问题解决办法
可以监听请求发送之前的事件:beforeload ,然后再添加请求的参数 me.store = Ext.create('Ext.data.JsonStore', { remoteSort: true, ...
- Bentley二次开发中的,沿曲线构造拉伸实体问题
引用文件:Bentley.Interop.MicroStationDGN 本人开发过程中遇到问题: 创建多个线段及弧线,通过自动创建复杂链获得,沿曲线构造拉伸实体的Path参数,拉伸曲线路径首尾特别近 ...
- golang的数据类型之字符串类型
基本案例: [root@node3 shangxuetang]# cat string.go package main import "fmt" func main() { //s ...
- viewport的深入调研
1.viewport概念:viewport就是设备的屏幕上能用来显示我们的网页的那一块区域. viewport的默认值980px或1024px等,以下是浏览器的默认viewport宽度 2.css中的 ...
- 【转载】Elasticsearch--java操作之QueryBuilders构建搜索Query
原文地址:https://www.cnblogs.com/pypua/articles/9459941.html package com.elasticsearch; import org.elast ...
- sqlite3 C语言 API 函数
int sqlite3_open(char *path, sqlite3 **db): 功能:打开sqlite数据库 参数: path: 数据库文件路径 db: 指向sqlite句柄的指针 返回值 ...