Til the Cows Come Home

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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.
 #include <iostream>
#include <string>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <deque>
#include <vector>
#define LL long long
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define dg(i) cout << "*" << i << endl;
using namespace std; /**
*spfa算法:邻接矩阵+SLF策略
*顶点标号:1..n
*源点:1,目的点:n
*/ const int sz = ; //size--最大顶点数
const int inf = 0x3f3f3f3f; //最大值
int w[sz][sz]; //w[i][j]--边(i,j)的权值,若(i,j)不存在,w[i][j]为inf
int dis[sz]; //dis[i]--源点到顶点i的最短距离,初始化为inf
bool in[sz]; //in[i]--标记顶点i是否在队列中,在为true
int n, m; //n--顶点数,m--边数 int main()
{
while(scanf("%d %d", &m, &n) != EOF)
{
int u, v, d;
memset(dis, inf, sizeof(dis)); //0x3f3f3f3f是可以用memset初始化的
dis[] = ; //
memset(w, inf, sizeof(w));
while(m--)
{
scanf("%d %d %d", &u, &v, &d);
w[u][v] = w[v][u] = min(d, w[u][v]); //为避免平行边,取min
}
memset(in, false, sizeof(in));
deque<int> que;
que.push_front(); //源点进队
in[] = true;
while(!que.empty())
{
int cur = que.front();
que.pop_front();
in[cur] = false; //撤销进队标志
for(int i = ; i <= n; i++) //对与cur相邻的点都进行松弛计算
{
if(dis[cur] + w[cur][i] < dis[i])
{
dis[i] = dis[cur] + w[cur][i]; //更新最短距离
if(!in[i]) //对于最短距离被更新的点,若不在队列则进队
{
//此处执行SLF策略,小的尽量放前面。不采取这个策略而直接让i进队也可以
if(!que.empty() && dis[i] < dis[que.front()])
que.push_front(i);
else que.push_back(i);
in[i] = true;
}
}
}
}
printf("%d\n", dis[n]);
}
return ;
}

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

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

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

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

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

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

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

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

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

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

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

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

  8. POJ 2387 Til the Cows Come Home 【最短路SPFA】

    Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...

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

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

  10. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

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

随机推荐

  1. 【HTML5】Canvas 内部元素添加事件处理

    前言 canvas 没有提供为其内部元素添加事件监听的方法,因此如果要使 canvas 内的元素能够响应事件,需要自己动手实现.实现方法也很简单,首先获得鼠标在 canvas 上的坐标,计算当前坐标在 ...

  2. 深入理解JavaScript 事件

    本文总结自<JavaScript高级程序设计>以及自己平时的经验,针对较新浏览器以及 DOM3 级事件标准(2016年8月),对少部分内容作了更正,增加了各种例子及解析. 如无特殊说明,本 ...

  3. paip.提升分词---准确度--常用量词表

    paip.提升分词---准确度--常用量词表 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.ne ...

  4. Atitit. 高级软件工程师and 普通的区别 高级编程的门槛总结

    Atitit.  高级软件工程师and 普通的区别 高级编程的门槛总结 1. 完备的知识体系 2 2. 编程理论/原理的掌握 2 1.1. 掌握常用的概念(ORM,IOC,AOP,event driv ...

  5. iOS开发---分类和扩展(Categories和Extensions)

      1.分类能够做到的事情主要是:即使在你不知道一个类的源码情况下,向这个类添加扩展的方法.   此外,分类能够保证你的实现类和其他的文件区分开.   1 #import “UIViewControl ...

  6. java获取静态页面内容

    package collection_map; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.F ...

  7. ps、grep和kill联合使用杀掉进程

    例如要杀掉hello这个进程,使用下面这个命令就能直接实现.   ps -ef |grep hello |awk '{print $2}'|xargs kill -9 这里是输出ps -ef |gre ...

  8. [原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推)

    [原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推) 内部推荐职位 高级JAVA技术经理: 岗位职责: 负责项目管理(技术方向),按照产品开发流 ,带领研发团队,制定 ...

  9. android: SQLite升级数据库

    如果你足够细心,一定会发现 MyDatabaseHelper 中还有一个空方法呢!没错,onUpgrade() 方法是用于对数据库进行升级的,它在整个数据库的管理工作当中起着非常重要的作用,可 千万不 ...

  10. python实现自动发送微博,当自己写博客时同步上去。

    一.需求: 自己在github上搭建一个基于Jekyll的博客(http://beginman.cn/),每次写完博客后就要push上去,博客写的再好,基本上没人访问,为了增加访问量,就想利用起来微博 ...