题目链接:http://poj.org/problem?id=2387

题目大意;

题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离。。

思路:dijkstra求单源最短路,但是要注意判重。
 
#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】的更多相关文章

  1. POJ2387 Til the Cows Come Home【Kruscal】

    题目链接>>> 题目大意: 谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大 解题思路: 最 ...

  2. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  3. POj2387——Til the Cows Come Home——————【最短路】

    A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & ...

  4. 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 ...

  5. poj2387 Til the Cows Come Home(Dijkstra)

    题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...

  6. 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 ...

  7. (Dijkstra) POJ2387 Til the Cows Come Home

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 81024   Accepted ...

  8. 怒学三算法 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 ...

  9. 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 ...

随机推荐

  1. 使用Eclipse进行Makefile项目

    最近在MCU on Eclipse网站上看到Erich Styger所写的一篇有关在Eclipse中使用Makefile创建项目的文章,文章讲解清晰明了非常不错,所以呢没人将其翻译过来供各位同仁参考. ...

  2. STM32L476应用开发之二:模拟量数据采集

    采集模拟量数据在一台一起中是必不可少的功能.在本次实验中我们要采集的模拟量值主要包括氧气传感器的输出以及压力变送器的输出. 1硬件设计 我们需要采集数据对精度有一定的要求,而STM32L476自带AD ...

  3. Confluence 6 警告的类型

    有下面的一些类型的警告. 警告和知识库(Alert and KB) 级别(Level) 默认阈值(Default threshold) 可配置(Configurable) Low free disk ...

  4. Confluence 6 配置草稿保存的时间

    这个设置仅仅应用到 Confluence 6.0 及后续版本中,如果你选择 禁用(disable )collaborative editing. 当协同编辑被启用后,我们将会保存所有的修改. 当协同编 ...

  5. python压缩文件

    #coding=utf-8 #压缩文件 import os,os.path import zipfile #压缩:传路径,文件名 def zip_compression(dirname,zipfile ...

  6. spring cloud 使用spring cloud bus自动刷新配置

    Spring Cloud Bus提供了批量刷新配置的机制,它使用轻量级的消息代理(例如RabbitMQ.Kafka等)连接分布式系统的节点,这样就可以通过Spring Cloud Bus广播配置的变化 ...

  7. 006-Python函数

    Python函数(def) 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.Python提供了许多内建函数,比如print().但你也可以自己创建函数,这被叫做用户自定义函数.函数能 ...

  8. Django中间件 及 form 实现用户登陆

    Django中间件 及 form 实现用户登陆 Form 验证 密码调用md5 加密存储 form.add_error("字段名", "错误信息") 自定义错误 ...

  9. 【C++ Primer | 10】再探迭代器

    插入迭代器 1. 测试代码: #include<iostream> #include<vector> #include<list> #include<iter ...

  10. 关于 java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String

    今天遇到了这个异常,其实是自己经验欠缺所致.我是通过mybatis查询到数据库传过来的主键,是一个32位的char类型. 代码: //查询总账表的该组织总账记录,包括该条记录的主键id.账户余额Dzz ...