怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 33015 | Accepted: 11174 |
Description
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
* 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
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90 理论上的效率应该是Dijsktra > SPFA > Bellman_Ford,但是前两者我用了vector,影响了效率,导致贝尔曼是最快的,迪杰斯特拉其次。
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
const int INF = 0x2fffffff;
bool S[SIZE];
int N,D[SIZE];
struct Node
{
int vec,cost;
};
struct comp
{
bool operator ()(int & a,int & b)
{
return D[a] > D[b];
}
};
vector<Node> G[SIZE];
priority_queue <int,vector<int>,comp> QUE; void dijkstra(int);
void relax(int,int,int);
int main(void)
{
int t,from;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&from,&temp.vec,&temp.cost);
G[from].push_back(temp);
swap(from,temp.vec);
G[from].push_back(temp);
}
dijkstra(N);
printf("%d\n",D[]); return ;
} void dijkstra(int s)
{
fill(D,D + SIZE,INF);
D[s] = ;
S[s] = true;
QUE.push(s); while(!QUE.empty())
{
int cur = QUE.top();
int len = G[cur].size();
S[cur] = true;
QUE.pop();
for(int i = ;i < len;i ++)
relax(cur,G[cur][i].vec,G[cur][i].cost);
if(cur == )
return ;
}
} void relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
if(!S[to])
QUE.push(to);
}
}
Dijkstra
#include <iostream>
#include <cstdio>
using namespace std; const int INF = 0x5fffffff;
const int SIZE = ;
bool UPDATE;
int D[SIZE];
int N,E;
struct Node
{
int from,to,cost;
}Edge[SIZE * ]; void Bellman_Ford(int);
void relax(int,int,int);
int main(void)
{
int t;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&temp.from,&temp.to,&temp.cost);
Edge[E ++] = temp;
swap(temp.from,temp.to);
Edge[E ++] = temp;
}
Bellman_Ford(N);
printf("%d\n",D[]); return ;
} void Bellman_Ford(int s)
{
fill(D,D + SIZE,INF);
D[s] = ; for(int i = ;i < N - ;i ++)
{
UPDATE = false;
for(int j = ;j < E;j ++)
relax(Edge[j].from,Edge[j].to,Edge[j].cost);
if(!UPDATE)
return ;
}
} void relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
UPDATE = true;
}
}
Bellman-Ford
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
const int INF = 0x5fffffff;
int N,D[SIZE];
bool IN_QUE[SIZE];
struct Node
{
int to,cost;
};
vector<Node> G[SIZE]; void spfa(int);
bool relax(int,int,int);
int main(void)
{
int t,from;
Node temp; scanf("%d%d",&t,&N);
while(t --)
{
scanf("%d%d%d",&from,&temp.to,&temp.cost);
G[from].push_back(temp);
swap(from,temp.to);
G[from].push_back(temp);
}
spfa(N);
printf("%d\n",D[]); return ;
} void spfa(int s)
{
int vec,cost;
queue<int> que;
fill(D,D + SIZE,INF);
D[s] = ;
IN_QUE[s] = true;
que.push(s); while(!que.empty())
{
int cur = que.front();
int len = G[cur].size();
IN_QUE[cur] = false;
que.pop(); for(int i = ;i < len;i ++)
{
vec = G[cur][i].to;
cost = G[cur][i].cost;
if(relax(cur,vec,cost) && !IN_QUE[vec])
{
IN_QUE[vec] = true;
que.push(vec);
}
}
}
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}
SPFA
怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)的更多相关文章
- POJ 2387 Til the Cows Come Home(dijkstra裸题)
题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...
- (简单) 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 pos ...
- 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 pos ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
随机推荐
- 如何解除Windows XP的IIS连接数限制
方法一: 开始 - 设置 - 控制面板 - 管理工具 - Internet 信息服务 - 默认网站 - 右键属性,把“网站”选项卡中“连接超时”下的复选框"保持HTTP连接"前的勾 ...
- android 弹出日期选择框
DatePickerDialog 在很多时候需要用户去设定时间,不可能让用户去在一个文本框中去输入时间,所以就需要有个日期弹出选择框,而这个框就是DatePickerDialog. 1.在API中的D ...
- 25.怎样创建一个Swift项目?
经历前面三部分的学习之后,我们对于Swift的有了基本的了解,知道它的基础语法,也知道了类.结构体.枚举.协议.扩展等等内容.但知道上面这些内容,并不代表我们就能很好的进行实际的项目开发了,本部分内容 ...
- 如果浏览器自动调用quirks模式打开的话
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-03-21) 则肯定你的html的声明,没有写好. 今天遇到几个,前面莫名其妙的多了个空格(在网页上看源码是多空格,复制到n ...
- eclipse提示servlet不存在 的解决办法
在以前的版本中,Tomcat的common/lib目录下有一个名为servlet-api.jar的包,把它拷贝至你的java安装目录下jre/lib/ext下就可以了. 如果是:tomcat6就在To ...
- iOS10适配知识点
http://ios.jobbole.com/89551/ http://ios.jobbole.com/88982/ 2.隐私数据访问问题 问题出现 现在app能运行了,当我打开相机时突然又cras ...
- 使用jQuery Mobile和Phone Gap开发Android应用程序
经过了一段时间的学习,初步了解了该如何使用jQuery Mobile和 Phone Gap来开发一个Android应用程序,也想把这些东西介绍给大家. 1. 软件准备 要进行android app的开 ...
- jQuery操作checkbox选择
1.checkbox list选择 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- Codeforces Gym 100463D Evil DFS
Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...
- OSG 实现跟随节点的相机(转)
本章教程将继续使用回调和节点路径(NodePath)来检索节点的世界坐标. 本章目标: 在一个典型的仿真过程中,用户可能需要从场景中的各种车辆和人物里选择一个进行跟随.本章将介绍一种将摄像机“依附 ...