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. C# 去重处理字符大小写

    本文展示了如何对集合去重并且处理大小写

  2. java基础练习2

    1, 写一段代码, 可以取出任意qq邮箱地址中的qq号码 public class Test { public static void main(String[] args) { String str ...

  3. anacoda 安装默认源中没有的包

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 1 安装失败 conda install pygame 2 搜索 anaconda se ...

  4. poj3074 DLX精确覆盖

    题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...

  5. SQLServer 创建自己的数据库

    1)进入数据库服务器,创建自己的数据库 use master go create database Dt_Devtest on primary(name=[Dt_new_data],filename= ...

  6. scrapy框架 + selenium 爬取豆瓣电影top250......

    废话不说,直接上代码..... 目录结构 items.py import scrapy class DoubanCrawlerItem(scrapy.Item): # 电影名称 movieName = ...

  7. JustOj 2040: 王胖子买零食 (贪心)

    题目描述 大豪哥有个好朋友叫王胖子,众所周知王胖子特别爱吃零食,比如各种不一样的糖果,辣条呀,可是王胖子每个月用在买零食上的钱不是固定的,但是因为王胖子特别爱吃零食,他希望把自己能花在买吃的钱全部用掉 ...

  8. [转载]谈谈document.ready和window.onload的区别

    在Jquery里面,我们可以看到两种写法:$(function(){}) 和$(document).ready(function(){}) 这两个方法的效果都是一样的,都是在dom文档树加载完之后执行 ...

  9. P1216 数字金字塔

    P1216  数字金字塔 我们可以用 f [ i ] [ j ] 表示从(1,1)出发,到达(i,j)的最大权值和. (i , j)可以由(i - 1 , j)或者(i - 1 , j - 1)转化来 ...

  10. ID3和C4.5分类决策树算法 - 数据挖掘算法(7)

    (2017-05-18 银河统计) 决策树(Decision Tree)是在已知各种情况发生概率的基础上,通过构成决策树来判断其可行性的决策分析方法,是直观运用概率分析的一种图解法.由于这种决策分支画 ...