原题链接: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. SQL Server 2014如何DATEDIFF()函数截取对应时间年月日

    4.1 定义和用法: DATEDIFF()函数返回两个日期之间的时间 4.2 语法 DATEDIFF(datepart,startdate,enddate) datepart值: year | qua ...

  2. ES6解构赋值的简单使用

    相较于常规的赋值方式,解构赋值最主要的是'解构'两个字,在赋值的过程中要清晰的知道等号右边的结构. 先简单地看一下原来的赋值方式. var a=[1,2] 分析一下这句代码的几个点: (1)变量申明和 ...

  3. MySQL 面试题汇总(持续更新中)

    COUNT COUNT(*) 和 COUNT(1) 根据 MySQL 官方文档的描述: InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) opera ...

  4. Table.NestedJoin合并…Join(Power Query 之 M 语言)

    数据源: "销量表"和"部门表"两个查找表,每个表中都有"姓名"列 目标: 根据"姓名列"将"部门表" ...

  5. 突破类型限制的“数据透视图”(Excel技巧集团)

    Excel中,图表一共16个大类,但是数据透视图却被"阉"了好几个-- 这也就是说,数据透视图无法与上图中高亮标出的图表类型并存了? 确实如此,但并不绝对,因为我们可以在" ...

  6. MM函数(Excel函数集团)

    此处文章均为本妖原创,供下载.学习.探讨! 文章下载源是Office365国内版1Driver,如有链接问题请联系我. 请勿用于商业!谢谢 下载地址:https://officecommunity-m ...

  7. AcWing1341. 十三号星期五

    题目: 十三号星期五真的很不常见吗? 每个月的十三号是星期五的频率是否比一周中的其他几天低? 请编写一个程序,计算 N 年内每个月的 13 号是星期日,星期一,星期二,星期三,星期四,星期五和星期六的 ...

  8. Qt的VS插件下载地址

    地址 https://download.qt.io/official_releases/vsaddin/2.4.3/

  9. 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 日期 题目地址:https://leetcod ...

  10. 1374 - Confusion in the Problemset

    1374 - Confusion in the Problemset    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory ...