POJ2387 Til the Cows Come Home 【Dijkstra】
题目链接:http://poj.org/problem?id=2387
题目大意;
题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离。。
#include <cstdio>
#include <cstring>
#define inf 0x3f3f3f3f
#define min(a,b) a<=b?a:b
int vis[], dis[]; //dis[j]表示起点到当前点的最短距离
int n, t;
int map[][]; void dij()
{
int i, j, cur, k;
memset(vis, , sizeof(vis));
for (i = ; i <= n; i++)(i == ) ? (dis[i] = ) : (dis[i] = inf);
cur = ;
for (i = ; i < n; i++) //循环n次,每次挑选没走过的到起点距离最短的点
{
vis[cur] = ;
for (j = ; j <= n; j++)
{
if (vis[j] == )
dis[j] = min(dis[j], map[cur][j] + dis[cur]); //更新每个没走过的点,到起点的最短距离
} //选择到起点距离最短的点
int g = inf; int x = ;
for (j = ; j <= n; j++)
{
if (dis[j] <= g && !vis[j])
{
g = dis[j];
x = j;
}
}
cur = x;
}
printf("%d\n", dis[n]); //输出终点到起点的最短距离
} int main()
{
int i, j, a, b, c;
while (scanf("%d%d", &t, &n) != EOF)
{
memset(map, inf, sizeof(map));
for (i = ; i <= t; i++)
{
scanf("%d%d%d", &a, &b, &c); //去除重边的情况
if (c < map[a][b])
map[a][b] = map[b][a] = c;
}
dij();
}
return ;
}
2018-04-01
POJ2387 Til the Cows Come Home 【Dijkstra】的更多相关文章
- POJ2387 Til the Cows Come Home【Kruscal】
题目链接>>> 题目大意: 谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大 解题思路: 最 ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- POj2387——Til the Cows Come Home——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- 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 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...
- 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 ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- 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 ...
随机推荐
- Confluence 6 MBeans
你可以使用下面的 Confluence MBeans 来实时查看你 Confluence 实例运行的实时信息. CacheStatistics 这个 MBean 显示了 Confluence 有关的 ...
- 断路器Feign
Feign是自带断路器,需要在配置文件中开启断路器 改造消费者项目(FeignDemo) 1.在application.yml配置文件中开启断路器 eureka: client: service-ur ...
- ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'mysql'
原因:修改数据库账号时删除了默认的localhost root, 新建了% root 但没有赋予全部权限; 解决方法: 1.关闭数据库# mysqld stop 2.在my.cnf里加入skip-g ...
- Decimal integer conversion
问题 : Decimal integer conversion 时间限制: 1 Sec 内存限制: 128 MB 题目描述 XiaoMing likes mathematics, and he is ...
- Python中的构造方法
在Java等语言中都有构造方法[进行对象的创建及初始化]这个东东,示例代码如下: public class Student { //成员变量 private String name; private ...
- ruby安装sass和compass步骤
依赖ruby,所以需要安装Ruby 如何安装Ruby呢?在windows下通过RubyInstaller来安装,安装过程中需要选择第二项 1.ruby -v 2.gem install sass (如 ...
- 对象存储服务(Object Storage Service,简称 OSS)
阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量.安全.低成本.高可靠的云存储服务.它具有与平台无关的RESTful API接口,能够提供99.99 ...
- gerrit原理
个人理解: 这个就是审核代码是否合理性的工具,一般是资深研发人工确认代码是否存在缺陷,通过发送邮件通知变化. 也可理解为这个是个git服务器,多一个代码审查的功能. 但是它是个web界面,方便管理 ...
- GAN-生成手写数字-Keras
from keras.models import Sequential from keras.layers import Dense from keras.layers import Reshape ...
- SpringBoot整合Kafka
一.准备工作 提前启动zk,kafka,并且创建一个Topic("Hello-Kafk") bin/kafka-topics.sh --create --zookeeper 192 ...