原题链接: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. 车载以太网第二弹|测试之实锤 -DoIP测试开发实践

    前言 车载以太网测试之实锤系列,之前我们已经从环境设备组成.被测对象组成再到测试过程和测试结果分析,分享了完整的PMA测试 .IOP测试 .TC8中的TCP/IP协议一致性测试 .也分享了1000BA ...

  2. 『学了就忘』Linux系统管理 — 85、工作管理相关命令

    目录 1.工作管理简介 2.如何把命令放入后台 3.后台管理命令 (1)查看后台的工作 (2)将后台暂停的工作恢复到前台执行 (3)把后台暂停的工作恢复到后台执行 4.后台命令脱离登录终端运行 1.工 ...

  3. CF1095B Array Stabilization 题解

    Content 有一个长度为 \(n\) 的数组 \(a_1,a_2,a_3,...,a_n\),现在需要从这些数中删除一个数,使得 \(\max\limits_{i=1}^na_i-\min\lim ...

  4. SP8374 PARKET1 - PARKET 题解

    Content 有一个 \(l\times w\) 大小的网格,其四周均被染成了红色,其余部分是棕色,已知红色网格与棕色网格的数量,求 \(l\) 与 \(w\) 的值. Solution 接下来给各 ...

  5. JAVA比较两个版本号的大小

    /** * 比较版本号的大小 (两个版本号格式应尽量相同) * * @param v1 版本号1 * @param v2 版本号2 * @return 正数:v1大 负数:v2大 0:相等 */ pu ...

  6. Java的运行机制

    Java程序运行机制 编译型(操作系统 c/c++) 解释型(网页 不追求速度) 程序运行机制

  7. UE4之第一个飞机游戏

    开始之前 UE4官网 初识ue4教程(1~9节): https://www.bilibili.com/video/BV164411Y732?p=1 第一个飞机游戏: http://www.sikied ...

  8. 【LeetCode】876. Middle of the Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...

  9. 1137 - Expanding Rods

    1137 - Expanding Rods    PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 M ...

  10. 1091. Tmutarakan Exams

    1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...