(POJ)[http://poj.org/problem?id=2387]
Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 69789 Accepted: 23386
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.

Source

USACO 2004 November

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector> using namespace std; const int maxn = 1e5;
const int maxm = 1050;
const int inf = 0x3f3f3f3f;
int n,m; struct node
{
int to,w,next;
}e[maxn];
int k;
int head[maxn];
int dis[maxn],vis[maxn];
void add(int u,int v,int w)
{
e[k].to=v;
e[k].next=head[u];
e[k].w=w;
head[u]=k++;
}
queue<int> q;
void spfa(int st)
{
for(int i=1;i<=n;i++) dis[i]=inf;
dis[st]=0;
vis[st]=1;
q.push(st);
while(!q.empty())
{
int now = q.front();
q.pop();
vis[now]=0;
for(int i=head[now]; i; i=e[i].next)
{
int ed = e[i].to;
if(dis[ed] > dis[now] + e[i].w)
{
dis[ed] = dis[now]+e[i].w;
if(!vis[ed])
{
q.push(ed);
vis[ed]=1;
}
}
}
}
}
//79MS
int main()
{
int x,y,z;
while(cin>>m>>n)
{
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
k=1;
for(int i=1;i<=m;i++)
{
cin>>x>>y>>z;
add(x,y,z);
add(y,x,z);
}
spfa(1);
printf("%d\n",dis[n]);
} }
/*
给了一个无向图,输入t和n,
t代表几个顶点,n代表问的是从第一个顶点到第n的顶点的最短距离,
各种最短路算法的模板题
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
*/

POJ 2387 链式前向星下的SPFA的更多相关文章

  1. POJ 1511 链式前向星+SPFA

    #include<iostream> #include<cstdio> #include<cstdlib> using namespace std; const i ...

  2. 【模板】链式前向星+spfa

    洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...

  3. 链式前向星版DIjistra POJ 2387

    链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...

  4. # [Poj 3107] Godfather 链式前向星+树的重心

    [Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...

  5. POJ 3169 Layout(差分约束+链式前向星+SPFA)

    描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

  6. POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)

    题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...

  7. 链式前向星写法下的DFS和BFS

    Input 5 7 1 2 2 3 3 4 1 3 4 1 1 5 4 5 output 1 5 3 4 2 #include<bits/stdc++.h> using namespace ...

  8. 链式前向星+SPFA

    今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...

  9. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

随机推荐

  1. The XOR Largest Pair

    刷刷书上的例题 在给定的N个整数A1,A2……An中选出两个进行XOR运算,得到的结果最大是多少?N<=105,0<=Ai<231 SOlution: 我们思考到对于两个数相异或,是 ...

  2. [Leetcode] word break ii拆分词语

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  3. nfs 和samba

    NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件.通过使用N ...

  4. HTML5用canvas绘制五星红旗

    在HTML5一览中,我们提到html 5被冠以很多高帽,其中最高的一顶.备受争议的就是"Flash杀手".IT评论界老喜欢用这个词了,杀手无处不在.不管是不是杀手,HTML 5引进 ...

  5. Ubuntu下安装LNMP之php7的安装并配置Nginx支持php及卸载php

    据了解,php7是比之前的版本性能快很多的.http://php.net/get/php-7.2.2.tar.gz/from/a/mirror 安装前也可提前将相关依赖库安装好,或者在安装php时若安 ...

  6. mysql修改表中某个字段的默认值

    Mysql中用SQL增加.删除字段,修改字段名.字段类型.注释,调整字段顺序总结   在网站重构中,通常会进行数据结构的修改,所以添加,删除,增加mysql表的字段是难免的,有时为了方便,还会增加修改 ...

  7. rsync 同步

    1./usr/bin/rsync -vzrtopg --progress  --include "weibo-service-server" --exclude "/*& ...

  8. Mybatis如何查询部分字段

    解决问题:数据库表里面很多字段不太需要,有时只想取到里面的部分字段的值,如果重新定义 DTO 会比较麻烦. BookMapper.xml 文件中定义如下: ` <!-- Book全部字段 --& ...

  9. openlayers3中应用proj4js

    要在openlayers3中应用proj4js,需要在html中引用proj4js,然后在引用所需要的projection的js定义,如 http://epsg.io/21781-1753.js 然后 ...

  10. DotNETCore 学习笔记 宿主

    Hosting -------------------------------------------------------------------------- Setting up a Host ...