Til the Cows Come Home (USACO 2004 November)(Dijkstra算法)

简介: Til the Cows Come Home (USACO 2004 November)(Dijkstra算法)

   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()在函数里面我们要用到一个标记,这个很重要可以防止前面的循环对后面影响,然后·第一步就是找一个连接起点最短的点然后标记让后面的计算中不能影响,否则会出现不必要的值,或者错乱,然后下一个代码就是找k下一个节点就是离k最短的那个点,然后就建立了一个完整的生态圈,最后打出dis[m];


下面是ac代码,关建点都有注释。

f58230e9f063709cf3167704f4efdf14.gif


有事你就q我;QQ2917366383


学习算法


额考虑到c语言好久没有写了就练习了一下,想看c++的可以找我。

#include<stdio.h>
#define maxn 0x3fffffff
int dis[1005],map[1005][1005];
void dk(int m)
{
  int p[1005]={0};//先全部初始化为0 
  p[1]=1;//自己到自己就是不行,标记他 
  int i,j,k,min; 
  for(int i=1;i<=m;i++)
  {
    min=maxn;//方便看 
    k=1;//怕出现特例 
    for(int j=1;j<=m;j++)
    if(!p[j]&min>dis[j])//标记大法,防止前面的循环影响后面的; 
    {
      min=dis[j];
      k=j;
    }//找到一个1连接的最短值 
    p[k]=1;//标记他,自己不能自己到自己 
    for(int j=1;j<=m;j++)
    {
      if(!p[j]&dis[j]>dis[k]+map[k][j])//相当于由前面的k接下一个节点 
      {
        dis[j]=dis[k]+map[k][j];//进行比较得到k下一个 节点的最短距离 
      }
    }
  }
  printf("%d",dis[m]);//听懂掌声 
 } 
int main() 
{
  int n,m,a,b,i,j,line;//定义部分 
  while(scanf("%d%d",&n,&m)!=EOF)//可以多次输入 测试数据 
  {
    for(int i=1;i<=m;i++)
    {
      map[i][i]=0;
      for(int j=1;j<i;j++)//细节哦,j<i,才能遍历哦(不遍历i=j) 
      map[i][j]=map[j][i]=maxn;//相当于把初始化最大,后面有用 
    }
    for(int i=1;i<=n;i++)//几个边 
    {
      scanf("%d%d%d",&a,&b,&line);//点和距离 
      if(line<map[a][b])//用了前面提到的maxn来变相赋值 
      map[a][b]=map[b][a]=line;//没有方向所以都要赋值 
    }
    for(int i=1;i<=m;i++)//接下来就是由1为出发点到点i都距离 
    dis[i]=map[1][i];
    dk(m);//下面就是考验思维了 
  }
}
相关文章
|
3月前
|
人工智能 算法 数据可视化
路径规划最全综述+代码+可视化绘图(Dijkstra算法+A*算法+RRT算法等)-2
路径规划最全综述+代码+可视化绘图(Dijkstra算法+A*算法+RRT算法等)-2
237 0
|
4月前
|
算法
最短路之Dijkstra算法
最短路之Dijkstra算法
24 0
|
8月前
|
算法 C++
Dijkstra算法及其C++实现
如果从图中某一顶点(称为源点)到达另一顶点(称为终点)的路径可能不止一条,如何找到一条路径使得沿此路径上各边上的权值总和达到最小。
45 0
|
3月前
|
存储 人工智能 算法
路径规划最全综述+代码+可视化绘图(Dijkstra算法+A*算法+RRT算法等)-1
路径规划最全综述+代码+可视化绘图(Dijkstra算法+A*算法+RRT算法等)-1
76 0
|
2月前
|
算法
关于Dijkstra算法
关于Dijkstra算法
|
4月前
|
算法
bellman_ford算法与dijkstra为什么dijkstra算法不能计算带有负权边图
bellman_ford算法与dijkstra为什么dijkstra算法不能计算带有负权边图
22 0
|
4月前
|
算法
leetcode-675:为高尔夫比赛砍树 (最短路径算法bfs,dijkstra,A*)
leetcode-675:为高尔夫比赛砍树 (最短路径算法bfs,dijkstra,A*)
33 0
|
4月前
|
存储 算法 定位技术
图论算法dijkstra dfs bfs以及动态规划
图论算法dijkstra dfs bfs以及动态规划
35 0
|
5月前
|
算法 定位技术 索引
class064 Dijkstra算法、分层图最短路【算法】
class064 Dijkstra算法、分层图最短路【算法】
32 0
|
5月前
|
算法
dijkstra算法与bellman_ford 为什么dijkstra算法不能计算带有负权边图
为什么dijkstra算法不能计算带有负权边图bellman_ford算法增加功能检测负权回路(不存在最短路径)
40 0