此为转载:http://blog.csdn.net/wangjian8006

题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离

解题思路:

模版题,这题要注意的是有重边,dijkstra的算法需要考虑下,bellman-ford和spfa可以忽略这个问题

PS:虽然转载,但是只有一点是值得我们去看的,那就是在有重边的Dijkstra的处理问题,虽然很简单,但不是每个人都能想到的;

#include <iostream>
using namespace std;
#define inf 1<<29
#define MAXV 1005 int map[MAXV][MAXV];
int n,m; void dijkstra(){
int i,j,min,v;
int d[MAXV];
bool vis[MAXV]; for(i=;i<=n;i++){
vis[i]=;
d[i]=map[][i];
} for(i=;i<=n;i++){
min=inf;
for(j=;j<=n;j++)
if(!vis[j] && d[j]<min){
v=j;
min=d[j];
}
vis[v]=; for(j=;j<=n;j++)
if(!vis[j] && d[j]>map[v][j]+d[v])
d[j]=map[v][j]+d[v];
}
printf("%d\n",d[n]);
} int main(){
int i,j,a,b,c;
while(~scanf("%d%d",&m,&n)){
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(i==j)
map[i][i]=;
else map[i][j]=map[j][i]=inf; for(i=;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c) map[a][b]=map[b][a]=c; //这个if语句就是处理重边,保证了这条边绝对是最小的。
}
dijkstra();
}
return ;
}

poj2387- Til the Cows Come Home(最短路)的更多相关文章

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

  2. POJ-2387 Til the Cows Come Home ( 最短路 )

    题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  3. Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)

    Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...

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

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

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

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

  6. (Dijkstra) POJ2387 Til the Cows Come Home

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

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

  8. poj2387 Til the Cows Come Home

    解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...

  9. POJ2387 Til the Cows Come Home 【Dijkstra】

    题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...

  10. POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)

    昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...

随机推荐

  1. 关于mysql 5.7版本“报[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause”错误的bug

    不同于oracle和sqlserver,mysql并不支持在更新某个表的数据时又查询了它,而查询的数据又做了更新的条件,因此我们需要使用如下的语句绕过: , notice_code ) a) ; 本地 ...

  2. React - 环境准备

    1. 下载 node.js,在 https://nodejs.org/en/download/ 选择合适的版本. 2. 检查 node.js 是否安装成功. hueyMB:~ zhisenzheng$ ...

  3. python 12306 车次数据获取

    ssl._create_default_https_context = ssl._create_default_https_context train_data = '2018-10-20' head ...

  4. UC手机浏览器(U3内核)相关文档整理

    Note:绝大多数API在IOS版下不支持,使用前请自行测试. UC官方的开发者中心:http://www.uc.cn/business/developer.shtml U3内核定制<meta& ...

  5. GDB基本用法

    基本命令 进入GDB:#gdb test test是要调试的程序,由gcc test.c -g -o test生成.进入后提示符变为(gdb) . 查看源码:(gdb) l 源码会进行行号提示. 如果 ...

  6. xampp + windows 配置 memcache流程

    1.运行xampp服务,打开phpinfo查看信息,如下图: 从上到下,依次为 PHP版本:5.6: architecture:x86: TS:线程安全: vc11 2.牢记以上几个横线内容之后去下载 ...

  7. python 面试题3

    注:本面试题来源于网络. 1.python下多线程的限制以及多进程中传递参数的方式 python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只能有一 ...

  8. 2016.6.18——Implement strStr()

    Implement strStr() 本题收获: 1.考虑多种边界条件. 2.haystack.size() size_type 是无符号的,即为正数 在32位系统上定义为 unsigned int ...

  9. Gh0st配置加密与解密算法(异或、Base64)

    1.前言 分析木马程序常常遇到很多配置信息被加密的情况,虽然现在都不直接分析而是通过Wireshark之类的直接读记录. 2017年Gh0st样本大量新增,通过对木马源码的分析还发现有利用Gh0st加 ...

  10. Django 1.10中文文档-模型参考

    模型字段 本文档包含了Django提供的全部模型 Field 包括 字段选项 和 字段类型 的API参考. 参见 如果内建的字段不能满足你的需求, 你可以蚕食 django-localflavor ( ...