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 ...
随机推荐
- springboot下的多数据源切换
今天在考虑如果分公司也用系统了,怎么办,是单独的数据库,还是一起使用?所以就想到了切换数据源来实现,但是发现,只是读写分离,还要再改一下,根据用户地域来切换数据源,今天先照着例子做一下. 看了好多文章 ...
- paper 154:姿态估计(Hand Pose Estimation)相关总结
Awesome Works !!!! Table of Contents Conference Papers 2017 ICCV 2017 CVPR 2017 Others 2016 ECCV 20 ...
- Django中的get()和filter()区别
前言 在django中,我们查询经常用的两个API中,会经常用到get()和filter()两个方法,两者的区别是什么呢? object.get()我们得到的是一个对象,如果在数据库中查不到这个对象或 ...
- Ubuntu 14.04修复grub
1.进livecd 2.sudo fdisk -l 找到安装分区 3.sudo mount /dev/sda? /mnt 挂载安装分区 4.sudo mount --bind /dev /mnt/de ...
- ArrayList和Map的一些知识
2014年10月17日16:39:01 1.如何获取某条数据在ArrayList中的索引值? 多个对象存储的ArrayList中,如果只知道对象其中的一个属性,如何获取该对象在ArrayList里的索 ...
- 大数据给IT企业带来攫金市场新机遇
大数据给IT企业带来攫金市场新机遇 大数据,一个时髦的名词,也是当下热门的业务领域.大数据诱人的利益诉求点之一,即通过大数据能更好地提高效率,更好地有的放矢.一方面,大数据让公司内部更有效地运作:另一 ...
- 获取header中content-type的值
后台传过来的值需要根据content-Type的值来判定成功与否 获取header中content-Tyep的值 用res.header['Content-Type']
- Prim算法生成迷宫
初始化地图 function initMaze(r,c){ let row = new Array(2 * r + 1) for(let i = 0; i < row.length; i++){ ...
- web.xml中servlet mapping标签
写了好多小项目后也没弄明白<url-pattern>的真正意义,写跳转的时候也是跳的三心二意的,今天查了一下web.xml的详细配置,看了看servlet-mapping的讲解,豁然开朗, ...
- C++中初始化列表的使用
1,初始化列表是在 C++ 中才引入的: 2,以“类中是否可以定义 const 成员?”这个问题来引入初始化列表: 1,const 这个关键字可以定义真正意义上的常量,也可以在某些情况下定义只读变量: ...