可惜没如果=_=
时光的河入海流
Til the Cows Come Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 112322   Accepted: 36010

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

 
SPFA的板子题,结果有不少坑,关于SPFA算法本身的有几个地方忘记了,一个是要开一个vis记录一下目标节点有没有在队列里,有的话就不用再添加了,还有一个是判断负环,如果一个点加入队列的次数超过n那就说明存在负环了。
然后就是双向边的话要注意开数组的时候范围要乘二
学了一下memset赋值∞,因为int里如果赋值0x7fffffff的话进行加法操作就会溢出,所以可以赋值0x3f3f3f3f
这里推荐一个大佬的博文:https://blog.csdn.net/jolinxia/article/details/39324061
这题有一个坑的地方,就是两个点之间可能有重边,所以还是要先开一个邻接矩阵存放两点之间的最小边
然后最坑的就是,这题laj被卡常了!!!mmp下次一定注意尽量把常数弄小的事情!!!
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 const int MAX=2005;
12 const int INF=0x3f3f3f3f;
13 int n,m,tot;
14 int head[MAX],adj[MAX*2],next[MAX*2],wei[MAX*2];
15 int map[MAX][MAX];
16 int dist[MAX];
17 bool vis[MAX];
18 void addedge(int u,int v,int w){
19     tot++;
20     adj[tot]=v;
21     next[tot]=head[u];
22     wei[tot]=w;
23     head[u]=tot;
24 }
25 void spfa(){
26     int i,j,x;
27     queue <int> q;
28     while (!q.empty()) q.pop();
29     q.push(1);
30     memset(vis,false,sizeof(vis));vis[1]=true;
31     while (!q.empty()){
32         x=q.front();q.pop();vis[x]=false;
33         for (i=head[x];i!=-1;i=next[i])
34             if (dist[adj[i]]>dist[x]+wei[i]){
35                 dist[adj[i]]=dist[x]+wei[i];
36                 if (!vis[adj[i]]){
37                     q.push(adj[i]);
38                     vis[adj[i]]=true;
39                 }
40             }
41     }
42 }
43 int main(){
44     freopen ("home.in","r",stdin);
45     freopen ("home.out","w",stdout);
46     int i,j;int u,v,w;
47     while(scanf("%d%d",&m,&n)!=EOF){
48         tot=0;
49         memset(head,-1,sizeof(head));
50         memset(dist,INF,sizeof(dist));dist[1]=0;
51         memset(map,INF,sizeof(map));
52         for (i=1;i<=m;i++){
53             scanf("%d%d%d",&u,&v,&w);
54             map[u][v]=map[v][u]=min(map[u][v],w);
55         }
56         for (i=1;i<=n;i++)
57             for (j=i+1;j<=n;j++)
58                 if (map[i][j]!=INF){
59                     addedge(i,j,map[i][j]);
60                     addedge(j,i,map[i][j]);
61                 }
62         spfa();
63         printf("%d\n",dist[n]);
64     }
65     return 0;
66 }

 

posted on 2021-03-12 20:05  珍珠鸟  阅读(55)  评论(0编辑  收藏  举报