poj 2387 最短路模板题

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

O(n^2)

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <cmath>
11 #include <stdlib.h>
12 #define MAXSIZE 1005
13 using namespace std;
14
15 int t,n;
16 int m[MAXSIZE][MAXSIZE],dis[MAXSIZE],vis[MAXSIZE];
17
18 void dij(int s)
19 {
20     int i,j,k;
21     for(i = 0;i<n;i++)
22     {
23         dis[i] = m[s][i];
24         vis[i] = 0;
25     }
26     dis[s] = 0;
27     vis[s] = 1;
28     for(i = 1;i<=n;i++)
29     {
30         k = 0;
31         int mn = 1000000;
32         for(j = 1;j<=n;j++)
33             if(!vis[j]&&dis[j]<mn)
34             {
35                 mn = dis[j];
36                 k = j;
37             }
38         vis[k] = 1;
39         for(j = 1;j<=n;j++)
40             if(!vis[j]&&dis[j]>dis[k]+m[k][j])
41                 dis[j] = dis[k]+m[k][j];
42     }
43 }
44
45 int main()
46 {
47     freopen("caicai.txt","r",stdin);
48     cin>>t>>n;
49     int i,j,a,b,w;
50     for(i = 0;i<=n;i++)
51         for(j = 0;j<=n;j++)
52             m[i][j] = 1000;
53     for(i = 0;i<t;i++)
54     {
55         scanf("%d%d%d",&a,&b,&w);
56         if(m[a][b]>w)
57         {
58             m[a][b] = w;
59             m[b][a] = w;
60         }
61     }
62     dij(n);
63     cout<<dis[1]<<endl;
64     return 0;
65 }

时间: 2024-10-10 11:46:55

poj 2387 最短路模板题的相关文章

poj1511/zoj2008 Invitation Cards(最短路模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds      Memory Limit: 65536 KB In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fa

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

[poj2449]Remmarguts&#39; Date(K短路模板题,A*算法)

解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long ll; const int N=1e3+10; const

Sliding Window POJ - 2823 单调队列模板题

Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间最小值 就是维护一个单调递增的序列 对于样例 8 3 1 3 -1 -3 5 3 6 7 我们先模拟一遍 1.队列为空 1 进队 队列:1 2.3>队尾元素 3 进队 队列: 1 3 3.-1小于队尾元素,一直从尾部出队知道找到比-1小的元素或者队列为空 队列:-1 当队列中元素大于m的时候从队头删

hdu1874 最短路模板题

之所以做了第二道模板题还要写是因为发现了一些自己的问题 用的是dij 最简单的松弛 需要注意的地方是松弛的时候 判断dis[i]<dis[w]+tance[w][i]时 还要再判断 vis[i] 要保证这个点没有成为过最小点 即这个点不会是已经被松弛过的点 输入的时候要注意 可能会有重边的输入 每次输入的时候进行一次判断 如果输入的是较大值 就不用更换了 关于memset的使用 它只能用来设置0与-1 别的值会出现莫名的错误 #include<stdio.h> #include<s

POJ 3041 匈牙利算法模板题

一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分别是行数和列数 每有一个点 就让所处行列连一条边 求最小点覆盖 然后卡住...后来看了增林的博客... 最小点覆盖=最大匹配数 果然是模板题.. 然后wa.. 后来发现是当进行对左边点的遍历的时候 每次都要mem一次vis数组 应该是每次找之前都重新清空啊..不然下次怎么找啊...增光路对点的是否被

Til the Cows Come Home (最短路模板题)

个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book[1]=1就错了..... 纠结中.... 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 s

POJ - 1113 Wall (凸包模板题)

原题链接 模板题,直接说思路. 思路: 要求一距离凸包为 L 的图形的周长,即为 凸包周长+L为半径的圆周长 ,直接用 Graham 求一次凸包即可. 1 /* 2 * @Author: windystreet 3 * @Date: 2018-08-02 20:41:25 4 * @Last Modified by: windystreet 5 * @Last Modified time: 2018-08-02 22:30:59 6 */ 7 #include <stdio.h> 8 #inc

POJ 3041 Asteroids(二分图模板题)

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. Fortun