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

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

熟悉一下刚学的SPFA,注意双向边,所以边数组要开倍。
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<queue>
using namespace std;
#define V 1005
#define E 4005
#define INF 99999999 struct Edge
{
int v,w;
int next;
} edge[E]; int head[V]; int cnte=;
void addEdge(int a,int b,int c)
{
edge[cnte].v=b;
edge[cnte].w=c;
edge[cnte].next=head[a];
head[a]=cnte++;
} int t,n;
int dist[V],vis[V];
void Spfa()
{
for(int i=; i<=n; i++)
{
dist[i]=INF;
vis[i]=;
}
queue<int>q;
q.push();
dist[]=;
vis[]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(dist[v]>dist[u]+edge[i].w)
{
dist[v]=dist[u]+edge[i].w;
if(!vis[v])
{
vis[v]=;
q.push(v);
}
}
}
vis[u]=;
}
}
int main()
{
//cout<<INF<<endl;
scanf("%d%d",&t,&n);
memset(head,-,sizeof(head));
for(int i=; i<t; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
addEdge(b,a,c);
}
Spfa();
printf("%d\n",dist[n]);
return ;
}
 

#include<iostream>#include<cstdio>#include<map>#include<cstring>#include<queue>using namespace std;#define V 1005#define E 4005#define INF 99999999
struct Edge{    int v,w;    int next;} edge[E];
int head[V];
int cnte=0;void addEdge(int a,int b,int c){    edge[cnte].v=b;    edge[cnte].w=c;    edge[cnte].next=head[a];    head[a]=cnte++;}
int t,n;int dist[V],vis[V];void Spfa(){    for(int i=0; i<=n; i++)    {        dist[i]=INF;        vis[i]=0;    }    queue<int>q;    q.push(1);    dist[1]=0;    vis[1]=1;    while(!q.empty())    {        int u=q.front();        q.pop();        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v;            if(dist[v]>dist[u]+edge[i].w)            {                dist[v]=dist[u]+edge[i].w;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }        vis[u]=0;    }}int main(){    //cout<<INF<<endl;    scanf("%d%d",&t,&n);    memset(head,-1,sizeof(head));    for(int i=0; i<t; i++)    {        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        addEdge(a,b,c);        addEdge(b,a,c);    }    Spfa();    printf("%d\n",dist[n]);    return 0;}

POJ_2387_最短路的更多相关文章

  1. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  2. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  3. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  4. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  5. bzoj1266最短路+最小割

    本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...

  6. HDU2433 BFS最短路

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. 最短路(代码来源于kuangbin和百度)

    最短路 最短路有多种算法,常见的有一下几种:Dijstra.Floyd.Bellman-Ford,其中Dijstra和Bellman-Ford还有优化:Dijstra可以用优先队列(或者堆)优化,Be ...

  8. Javascript优化细节:短路表达式

    什么是短路表达式? 短路表达式:作为"&&"和"||"操作符的操作数表达式,这些表达式在进行求值时,只要最终的结果已经可以确定是真或假,求值过程 ...

  9. Python中三目计算符的正确用法及短路逻辑

    今天在看别人代码时看到这样一种写法, 感觉是个挺容易踩到的坑, 搞清楚后写出来备忘. 短路逻辑 Python中进行逻辑运算的时候, 默认采用的是一种叫做短路逻辑的运算规则. 名字是很形象的, 下面直接 ...

随机推荐

  1. struts1与struts2的差别

     Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与Struts 1的体系结 ...

  2. 字节序:Big Endian 和 Little Endian

    一.字节序 字节序,也就是字节的顺序,指的是多字节的数据在内存中的存放顺序. 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.例如:如果C/C++中的一个int型变量 a 的起始地址是& ...

  3. 从Linux内核升级的必要性说开去

    Linux内核更新超级频繁,可是有必要时刻升级吗?个人感觉没有必要,可是你要时刻关注新特性列表,然后把自己的内核升级到离最新版本号差一两个月公布的版本号而不是最新版本号.以保证稳定性,由于一两个月的时 ...

  4. tt1

    DIm #include iostream.h#include iostream.h# #include iostream.h http://www.cnblogs.com/cmt/archive/2 ...

  5. MySQLi 和 PDO 连接 MySQL

    PHP 连接 MySQL PHP 5 及以上版本建议使用以下方式连接 MySQL : MySQLi extension ("i" 意为 improved) PDO (PHP Dat ...

  6. c# 获取根节点的属性信息

    <?xml version="1.0" encoding="UTF-8"?> <!--课程封面信息 --> <GK version ...

  7. shell如何查看单个或多个文件的行数或总行数

    shell如何查看单个或多个文件的行数或总行数_百度经验 https://jingyan.baidu.com/article/cbf0e500b8470f2eab28937d.html 单个文件   ...

  8. 怎样处理Gradle中的这个文件下载慢的问题的

    如图:在build.gradle中的dependencies中加上要依赖的包后,就点击sync gradle.然后就开始了下载.在此过程中我是FQ了的(在此同时我是可以用chrome进入https:/ ...

  9. 写一个简单的Makefile

    all: osx .PHONY: osx linux run osx: kale.dylib linux : kale.so run: kale.bin CC = gcc OBJECTS = $(pa ...

  10. 使Android 自带SDK 完美支持HTML5 之 html5webview

    HTML5出来之后,webkit 大部分都支持了,但是由于历史原因,支持限度有限,我在Android 4.0使用 hao123的客户端访问youxi.cn期望可以万网手机HTML5游戏但是有些失望,进 ...