Til the Cows Come Home

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.

题解

最短路 之前是用的dijkstra,换用spfa写写

代码

#include<iostream>
#include<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h> //INT_MAX
#include<bitset> // bitset<?> n
using namespace std; typedef long long LL;
typedef long long ll;
typedef pair<int,int> P;
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+;
const int MAXN = ;
const int maxn = +;
int a[maxn][maxn],dis[maxn];
int vis[maxn]; void SPFA(int n)
{
queue<int> q;
for(int i = ;i <= n; i++){
dis[i] = INF;
vis[i] = ;
}
dis[] = ;
vis[] = ;
q.push() ;
while(!q.empty())
{
int k = q.front();
q.pop();
vis[k] = ;
for(int j = ;j <= n; j++)
if(dis[j] > dis[k] + a[k][j])
{
dis[j] = dis[k] + a[k][j];
if(!vis[j])
{
q.push(j);
vis[j] = ;
}
}
}
}
int main()
{
int m,n,w;
int x,y;
while(read2(m,n) != EOF)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
if(i == j) a[i][j] = ;
else a[i][j] = a[j][i] = inf;
while(m--)
{
read3(x,y,w);
if(w < a[x][y])
a[x][y] = a[y][x] = w;
}
SPFA(n);
print(dis[n]);
}
return ;
}

POJ 2387 Til the Cows Come Home 【最短路SPFA】的更多相关文章

  1. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

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

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

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

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

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

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

  5. POJ.2387 Til the Cows Come Home (SPFA)

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

  6. POJ 2387 Til the Cows Come Home

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

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

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

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

  9. POJ 2387 Til the Cows Come Home Dijkstra求最短路径

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

随机推荐

  1. a标签响应onclick事件,并且不执行href动作

    1.javascript:void(0)相当于一个死链接,href不执行 <a href="javascript:void(0)" onclick="doSomet ...

  2. 《大话设计模式》c++实现 模版方法模式

    模板方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 角色: (1)AbstractClass:是抽象类,其实也 ...

  3. PHP运行脚本

    PHP运行脚本 php.exe -f "php文件" php.exe -r "php代码" 例如:在cmd中 C:\Users\Administrator.SK ...

  4. Python全栈-day5-数据类型

    一.元组 1.元组基础 1)定义:不可变的‘列表’,定义方式(元素1,元素2.......) 2)用途:存多个值,但是只能读不能写 注意:元组的不可变指的是元组内元素id的不可变 t = (11,2, ...

  5. Linux基础命令---文本过滤coi

    col 过滤掉影响阅读的控制字符,使用重定向符把说明手册的内容输出到文本文件时,控制字符就成乱码.col指令可以过滤掉控制字符,使文本可读.col从标砖输入读取内容,输出到标准输出.col在读取字符时 ...

  6. 以太坊erc20转账失败的情况和原因

    以太坊erc20转账失败的情况和原因 eth转账失败有多种情况,除了手续费过低以外(Out of gas),众筹额度满了(Bad instruction)也会失败链上转账有可能失败,转账失败转账的币退 ...

  7. Spring SpringBoot和SpringCloud的关系

    Spring SpringBoot和SpringCloud的关系 Spring Cloud 是完全基于 Spring Boot 而开发,Spring Cloud 利用 Spring Boot 特性整合 ...

  8. react系列笔记1 用npx npm命令创建react app

    react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...

  9. Promise的简单用法

    众所周知的,Javascript是一种单线程的语言,所有的代码必须按照所谓的“自上而下”的顺序来执行.本特性带来的问题就是,一些将来的.未知的操作,必须异步实现.本文将讨论一个比较常见的异步解决方案— ...

  10. JustOj 1414: 潘神的排序

    题目描述 老潘,袁少,小艾都是江理的大个子,他们想按身高排队,现在给你他们的身高,请你算出队伍中站在第二的有多高. 输入 输入三个整数,分别表示三个人的身高.(单位:纳米) 输出 输出身高排第二的身高 ...