poj2387 Til the Cows Come Home(Dijkstra)
题目链接
http://poj.org/problem?id=2387
题意
有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1到路标n的最短路径)。
思路
最短路径问题,由于结点数目最多可以有1000个,用Floyd算法应该会超时,而且做了之后发现输入会有相同的边,使用SPFA算法会麻烦一些,所以这里使用Dijkstra算法求解。
代码
- #include <algorithm>
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- using namespace std;
- const int INF = 0x3f3f3f;
- const int N = + ;
- int map[N][N];
- int dist[N];
- int visit[N];
- int n, t;
- void dijkstra()
- {
- for (int i = ; i <= n; i++)
- dist[i] = map[][i];
- dist[] = ;
- int min_dist, now = ;
- for (int i = ; i <= n; i++)
- {
- visit[now] = ;
- min_dist = INF;
- for (int j = ; j <= n; j++)
- {
- if (!visit[j] && dist[j] < min_dist)
- {
- min_dist = dist[j];
- now = j;
- }
- }
- visit[now] = ;
- for (int j = ; j <= n; j++)
- dist[j] = min(dist[j], dist[now] + map[now][j]);
- }
- printf("%d\n", dist[n]);
- }
- int main()
- {
- //freopen("poj2387.txt", "r", stdin);
- scanf("%d%d", &t, &n);
- memset(map, INF, sizeof(map));
- int a, b, d;
- for (int i = ; i < t; i++)
- {
- scanf("%d%d%d", &a, &b, &d);
- if(map[a][b] > d) //对于相同的边,取权值最小的那条
- map[a][b] = map[b][a] = d;
- }
- dijkstra();
- return ;
- }
poj2387 Til the Cows Come Home(Dijkstra)的更多相关文章
- Til the Cows Come Home(Dijkstra)
Dijkstra (迪杰斯特拉)最短路算法,算是模板 POJ - 2387 #include<iostream> #include<algorithm> #include< ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- POJ 2387 Til the Cows Come Home(dijkstra裸题)
题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...
- POJ 2387 Til the Cows Come Home (dijkstra模板题)
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- poj2387 Til the Cows Come Home 最短路径dijkstra算法
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- POJ2387 Til the Cows Come Home 【Dijkstra】
题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...
- POJ2387 Til the Cows Come Home (最短路 dijkstra)
AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...
- 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 ...
- 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)
迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...
随机推荐
- angularJS $watch $apply $digest
看O'Reilly的书看到$watch这部分,不过没看懂,网上很多资料也含糊不清,不过还是找到了几个好的,简单记录一下. 一句话说明,$watch是用来监视变量的,好了直接上代码 <html&g ...
- Enumeration和Iterator
首先,Enumeration已经被Iterator取代了..... Enumeration是个接口,不是类,使用时需要具体的实现类. 里面只定义了两个方法: hasMoreElements()和nex ...
- Eclipse安卓插件安装
首先说明下载的ADT专门真安卓开发的Eclipse下载下来后就集成了可以直接使用了 但是使用j2EE版本的Eclipse就需要安装插件支持安卓开发了 首先下载ADT Eclipse安卓插件 下载完成后 ...
- bzoj1190 [HNOI2007]梦幻岛宝珠
传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1190 [题解] 首先,我们把所有物品都分解成$a\times 2^b$的形式,然后把物品按 ...
- PHP面向对象__set(赋值方法)
//类Ren里面的变量都是私有的,子类$r里面直接取是取不到的,正常给变量赋值的方法为$r->__set("age",20);,但是可以直接写成$r->age = 20 ...
- transform动画效果
transform动画效果 transform :移动,旋转.倾斜.缩放. transform:translate(0,300px); x代表的是水平的偏移距离,y代表垂直的. t ...
- Linux服务-搭建Nginx
任务目标:二进制安装nginx包,作为web服务修改配置文件,让配置生效,验证配置 首先使用yum 来安装 nginx 服务,基于epel-release平台的nginx需要epel的支持,所以要先安 ...
- 【译】第三篇 Integration Services:增量加载-Adding Rows
本篇文章是Integration Services系列的第三篇,详细内容请参考原文. 增量加载是什么增量加载仅加载与先前加载差异的.差异包括:->新增的行->更新的行->删除的行通过 ...
- c++ static静态
在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节 ...
- MySQL 执行SQL脚本 报ERROR 1231 (42000)的解决办法【转】
今天在source mysqldump 备份文件时,发现导入的过程中报如下的错误: ERROR 1231 (42000): Variable 'time_zone' can't be set to t ...