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 配置一个数据源连接
这个指南指导你如何配置使用 JNDI 数据源来连接到你的数据库.使用这个类型的连接,Confluence 将会询问应用服务器(Tomcat)中你配置的连接信息. 如果你希望使用 JDBC 的数据库连接 ...
- IPv4和IPv6简单对比介绍(转载)
原链接:https://baijiahao.baidu.com/s?id=1570208896149974&wfr=spider&for=pc 在配置计算机网络,特别是内网的时候,有时 ...
- SpringBoot图片上传(二)
需求简介:做新增的时候,需要上传图片.(⊙o⊙)…这需求描述也太简单了吧,限制文件大小60*60 512kb ,第一次做,记录一下嗷,废话就不啰嗦了 上代码 代码: //html代码<div c ...
- QuerySet中添加Extra进行SQL查询
select参数接受一个映射到SQL的字符串标识字典,让你可以根据SQL select子句里的选择进行对模型实例添加属性,或者查询. from blog.models import BlogPost& ...
- bzoj2200拓扑排序+最短路+联通块
自己写的不知道哪里wa了,明明和网上的代码差不多.,. /* 给定一张图,有的边是无向边,有的是有向边,有向边不会出现在环中,且有可能是负权值 现在给定起点s,求出s到其余所有点的最短路长度 任何存在 ...
- .tar.xz文件的解压方法
废话不多说: 直接看 方法一: tar -xvJf ***.tar.gz 方法二: 先减压成 .tar 格式的文件, 再解压 .tar #xz是一个工具, 系统中没有安装,需要下载 xz -d *** ...
- 论文阅读笔记二十:LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation(CVPR2017)
源文网址:https://arxiv.org/abs/1707.03718 tensorflow代码:https://github.com/luofan18/linknet-tensorflow 基于 ...
- linux上安装redis并使用
1.下载:curl -O http://download.redis.io/releases/redis-4.0.6.tar.gz 2.在/usr/local/redis上解压:tar -zxvf r ...
- Android 中的缓存
AsimpleCache 1.它可以缓存什么东西? 普通的字符串. json. 序列化的java对象 字节数字. 2.主要特色 1:轻,轻到只有一个JAVA文件. 2:可配置,可以配置缓存路径,缓存 ...
- sqlserver数据库不能重命名报错5030
在学习asp.net的时候使用mssql'经常会出现这种错误,数据库不能重名名5030的错误,其实很简单原因就是有应用程序正在占用这个连接,使用这样一行命令就可以查询出正在占用的连接 use mast ...