POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)

时间:2022-08-29 17:59:57

题目连接:

http://poj.org/problem?id=2387

Description

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.

题意描述:
最短路水题。
解题思路:
处理数据,使用迪杰斯特拉算法。
AC代码:
 #include<stdio.h>
#include<string.h>
int e[][],dis[],bk[];
int main()
{
int i,j,min,t,t1,t2,t3,n,u,v;
int inf=;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if(i==j)
e[i][j]=;
else
e[i][j]=inf;
}
}
for(i=;i<=t;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
if(e[t1][t2]>t3)
{
e[t1][t2]=t3;
e[t2][t1]=t3;
}
}
for(i=;i<=n;i++)
dis[i]=e[][i];
memset(bk,,sizeof(bk));
bk[]=;
for(i=;i<=n-;i++)
{
min=inf;
for(j=;j<=n;j++)
{
if(bk[j]==&&dis[j]<min)
{
min=dis[j];
u=j;
}
}
bk[u]=;
for(v=;v<=n;v++)
{
if(e[u][v]<inf && dis[v]>dis[u]+e[u][v])
dis[v]=dis[u]+e[u][v];
}
}
printf("%d\n",dis[n]);
}
return ;
}
 

POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)的更多相关文章

  1. 怒学三算法 POJ 2387 Til the Cows Come Home &lpar;Bellman&lowbar;Ford &vert;&vert; Dijkstra &vert;&vert; SPFA&rpar;

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

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

  3. &lpar;简单&rpar; 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 ...

  4. POJ 2387 Til the Cows Come Home(dijkstra裸题)

    题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...

  5. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

  6. POJ&period;2387 Til the Cows Come Home &lpar;SPFA&rpar;

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  7. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

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

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

  9. POJ 2387 Til the Cows Come Home(最短路 Dijkstra&sol;spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  10. POJ 2387 Til the Cows Come Home (最短路 dijkstra)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...

随机推荐

  1. pdf&period;js在IIS中配置使用笔记

    最近在手机App开发Android版本时候遇到需要显示PDF文件的需求,记得之前直接使用系统浏览器或者WebView就可以显示,但是现在不可以了,只能另寻其他办法. 最终找到PDF.JS来进行实现,但 ...

  2. cocos2d-x之action初试

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...

  3. MySQL在ROW模式下通过binlog提取SQL语句

    Linux基于row模式的binlog,生成DML(insert/update/delete)的rollback语句通过mysqlbinlog -v 解析binlog生成可读的sql文件提取需要处理的 ...

  4. 浅析libev的ev&lowbar;signal过程

    ev_signal是libev提供的对信号处理的一个模块,基本上是对sigaction函数的一个封装,并将本身是异步的信号转化为同步.ev_signal的使用十分简单: #include <ev ...

  5. String和StringBuffer 常用方法总结

     String和StringBuffer 常用方法总结 一.不可变长度String 1.字符串---->char数组 char[] chars=str.toCharArray(); 2.字符串中 ...

  6. angularjs 选项卡tab切换(移动端用户订单状态)

    <!--头部导航tabs切换--> <div class="tabs-striped tabs-top tabs-background-positive tabs-colo ...

  7. C&sol;C&plus;&plus; 内存对齐原则及作用

    struct/class/union内存对齐原则有四个: 1).数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员存储 ...

  8. 冲刺Two之站立会议10

    今天是最后一次站立会议,我们为自己软件最终版的发布进行了讨论,针对项目开发过程中出现的问题进行了总结.并讨论了之后软件如何发布和推广.

  9. Modbus库开发笔记之十一:关于Modbus协议栈开发的说明(转)

    源: Modbus库开发笔记之十一:关于Modbus协议栈开发的说明

  10. window环境下使用sbt编译spark源码

    前些天用maven编译打包spark,搞得焦头烂额的,各种错误,层出不穷,想想也是醉了,于是乎,换种方式,使用sbt编译,看看人品如何! 首先,从官网spark官网下载spark源码包,解压出来.我这 ...