超水的一道最短路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 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.

Source

USACO 2004 November

直接上spfa侍候。

才发现自己的博客里面没有关于spfa的学习笔记qwq

 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<vector>
 5 #include<algorithm>
 6 using namespace std;
 7 const int inf=0x3f3f3f;
 8 int mp[1006][1006];
 9 int dist[1006];
10 bool book[1006];
11 int T,n;
12
13 void spfa()
14 {
15     memset( book, 0, sizeof book);
16     book[1]=1;
17     queue<int> iop;
18     iop.push(1);
19     dist[1]=0;
20     while( !iop.empty()){
21         int kop=iop.front();
22         iop.pop();
23         for(int i=1;i<=n;i++){
24             if(dist[kop]+mp[kop][i]<dist[i]){
25                 dist[i]=dist[kop]+mp[kop][i];
26                 if(!book[i]){
27                     book[i]=true;
28                     iop.push(i);
29                 }
30             }
31         }
32         book[kop]=false;
33     }
34     cout << dist[n] <<endl;
35 }
36
37 int main()
38 {
39     while( cin >> T >> n){
40         memset( mp, inf, sizeof mp);
41         memset( dist, inf, sizeof dist);
42         for(int i=1;i<=T;i++){
43             int x,y,z;
44             cin >> x >> y >> z;
45             mp[x][y]=min( mp[x][y],z);
46             mp[y][x]=min( mp[y][x],z);
47         }
48         spfa();
49     }
50     return 0;
51 }

原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9074023.html

时间: 2024-10-10 00:57:08

超水的一道最短路poj2387的相关文章

G 最水的一道

G - Here Be Dragons The Triwizard Tournament's third task is to negotiate a corridor of many segments, and reach the other end. The corridor is N segments long. The ith segment is either empty or has a dragon. Harry cannot pass the dragon and will ha

NBUT 1115 Cirno&#39;s Trick (超水)

题意:给出多个double数,去掉其最小的和最大的,再对余下的求均值. 思路:再输入时将最大和最小去掉,顺便统计非最值的和,输出时除一下个数即可. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 //freopen("input.txt", "r", stdin); 7 int n; 8 while(~scanf("%d",&n))

Hard??没搞错吧,LeetCode水题一道:Find Minimum in Rotated Sorted Array II

做完Find Minimum in Rotated Sorted Array I觉得很简单,再做2发现也不难,但是LeetCode竟然给了一个hard评级,难道是我编程水平大有长进?哈哈我估计是对复杂度有很高的要求的情况下比较难吧.. Python(偷懒)做法就是一句话:return min(num) Java(基本做法)如下: public class Solution { public int findMin(int[] num) { int len=num.length; if (len=

Convert to Ones CodeForces(超水题)

题目大意:给你几个数,这些数里面只有0或1,你有两种操作:1.把一段区域内的所有数前后交换位置.2.把一段区域内所有数取反.(区域可大可小,可以是所有数也                       可以只有一个数).两个操作各有它的代价,你操作一次就要消耗一次代价,求把所有数都变成1所需要的最小代价. 输入:第一行输入n , ab, c :表示有n个数,操作1代价b,操作2代价c:     第二行输入n个数,就是你要操作的数组. 输出:一行,输出最小代价. 题目分析: 1.如何运用操作1 :

poj3013 Big Christmas Tree --- 最短路

我都不好意思在标题上写这是最短路 这题挺有意思,关键在于把题目所求的量转换为最短路问题. 题意: 给一个无向图,每个结点有权值p[i],每条边有权值w[i] 求使这颗树所有顶点与根节点1联通的最小花费, 最小花费=∑w[i]×∑p[i] 第一个∑是所有边,第二个∑是该边下所有结点的权值和 思路: 通过推导可以发现,对于每个结点,它被算入的花费为 p[i]*d[i], d[i]为该结点到根结点的距离. 于是豁然开朗了吧..水题一道 唯一的坑点是要int64,而且inf要足够大 #include<c

poj1125 基础最短路

题目:https://vjudge.net/problem/POJ-1125 Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For

Gym - 101291C (很有意思的最短路)

题意: 给出一张地图和机器人还有出口的位置,地图上面有障碍.然后给出UDLR上下左右四种指令,遇到障碍物或者越界的指令会忽略,剩下的继续执行. 只要到达出口就算找到出口,然后给你一串指令,让你修改指令达到出口,删除或插入任意一个指令花费为1,问让机器人能够找到出口所花费最少. 思路: 感觉很有意思的一道最短路,思路是把每个点分成变成指令长度个点+1,然后就相当于有n^3个点.然后指令是顺序执行的,所以当前点的状态最多到达 周围可到达点的同一状态.所以我们就可以建边,如果我们走到隔壁点的当前状态就

[ZPG TEST 114] 阿狸的英文名【水题】

1.      阿狸的英文名 阿狸最近想起一个英文名,于是他在网上查了很多个名字.他发现一些名字可以由两个不同的名字各取一部分得来,例如John(约翰)的前缀 "John"和Robinson(鲁滨逊)的后缀 "son" 连在一起就是Johnson. 现在他找到了两个喜欢的名字(名字可看作字符串),用A和B表示,他想知道取A的一个非空前缀和B的一个非空后缀,连接在一起能组成多少不同的字符串. 输入格式 输入两行,分别表示字符串A和B:字符串只包含小写英文字母. 输出格

【网络流24题】 No.12 软件补丁问题(最小转移代价 最短路)

[题意] T 公司发现其研制的一个软件中有 n 个错误, 随即为该软件发放了一批共 m 个补丁程序. 每一个补丁程序都有其特定的适用环境, 某个补丁只有在软件中包含某些错误而同时又不包含另一些错误时才可以使用.一个补丁在排除某些错误的同时, 往往会加入另一些错误.换句话说, 对于每一个补丁 i, 都有 2 个与之相应的错误集合 B1[i]和 B2[i],使得仅当软件包含 B1[i]中的所有错误, 而不包含 B2[i]中的任何错误时, 才可以使用补丁 i. 补丁 i 将修复软件中的某些错误 F1[