POJ2387 水水最短路

迪杰斯特拉哦,很挫哦

A - Til the Cows Come Home

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 2387

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.

AC代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <queue>
 6 #include <math.h>
 7 #define INF  1<<29
 8 using namespace std;
 9 int s[1011][1011],dis[1010],n,vis[1010];
10 void findeasy()//迪杰斯特拉算法是求从一个单源点到其余每个点的最短路径;
11 {
12     int x;
13     memset(vis,0,sizeof(vis));
14     for(int i=1; i<=n; i++)
15     {
16         dis[i]=s[n][i];//每个节点的初始状态
17     }
18     vis[n]=1;
19     for(int i=0; i<n; i++)
20     {
21         int min1=INF;
22         for(int j=1; j<=n; j++)
23         {
24             if(!vis[j]&&dis[j]<min1)//每次从剩余节点里挑个最短的节点,对于这个节点来说这是他的最短路,不需要再继续更新了,就算继续更新也不会再变短了;
25             {
26                 min1=dis[j];
27                 x=j;
28             }
29         }
30         vis[x]=1;
31         for(int j=1; j<=n; j++)//松弛操作,每次找到一个节点就检验一下是否能够使得其余节点松弛.
32         {
33             if(!vis[j]&& dis[j]> dis[x] + s[x][j])
34             {
35                 dis[j]=dis[x]+s[x][j];
36             }
37         }
38     }
39     printf("%d\n",dis[1]);
40 }
41 int main()
42 {
43     int T,a,b,c;
44     scanf("%d%d",&T,&n);
45     for(int i=1;i<=n;i++)//最短路问题初始化,不同点之间的距离设为无穷大,相同点的距离为零;
46     {
47         for(int j=1;j<=n;j++)
48         {
49             if(i!=j)
50             {
51                 s[i][j]=INF;
52             }
53             else
54             {
55                 s[i][j]=0;
56             }
57         }
58     }
59     while(T--)
60     {
61         scanf("%d%d%d",&a,&b,&c);
62         if(s[a][b]>c)//防止有坑的数据,使得不同点之间保存最短的那条路;
63         {
64             s[a][b]=c;
65             s[b][a]=c;
66         }
67     }
68     findeasy();
69     return 0;
70 }

时间: 2024-12-10 13:56:40

POJ2387 水水最短路的相关文章

poj2387 初涉最短路

前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码~ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int tance[1005][1005]; bool vis[1005]; int dis[1005]; int

poj2387(最短路)

题目连接:http://poj.org/problem?id=2387 题意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离. 分析:最短路裸题. #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <algorithm> #include <q

最短路Floyd(hdu1874),dijstra(poj2387)

Floyd算法,多源最短路,O(n^3) 所以时间很受限制-- 主要注意细节,记住简单的三层for循环就好 1.初始化输入: 多样例,所以数组清空 注意重边情况,注意自己到自己是0 2.三层for 循环遍历每个点k, 循环计算map[i][j],看i->j最小还是i->k->j最小. hdu1874 #include <cstdio> #include <cstring> #define MAX 202 #define min(x,y) x<y?x:y #d

POJ-2387 Til the Cows Come Home ( 最短路 )

题目链接: http://poj.org/problem?id=2387 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 ba

POj2387——Til the Cows Come Home——————【最短路】

A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2387 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before F

POJ-2387 Til the Cows Come Home (最短路+Dijkstra)

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 Joh

poj2387 spfa求最短路

1 //Accepted 4688 KB 63 ms 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <queue> 6 #include <cmath> 7 #include <algorithm> 8 using namespace std; 9 /** 10 * This is a documentation comment bl

poj2387 最短路

题意:给出一堆双向路,求从N点到1点的最短路径,最裸的最短路径,建完边之后直接跑dij或者spfa就行 dij: 1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 typedef pair<int,int> pii; 8 const in

超水的一道最短路poj2387

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 68667   Accepted: 23016 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