个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book【1】=1就错了.....

纠结中....

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

Input

* Line 1: Two integers: T and N

* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.

Output

* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

Sample Input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

Sample Output

90

Hint

INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.

Dijkstra算法
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
int t,n;
int cow[][];
int dis[];
int book[];
void dijkstra()
{
memset(book,,sizeof(book));
for(int i=;i<=n;i++) dis[i]=cow[][i];
dis[]=;
book[]=;
for(int i=;i<=n;i++)
{
int mina=inf;
int k;
for(int j=;j<=n;j++)
{
if(book[j]==&&mina>dis[j])
{
mina=dis[j];
k=j;
}
}
book[k]=;
for(int j=;j<=n;j++)
{
if(book[j]==&&dis[j]>dis[k]+cow[k][j])
dis[j]=dis[k]+cow[k][j];
}
}
}
int main()
{
while(cin>>t>>n){
int x,y,z;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i==j) cow[i][j]=;
else cow[i][j]=inf;
for(int i=;i<=t;i++){
cin>>x>>y>>z;
if(cow[x][y]>z)
cow[x][y]=cow[y][x]=z; }
dijkstra();
cout<<dis[n]<<endl;}
return ;
}

Bellman-Ford算法

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
int t,n;
int u[],v[],w[];
int dis[];
int book[];
int main()
{
while(cin>>t>>n)
{
for(int i=;i<=t;i++)
cin>>u[i]>>v[i]>>w[i];
for(int i=;i<=n;i++)
dis[i]=inf;
dis[]=;
for(int i=;i<=n;i++)
for(int j=;j<=t;j++){
if(dis[v[j]]>dis[u[j]]+w[j])
dis[v[j]]=dis[u[j]]+w[j];
if(dis[u[j]]>dis[v[j]]+w[j])
dis[u[j]]=dis[v[j]]+w[j]; }
cout<<dis[n]<<endl; }
return ;
}

Queue

 /*
spfa
Memory 256K
Time 32MS
*/
#include <iostream>
#include <queue>
using namespace std;
#define inf 1<<29
#define MAXM 4005
#define MAXV 1005 typedef struct{
int a,b,w,next;
}Edge; Edge edge[MAXM];
int n,m,headlist[MAXV]; void spfa(){
int i,d[MAXV],v,vis[MAXV];
queue <int>q;
for(i=;i<=n;i++){
d[i]=inf;
vis[i]=;
}
d[]=;
vis[]=;
q.push();
while(!q.empty()){
v=q.front();q.pop();
vis[v]=; for(i=headlist[v];i!=-;i=edge[i].next)
if(d[v]+edge[i].w<d[edge[i].b]){
d[edge[i].b]=d[v]+edge[i].w;
if(!vis[edge[i].b]){
vis[edge[i].b]=;
q.push(edge[i].b);
}
}
}
printf("%d\n",d[n]);
} int main(){
int i,a,b,c;
while(~scanf("%d%d",&m,&n)){
for(i=;i<=n;i++) headlist[i]=-;
for(i=;i<=*m;i+=){
scanf("%d%d%d",&a,&b,&c);
edge[i].a=a;
edge[i].b=b;
edge[i].w=c;
edge[i].next=headlist[a];
headlist[a]=i;
edge[i+].a=b;
edge[i+].b=a;
edge[i+].w=c;
edge[i+].next=headlist[b];
headlist[b]=i+;
}
spfa();
}
return ;
}

Til the Cows Come Home (最短路模板题)的更多相关文章

  1. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

  2. Til the Cows Come Home(最短路模板题)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Bessie is ...

  3. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

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

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

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

  7. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  8. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  9. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  10. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

随机推荐

  1. Loadrunner脚本篇——从文件中读取内容并参数化

    直接代码展示: char* testfn() { int count, total = 0; char * buffer = NULL; int filelenth = 0; long file_st ...

  2. 《Effective MySQL之SQL语句最优化》读书笔记——乱七八糟系列(给自己看)

    该书区别于诸如<MySQL技术内幕——InnoDB存储引擎>等书的一大特色是该书主要讲的是MySQL数据库中的索引技术,并分别讲了InnoDB.MyISAM.Memory三个存储引擎.其中 ...

  3. 实用篇如何使用github(本地、远程)满足基本需求

    一.结构:    |--工作区    |--版本库        |--stage——add,可以每个添加到暂存区        |--master——commit        一次性提交到版本库 ...

  4. Oracle 9i Unix Manager

    在Unix上被迫终止ORACLE进程时,必须做以下事情: (1) 杀掉所有Oracle进程.    ps -ef|grep $ORACLE_SID|grep -v grep|awk '{print $ ...

  5. 1.2CMM/CMMI是什么?

    "CMM是指“能力成熟度模型”,其英文全称为Capability Maturity Model for Software,英文缩写为SW-CMM,简称CMM.它是对于软件组织在定义.实施.度 ...

  6. JavaWeb Request和Response

    1. Request与Response 1.1. Web应用运行机制 到目前为止,我们已经掌握了Web应用程序的运行机制,现在学习的就是Web应用程序运行机制中很重要的内容 —— Request与Re ...

  7. 前段时间说了AssetBundle打包,先设置AssetLabels,再执行打包,但是这样有个弊端就是所有设置了AssetLabels的资源都会打包,这次说说不设置AssetLabels,该如何打包AssetBundle

    BuildPipeline.BuildAssetBundles() 这个函数,有多个重载,一个不用AssetBundleBuild数组,一个需要,如果设置了AssetLabels,那么这时候是不需要的 ...

  8. 在Linux系统下使用Docker以及Weave搭建Nginx反向代理

    Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...

  9. mysql慢查询设置

    不同版本的mysql命令和配置不一样,以下是2个版本 修改配置文件 log-slow-queries=/alidata/mysql-log/mysql-slow.log long_query_time ...

  10. 医院内外网之间通过网闸交互,通过端口转发加nginx代理实现内网访问外网

    首先介绍下主要需求,很简单,就是要在医院his系统内嵌公司的平台,实现内网直接访问外网 这是院方给我提供的网闸相关配置,105是医院内网的服务器,120是外网的服务器,中间通过网闸配置的几个端口实现互 ...