怒学三算法 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 ...
随机推荐
- Weka中EM算法详解
private void EM_Init (Instances inst) throws Exception { int i, j, k; // 由于EM算法对初始值较敏感,故选择run k mean ...
- UI:登录窗的自定义键盘
在创建一个自定义键盘的时候遇到的错误 //双重for循环,对于Button上的数字用二维数组 // NSArray * butArr[4][3] = {@[@"1",@&qu ...
- Linux下MySQL5.6的修改字符集编码为UTF8
一.登录MySQL查看用SHOW VARIABLES LIKE 'character%';下字符集,显示如下: +--------------------------+---------------- ...
- IAR EWARM Example Download List
https://srv.iar.com/ExamplesOnDemand/versions.xml http://netstorage.iar.com/SuppDB/Public/EXAMPLES/0 ...
- 关闭IE窗口
$a=(New-Object -comObject Shell.Application).Windows() ($a|?{$_.locationname -eq "人力与人才信息管理系统&q ...
- Java组待开发的任务
周枫: A.将digital,xylinkWeb修改为支持oracle版,并完成测试工作.准备好实施安装的步骤和每步需要的文件,比如发布的项目,tomcat,jdk,memcached,数据库等,在单 ...
- [AngularJS] Accessing Data in HTML -- controllerAs, using promises
<!DOCTYPE html> <html> <head> <title>Access Data From HTML</title> < ...
- Android横竖屏切换及其相应布局载入问题
第一.横竖屏切换连带载入多屏布局问题: 假设要让软件在横竖屏之间切换.因为横竖屏的高宽会发生转换,有可能会要求不同的布局. 能够通过下面两种方法来切换布局: 1)在res文件夹下建立layout-la ...
- windows下如何github ssh 公钥
windows下如何github ssh 公钥 1. 安装git,从程序目录打开 "Git Bash" 2. 键入命令:ssh-keygen -t rsa -C " ...
- 解决fonts.gstatic.com无法访问
最近很多google的服务又在大陆地区受限了,原因不做过多讨论.屏蔽这些服务不仅仅意味着gmail,谷歌学术等方便的工具使用受到限制,更意味着很多寄托于google的web服务无法使用.wordpre ...