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. 【设计模式】Java版设计模式的类图汇总

    Abstract Factory Intent: Provide an interface for creating families of related or dependent objects ...

  2. .NET Remoting学习笔记(二)激活方式

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科  ♂风车车.Net 激活方式概念 在 ...

  3. jQuery获取浏览器URL链接的值

    代码: 方法一: $.extend({ getUrlVars: function () { var vars = [], hash; ).split('&'); ; i < hashes ...

  4. centos 6.4 /var/log/secure 不记录日志的問題

    先确保日志服务开启:不妨重启下日志服务:由于目前RHEL 6/centos 6已经使用rsyslog替换了syslog.,所以不要在找/etc/syslog.conf了:重启命令:/etc/init. ...

  5. jdk分析工具:jps和jstack

    jps 用来查看基于HotSpot JVM里面所有进程的具体状态, 包括进程ID,进程启动的路径等等.与unix上的ps类似,用来显示本地有权限的java进程,可以查看本地运行着几个java程序,并显 ...

  6. 取消vs2013在代码中的Reference数量功能

    继续吐槽.新增的自动统计reference数量的功能: 不爽的是总以为那是一行空行,可是鼠标放上去总是落空,遂我要干掉他. 这玩意有个好处就是有两个版本的程序有小修改的时候(尤其有很多重载方法的调用变 ...

  7. mysql 优化配置参数详解

    在 my.cnf 文件中 各设置参数的含义如下: innodb_data_home_dir 这是InnoDB表的目录共用设置.如果没有在 my.cnf 进行设置,InnoDB 将使用MySQL的 da ...

  8. 曲率已驱动了头发——深度分析谷歌AlphaGo击败职业棋手

    这篇是我们自开设星际随笔以来写得最长的一篇.我们也花了不少力气.包括把那5盘棋各打了两遍的谱,包括从Nature官网上把那篇谷歌的报告花了200元下载下来研究它的算法(后来发现谷 歌网站上可以免费下载 ...

  9. ubuntu 16.04 U盘多媒体不自动弹出

    如下图: 设置 --> 详细信息 --> 可移动媒体 来自为知笔记(Wiz)

  10. 从零开始--系统深入学习IOS(使用Swift---带链接)

    这是一篇面向IOS新手的文档.同时提供一些系统知识的链接,让你系统学习IOS.它提供一些信息帮助你采用技术和编程接口来开发苹果软件产品,本人不保证会在将来更新.学习它,需要你掌握一些基本的编程知识 1 ...