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

  1. 5 5
  2. 1 2 20
  3. 2 3 30
  4. 3 4 20
  5. 4 5 20
  6. 1 5 100

Sample Output

  1. 90
  2.  
  3. 熟悉一下刚学的SPFA,注意双向边,所以边数组要开倍。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #include<cstring>
  5. #include<queue>
  6. using namespace std;
  7. #define V 1005
  8. #define E 4005
  9. #define INF 99999999
  10.  
  11. struct Edge
  12. {
  13. int v,w;
  14. int next;
  15. } edge[E];
  16.  
  17. int head[V];
  18.  
  19. int cnte=;
  20. void addEdge(int a,int b,int c)
  21. {
  22. edge[cnte].v=b;
  23. edge[cnte].w=c;
  24. edge[cnte].next=head[a];
  25. head[a]=cnte++;
  26. }
  27.  
  28. int t,n;
  29. int dist[V],vis[V];
  30. void Spfa()
  31. {
  32. for(int i=; i<=n; i++)
  33. {
  34. dist[i]=INF;
  35. vis[i]=;
  36. }
  37. queue<int>q;
  38. q.push();
  39. dist[]=;
  40. vis[]=;
  41. while(!q.empty())
  42. {
  43. int u=q.front();
  44. q.pop();
  45. for(int i=head[u]; i!=-; i=edge[i].next)
  46. {
  47. int v=edge[i].v;
  48. if(dist[v]>dist[u]+edge[i].w)
  49. {
  50. dist[v]=dist[u]+edge[i].w;
  51. if(!vis[v])
  52. {
  53. vis[v]=;
  54. q.push(v);
  55. }
  56. }
  57. }
  58. vis[u]=;
  59. }
  60. }
  61. int main()
  62. {
  63. //cout<<INF<<endl;
  64. scanf("%d%d",&t,&n);
  65. memset(head,-,sizeof(head));
  66. for(int i=; i<t; i++)
  67. {
  68. int a,b,c;
  69. scanf("%d%d%d",&a,&b,&c);
  70. addEdge(a,b,c);
  71. addEdge(b,a,c);
  72. }
  73. Spfa();
  74. printf("%d\n",dist[n]);
  75. return ;
  76. }
  1.  

#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. react 项目实战(四)组件化表单/表单控件 高阶组件

    高阶组件:formProvider 高阶组件就是返回组件的组件(函数) 为什么要通过一个组件去返回另一个组件? 使用高阶组件可以在不修改原组件代码的情况下,修改原组件的行为或增强功能. 我们现在已经有 ...

  2. 【POJ 3122】 Pie (二分+贪心)

    id=3122">[POJ 3122] Pie 分f个派给n+1(n个朋友和自己)个人 要求每一个人分相同面积 但不能分到超过一个派 即最多把一整个派给某个人 问能平均分的最大面积 二 ...

  3. Linux 内核开发 - 内存管理

    1.1什么是内存管理 内存管理是对计算机内存进行分配和使用的技术.内存管理主要存在于多任务的操作系统中,因为内存资源极其有限.须要在不同的任务之间共享内存,内存管理的存在就是要高效.高速的非配内存,并 ...

  4. web 开发之js---js 多线程编程

    AJAX 开发中的难题 试试多线程编程 想了解更多 有关作者   转载注明出处:http://www.infoq.com/cn/articles/js_multithread 虽然有越来越多的网站在应 ...

  5. 人脸和性别识别(基于OpenCV)

    描写叙述 人脸识别包含四个步骤 人脸检測:定位人脸区域,仅仅关心是不是脸: 人脸预处理:对人脸检測出来的图片进行调整优化. 收集和学习人脸:收集要识别的人的预处理过的人脸,然后通过一些算法去学习怎样识 ...

  6. Grunt学习日记

    Grunt和 Grunt 插件是通过npm安装并管理的, npm是Node.js的包管理器. 第一步:先安装node.js环境 第二步:安装Grunt-CLI 在node.js命令工具中输入npm i ...

  7. TableLayout与MigLayout

    最近新接触的两个Layout,另外之前用的GridBagLayoutHelper以及最近听说的Qt for java的QCSS据说也不错, 只是Qt的跨平台需要单独发布,假如使用QT for java ...

  8. hdoj--1872--稳定排序(水题)

     稳定排序 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  9. luogu3942将军令

    https://www.zybuluo.com/ysner/note/1302132 题面 在大小为\(n\)的树上选择尽量少的点,使得所有未选择的点距离选择了的点小于等于\(k\). \(n\leq ...

  10. [USACO15DEC]High Card Low Card (Platinum)

    https://www.zybuluo.com/ysner/note/1300791 题面 贝西和她的朋友艾尔西正在玩这个简单的纸牌游戏.游戏有\(2N\)张牌,牌上的数字是\(1\)到\(2N\). ...