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算法堆优化中另外 ...
随机推荐
- dp px 互转工具类
public class DensityUtils { public static int dpToPx(Context context,int dp){ float density = contex ...
- dubbo Filter
官方说明: 调用拦截扩展 扩展说明 服务提供方和服务消费方调用过程拦截,Dubbo 本身的大多功能均基于此扩展点实现,每次远程方法执行,该拦截都会被执行,请注意对性能的影响. 约定: 用户自定义 fi ...
- Anaconda+django写出第一个web app(二)
今天开始建立App中的第一个Model,命名为Tutorial. Model的定义在main文件夹下的models.py中通过类进行,我们希望Tutorial这个model包含三个属性:标题.内容和发 ...
- js鼠标自定移入输入框文本框光标自动定位到文本框
1.干货直接上 选中输入框设置如下: document.getElementById("Text1").focus();
- 44、File类简介
使用File类创建文件夹 File类在java.io包下,看名字应该可以猜到,这个类是跟文件夹操作有关,下面使用File类中的方法在硬盘中创建文件夹. package com.sutaoyu.file ...
- spfa+差分约束系统(C - House Man HDU - 3440 )+对差分约束系统的初步理解
题目链接:https://cn.vjudge.net/contest/276233#problem/C 题目大意:有n层楼,给你每个楼的高度,和这个人单次的最大跳跃距离m,两个楼之间的距离最小是1,但 ...
- Dream------scala--类的属性和对象私有字段实战详解
Scala类的属性和对象私有字段实战详解 一.类的属性 scala类的属性跟java有比较大的不同,需要注意的是对象的私有(private)字段 1.私有字段:字段必须初始化(当然即使不是私有字段也要 ...
- CSS ... 文本溢出用省略号代替
{ overflow:hidden; text-overflow:ellipsis; white-space:nowrap } text-overflow 属性规定当文本溢出包含元素时发生的事情. c ...
- aarch64_a2
asterisk-sounds-core-en_GB-1.5.0-2.fc26.noarch.rpm 2017-02-14 08:24 26K fedora Mirroring Project ast ...
- plupload 上传组件的使用
在这之前在感谢园子好多大牛的文章,在这里就不列出来了. 进入正题. svn检索https://github.com/moxiecode/plupload 获取到代码,这篇文章使用的是v2.1.8 主要 ...