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. C/C++综合測试题(四)

    又刷了一套题 这些题都是百度.阿里巴巴.腾讯.网易.新浪等公司的面试原题.有一定的难度.只是确实相当有水平.能够通过做题来查漏补缺. 11.以下代码的输出是什么? class A { public: ...

  2. 新IOS编程语言 Swift 新编译器Xcode6

    https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_ ...

  3. 关于jiffies回绕以及time_after,time_before

    系统中有非常多变量用来记录一个单调递增的现实,典型的有两个,一个是TCP的序列号.还有一个就是jiffies,可是由于计算机内表示的数字都是有限无界的,所以不论什么数字都不能做到全然意义的单调递增,它 ...

  4. luogu2540 斗地主增强版

    题目大意 给你一副手牌,没有飞机带翅膀,按斗地主的规则,求将所有牌打出的最少次数. 题解 先不考虑顺子 我们已经知道花色对牌没有影响,那么如果不考虑顺子,每个牌具体是什么数字我们也用不着知道,我们关心 ...

  5. beego3---gohttp底层实现

    package main //gohttp底层实现,通过gohttp不是通过beego实现的输出 // import ( "io" "log" "ne ...

  6. URAL 1057 数位dp

    题目传送门http://acm.timus.ru/problem.aspx?space=1&num=1057 最近在学习数位dp,具体姿势可以参照这篇论文:http://wenku.baidu ...

  7. jquery uploadify在谷歌浏和火狐下无法上传的解决方案(.Net版)

    在项目紧张的进行过程中,jquery uploadify上传不兼容的问题一直没有试着去解决,只幻想着用ie的人越来越多,怎么奈何firefox4刚推出,就有4000万的下载.......仰天长叹,记生 ...

  8. 三种解密 HTTPS 流量的方法介绍

    转载自:https://imququ.com/post/how-to-decrypt-https.html作者: Jerry Qu Web 安全是一项系统工程,任何细微疏忽都可能导致整个安全壁垒土崩瓦 ...

  9. android获取手机的IMSI码

    android--获取手机的IMSI码,并判断是中国移动\中国联通\中国电信转载 TelephonyManager telManager = (TelephonyManager) getSystemS ...

  10. 【POJ 3352】 Road Construction

    [题目链接] 点击打开链接 [算法] tarjan算法求边双联通分量 [代码] #include <algorithm> #include <bitset> #include ...