poj2387- Til the Cows Come Home(最短路)
此为转载: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(最短路)的更多相关文章
- 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 ( 最短路 )
题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- 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(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- 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
解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...
- POJ2387 Til the Cows Come Home 【Dijkstra】
题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...
- POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)
昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...
随机推荐
- 让网页的title动起来
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Javascript 常用的工具函数,更新中...
1.时间戳转为格式化时间 /** * 时间戳转为格式化时间 * @Author chenjun * @DateTime 2017-11-10 * @param {[date]} timestamp [ ...
- [转]closed-form solution (闭合解/解析解)和数值解的理解
参考整理自:http://hi.baidu.com/cjb366/item/7290773b2d2eb9f2a9842873 closed-form solution :一般翻译为闭合解/解析解.这一 ...
- AS中一些不经常用到的快捷键
1 书签 添加/移除书签 Ctrl+shift+F11 展示书签 shift+F11 下一个书签 shift+加号 上一个书签 shift+减号 2 折叠/展开代码块 展开代码块 ctrl+加号 ...
- nodejs 接收上传的图片
1.nodejs接收上传的图片主要是使用formidable模块,服务器是使用的express搭建. 引入formidable var formidable = require('./node_mod ...
- vue2.0组件之间的传值
1.父子组件--props props:需要注意的是,虽然的是单向的数据流,但是如果传递的是数组或是对象这样的引用类型,子组件数据变化,父组件的数据通也会变化 子组件代码 <template&g ...
- 数据库-mysql数据库和表操作
一:数据库查询增加删除 1)mysql数据库查询:show databases MariaDB [mysql]> show databases; +--------------------+ | ...
- JavaScript之MV*模式
前言 在前端技术的快速发展过程中,MVC(模型-视图-控制器).MVP(模型-视图-表示器)和MVVM(模型-视图-视图模型)也得到了很多使用.然而,这三种框架模式非常相似,不易分清,易于混淆.本文重 ...
- github后端开发面试题大集合(二)
作者:小海胆链接:https://www.nowcoder.com/discuss/3615?type=0&order=0&pos=8&page=0来源:牛客网 7.非关系型数 ...
- wpf listBox的item,滚动条拖到低部,底部内容不能完全显示的问题
listBox外部包裹一层 <ScrollViewer VerticalScrollBarVisibility="Auto"> 然后修改listBox的style,取消 ...