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 ...
随机推荐
- MVVM MVC
在WPF的MVVM模式中,View和ViewModel之间数据和命令的关联都是通过绑定实现的,绑定后View和ViewModel并不产生直接的依赖.具体就是View中出现数据变化时会尝试修改绑定的目标 ...
- 【CF1257A】Two Rival Students【思维】
题意:给你n个人和两个尖子生a,b,你可以操作k次,每次操作交换相邻两个人的位置,求问操作k次以内使得ab两人距离最远是多少 题解:贪心尽可能的将两人往两边移动,总结一下就是min(n-1,|a-b| ...
- php获取linux服务器CPU、内存、硬盘使用率的实现代码
define("MONITORED_IP", "172.16.0.191"); //被监控的服务器IP地址 也就是本机地址 define("DB_SE ...
- appium 安装
1.npm安装: cmd——cnpm install -g appium 卸载: cmd——npm uninstall -g appium 2.下载安装包安装: Appium server: 1. A ...
- 状压 DP:[USACO06NOV] Corn Fields,[USACO13NOV] No Change
[USACO06NOV] Corn Fields (试题来源:Link ) 题目描述 Farmer John has purchased a lush new rectangular pasture ...
- docker安装部署命令
一.安装工具包 $ sudo yum install -y yum-utils #安装工具包,缺少这些依赖将无法完成 二.设置远程仓库 $sudo yum-config-manager --add-r ...
- Linux下安装xwindow图形界面
执行命令 yum -y groupinstall Desktop yum -y groupinstall "X Window System" 然后执行"startx&qu ...
- LeetCode 求众数 II
题目链接:https://leetcode-cn.com/problems/majority-element-ii/ 题目大意: 略. 分析: k个一起删, 最后check一下即可. 代码如下: #d ...
- Boosting Ensemble and GBDT Algorithm
Boosting Ensemble: 机器学习中,Ensemble model除了Bagging以外,更常用的是Boosting.与Bagging不同,Boosting中各个模型是串行的.其思想是,后 ...
- 面相对象编程 扩充之封装、访问机制、 property
封装: 封装指的是可以将一堆属性和方法,封装到对象中 ps : 对象就好比一个 “袋子/容器”, 可以存放一堆属性和方法 ps : 存不是目的,目的是为了取,可以通过“对象” d的方式获取属性或方法 ...