原题链接: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 (最短路径 模版题 三种解法)的更多相关文章

  1. 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 ...

  2. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  3. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  4. 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 ...

  5. 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 ...

  6. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  7. 怒学三算法 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 ...

  8. 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 ...

  9. Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)

    题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...

随机推荐

  1. greeting-150

    拿到程序例行检查,可以看出程序是32位的程序 将程序放入ida中进入主函数查看 但是我们将程序运行一次后发现程序还运行了nao的程序 说明程序在中间还引用了nao函数,通过代码审计我们可以很直接的看到 ...

  2. 『忘了再学』Shell基础 — 1、Shell的介绍

    目录 1.Shell的由来 2.Shell的两种执行指令方式 3.什么是Shell脚本 4.Shell 是一种脚本语言 1.Shell的由来 我们比较熟悉Windows系统的图形化界面,对于图形界面来 ...

  3. Table.LastN保留后面N….Last…(Power Query 之 M 语言)

    数据源: "姓名""基数""个人比例""个人缴纳""公司比例""公司缴纳"&qu ...

  4. CF1514A Perfectly Imperfect Array 题解

    Content 给定一个长度为 \(n\) 的序列,问是否存在一个非空子序列,使得这个子序列所有元素的积不是完全平方数. 数据范围:\(t\) 组数据,\(1\leqslant t\leqslant ...

  5. 常用故障排查监控shell脚本

    #!/bin/bash #ping_monitor.sh IP_ADDRESS=$1 if [ -n "$IP_ADDRESS" ] ; then while : do PING_ ...

  6. 总结Vue 第四天:vue-cli(Vue2.0 新手入门 — 从环境搭建到发布)

    总结Vue 第四天:vue-cli(Vue2.0 新手入门 - 从环境搭建到发布) 一.Vue CLI----(Vue2.0 新手入门 - 从环境搭建到发布): ■   CLI是Command-Lin ...

  7. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  8. 【九度OJ】题目1185:特殊排序 解题报告

    [九度OJ]题目1185:特殊排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1185 题目描述: 输入一系 ...

  9. 【LeetCode】398. Random Pick Index 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...

  10. [Xavier] Understanding the difficulty of training deep feedforward neural networks

    目录 概 主要内容 Glorot X, Bengio Y. Understanding the difficulty of training deep feedforward neural netwo ...