POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
题目大意:有 个点,给出从
点到
点的距离并且
和
是互相可以抵达的,问从
到
的最短距离。
题目分析:这是一道典型的最短路径模版题,需要注意的是:使用dijkstra算法求解需要考虑有重复边问题,而使用bellman-ford算法 和 spfa算法 可以忽略这个问题。
代码如下:
// Dijkstra
#include <iostream>
using namespace std;
const int INTFY = 1 << 29;
const int MAX = 1005;
int map[MAX][MAX];
int n,m;
void dijkstra() {
int min, v;
int d[MAX];
bool vis[MAX];
for(int i = 1; i <= n; i++) {
vis[i] = 0;
d[i] = map[1][i];
}
for(int i = 1; i <= n; i++) {
min = INTFY;
for(int j = 1; j <= n; j++)
if(!vis[j] && d[j] < min) {
v = j;
min = d[j];
}
vis[v] = 1;
for(int j = 1; j <= n; j++)
if(!vis[j] && d[j] > map[v][j] + d[v])
d[j] = map[v][j] + d[v];
}
cout << d[n] << endl;
}
int main() {
int a, b, c;
while(cin >> m >> n) {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(i == j)
map[i][i] = 0;
else map[i][j] = map[j][i] = INTFY;
for(int i = 1; i <= m; i++) {
cin >> a >> b >> c;
if(map[a][b] > c) map[a][b] = map[b][a] = c;
}
dijkstra();
}
return 0;
}
// Bellman-ford
#include <iostream>
using namespace std;
const int INFTY = 1 << 29;
const int MAXM = 2005;
const int MAXV = 1005;
typedef struct {
int a, b, w;
}Edge;
Edge edge[MAXM];
int n, m;
void bellman_ford() {
int d[MAXV];
for(int i = 2; i <= n; i++) d[i] = INFTY;
d[1] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(d[edge[j].a] > edge[j].w + d[edge[j].b]) d[edge[j].a] = edge[j].w + d[edge[j].b];
if(d[edge[j].b] > edge[j].w + d[edge[j].a]) d[edge[j].b] = edge[j].w + d[edge[j].a];
}
}
cout << d[n] << endl;
}
int main() {
int a, b, c;
while(cin >> m >> n) {
for(int i = 1; i <= m; i++) {
cin >> a >> b >> c;
edge[i].a = a;
edge[i].b = b;
edge[i].w = c;
}
bellman_ford();
}
return 0;
}
// Spfa
#include <iostream>
#include <queue>
using namespace std;
const int INFTY = 1 << 29;
const int MAXM = 4005;
const int MAXV = 1005;
typedef struct{
int a, b, w, next;
}Edge;
Edge edge[MAXM];
int n, m, headlist[MAXV];
void spfa() {
int d[MAXV], vis[MAXV];
queue<int> q;
for(int i = 2; i <= n; i++) {
d[i] = INFTY;
vis[i] = 0;
}
d[1] = 0;
vis[1] = 1;
q.push(1);
while(!q.empty()) {
int v = q.front(); q.pop();
vis[v] = 0;
for(int i = headlist[v]; i != -1; i = edge[i].next)
if(d[v] + edge[i].w < d[edge[i].b]) {
d[edge[i].b] = d[v] + edge[i].w;
if(!vis[edge[i].b]) {
vis[edge[i].b] = 1;
q.push(edge[i].b);
}
}
}
cout << d[n] << endl;
}
int main() {
int a, b, c;
while(cin >> m >> n) {
for(int i = 1; i <= n; i++) headlist[i] = -1;
for(int i = 1; i <= 2 * m; i += 2) {
cin >> a >> b >> c;
edge[i].a = a;
edge[i].b = b;
edge[i].w = c;
edge[i].next = headlist[a];
headlist[a] = i;
edge[i+1].a = b;
edge[i+1].b = a;
edge[i+1].w = c;
edge[i+1].next = headlist[b];
headlist[b] = i + 1;
}
spfa();
}
return 0;
}
POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)的更多相关文章
- 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 Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- 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 ...
- 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 (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
随机推荐
- Excel如何使用VLOOKUP函数多条件匹配查找数据
一.对应源数据如sheet6所示,对应需查找的数据如sheet7所示 二.在sheet6中添加一列辅助列 三.在sheet7对应位置插入vlookup函数 四.最终结果如下图所示
- 安装火狐浏览器报错找不到VCRUNTIME140_1.DLL
产生原因参考及下载地址:https://cn.dll-files.com/vcruntime140_1.dll.html vcruntime140_1.dll 相关的错误可能源于多种不同原因.比如,错 ...
- Python 如何管理类的创建行为
问题 如果我们要给类加上一个属性,只需在定义的时候加上属性就可以了: class Animal: can_fly = True 如果这样的类有很多,我们可以定义一个父类,让其它类继承他就可以了: cl ...
- Table.FirstN保留前面N….First…(Power Query 之 M 语言)
数据源: "姓名""基数""个人比例""个人缴纳""公司比例""公司缴纳"&qu ...
- JetBrains又出神器啦!Fleet,体验飞一般的感觉
目录 简介 从eclipse到Fleet Fleet的特性 JetBrains Space 总结 简介 java开发的同学可能对于JetBrains这家公司并不陌生,因为JetBrains号称拥有世界 ...
- THUSC 2021 游记
想了想不往博客园放不行,还是放上来了. 原文 \[\texttt{Brief Introduction} \] 众所周知,THUSC2021 5 月 15-16 日在杭州市 XJ 中学举办,然而由于 ...
- atexit模块介绍
atexit 模块介绍 python atexit 模块定义了一个 register 函数,用于在 python 解释器中注册一个退出函数,这个函数在解释器正常终止时自动执行,一般用来做一些资源清理的 ...
- Error: Not found: 'package:json_annotation/json_annotation.dart'
问题原因 json_annotation版本不对 修改json_annotation版本号 当前可用版本号 json_annotation: ^2.2.0
- SpringBoot整合Light Security框架
官方git地址:https://gitee.com/itmuch/light-security/tree/master 引入maven <dependency> <groupId&g ...
- cmake以源码的方式引入第三方项目
最前 本文将介绍一种以源码的方式引入第三方库的方法 准备 主项目,需要引用第三方库的某些函数 第三方库,以源码的形式提供给主项目使用 注意: 本文的背景:已经将第三方源码下载好. 一个例子 我这里准备 ...