博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
阅读量:6882 次
发布时间:2019-06-27

本文共 5595 字,大约阅读时间需要 18 分钟。

Til the Cows Come Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 33015   Accepted: 11174

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 51 2 202 3 303 4 204 5 201 5 100

Sample Output

90 理论上的效率应该是Dijsktra > SPFA > Bellman_Ford,但是前两者我用了vector,影响了效率,导致贝尔曼是最快的,迪杰斯特拉其次。
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 7 const int SIZE = 1005; 8 const int INF = 0x2fffffff; 9 bool S[SIZE];10 int N,D[SIZE];11 struct Node12 {13 int vec,cost;14 };15 struct comp16 {17 bool operator ()(int & a,int & b)18 {19 return D[a] > D[b];20 }21 };22 vector
G[SIZE];23 priority_queue
,comp> QUE;24 25 void dijkstra(int);26 void relax(int,int,int);27 int main(void)28 {29 int t,from;30 Node temp;31 32 scanf("%d%d",&t,&N);33 while(t --)34 {35 scanf("%d%d%d",&from,&temp.vec,&temp.cost);36 G[from].push_back(temp);37 swap(from,temp.vec);38 G[from].push_back(temp);39 }40 dijkstra(N);41 printf("%d\n",D[1]);42 43 return 0;44 }45 46 void dijkstra(int s)47 {48 fill(D,D + SIZE,INF);49 D[s] = 0;50 S[s] = true;51 QUE.push(s);52 53 while(!QUE.empty())54 {55 int cur = QUE.top();56 int len = G[cur].size();57 S[cur] = true;58 QUE.pop();59 for(int i = 0;i < len;i ++)60 relax(cur,G[cur][i].vec,G[cur][i].cost);61 if(cur == 1)62 return ;63 }64 }65 66 void relax(int from,int to,int cost)67 {68 if(D[to] > D[from] + cost)69 {70 D[to] = D[from] + cost;71 if(!S[to])72 QUE.push(to);73 }74 }
Dijkstra
1 #include 
2 #include
3 using namespace std; 4 5 const int INF = 0x5fffffff; 6 const int SIZE = 1005; 7 bool UPDATE; 8 int D[SIZE]; 9 int N,E;10 struct Node11 {12 int from,to,cost;13 }Edge[SIZE * 4];14 15 void Bellman_Ford(int);16 void relax(int,int,int);17 int main(void)18 {19 int t;20 Node temp;21 22 scanf("%d%d",&t,&N);23 while(t --)24 {25 scanf("%d%d%d",&temp.from,&temp.to,&temp.cost);26 Edge[E ++] = temp;27 swap(temp.from,temp.to);28 Edge[E ++] = temp;29 }30 Bellman_Ford(N);31 printf("%d\n",D[1]);32 33 return 0;34 }35 36 void Bellman_Ford(int s)37 {38 fill(D,D + SIZE,INF);39 D[s] = 0;40 41 for(int i = 0;i < N - 1;i ++)42 {43 UPDATE = false;44 for(int j = 0;j < E;j ++)45 relax(Edge[j].from,Edge[j].to,Edge[j].cost);46 if(!UPDATE)47 return ;48 }49 }50 51 void relax(int from,int to,int cost)52 {53 if(D[to] > D[from] + cost)54 {55 D[to] = D[from] + cost;56 UPDATE = true;57 }58 }
Bellman-Ford
1 #include 
2 #include
3 #include
4 using namespace std; 5 6 const int SIZE = 1005; 7 const int INF = 0x5fffffff; 8 int N,D[SIZE]; 9 bool IN_QUE[SIZE];10 struct Node11 {12 int to,cost;13 };14 vector
G[SIZE];15 16 void spfa(int);17 bool relax(int,int,int);18 int main(void)19 {20 int t,from;21 Node temp;22 23 scanf("%d%d",&t,&N);24 while(t --)25 {26 scanf("%d%d%d",&from,&temp.to,&temp.cost);27 G[from].push_back(temp);28 swap(from,temp.to);29 G[from].push_back(temp);30 }31 spfa(N);32 printf("%d\n",D[1]);33 34 return 0;35 }36 37 void spfa(int s)38 {39 int vec,cost;40 queue
que;41 fill(D,D + SIZE,INF);42 D[s] = 0;43 IN_QUE[s] = true;44 que.push(s);45 46 while(!que.empty())47 {48 int cur = que.front();49 int len = G[cur].size();50 IN_QUE[cur] = false;51 que.pop();52 53 for(int i = 0;i < len;i ++)54 {55 vec = G[cur][i].to;56 cost = G[cur][i].cost;57 if(relax(cur,vec,cost) && !IN_QUE[vec])58 {59 IN_QUE[vec] = true;60 que.push(vec);61 }62 }63 }64 }65 66 bool relax(int from,int to,int cost)67 {68 if(D[to] > D[from] + cost)69 {70 D[to] = D[from] + cost;71 return true;72 }73 return false;74 }
SPFA

 

转载于:https://www.cnblogs.com/xz816111/p/4488288.html

你可能感兴趣的文章
运维监控利器Nagios:概念、结构和功能
查看>>
【Absible学习】Ansible常用模块---命令类模块
查看>>
总结:mysql三种灾备与恢复使用解析。
查看>>
Lync Server 2010详解系列1:环境介绍和准备
查看>>
Android系统Surface机制的SurfaceFlinger服务对帧缓冲区(Frame Buffer)的管理分析
查看>>
DHCP服务器如何检测穿过中继代理的IP地址冲突(gratuitous ARP肯定是不行的)
查看>>
XAML实例教程系列 – 开篇
查看>>
cocos2d-x学习笔记06:如何将win32移植到android
查看>>
Anonymous几天之内攻陷500多个中国网站
查看>>
6421B Lab4 IPv6的配置与故障排除
查看>>
让Windows Server 2008 R2 SP1 的“网络发现”真正能发现和被发现
查看>>
工作中 Oracle 常用数据字典集锦
查看>>
SFB 项目经验-12-为某上市企业的Skype for Business购买Godday证书
查看>>
NBU备份时常见错误代码分析及处理
查看>>
[C#基础知识]专题十三:全面解析对象集合初始化器、匿名类型和隐式类型
查看>>
大数据虚拟化零起点-2基础运维第一步-环境规划和准备
查看>>
Tomcat端口被占用解决方法(不用重启)
查看>>
双11,大小荣的不同玩法
查看>>
Jeep自由光:汽车界的iPhone6
查看>>
ASP.NET MVC 5 -从控制器访问数据模型
查看>>