一、题目

POJ2387

二、分析

Bellman-Ford算法

 该算法是求单源最短路的,核心思想就是不断去更新到起点的最短距离,更新的前提是没有负边。如果有负边需要手动控制循环次数。

Dijkstra算法

 同样是单源最短路,它的核心是

 (1) 找到最短距离已经确定的顶点,再从该顶点出发,更新与它相邻的点的最短距离。

 (2) 对于最短距离已经确定的点不再更新。

Floyd算法

 可以求解任意两点之间的最短距离。但是这题会TLE。

三、AC代码

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <vector>
5 #include <fstream>
6 using namespace std;
7 const int MAXN = 2e3+14;
8 const int INF = 0x3f3f3f3f;
9 struct edge
10 {
11 int from, to, cost;
12 }E[MAXN<<1];
13 int T, N, C;
14 int dist[MAXN];
15 void Bellman_Ford()
16 {
17 memset(dist, INF, sizeof(dist));
18 dist[1] = 0;
19 while(1)
20 {
21 bool flag = 0;
22 for(int i = 0; i < C; i++)
23 {
24 if(dist[E[i].from] != INF && dist[E[i].to] > dist[E[i].from] + E[i].cost)
25 {
26 dist[E[i].to] = dist[E[i].from] + E[i].cost;
27 flag = 1;
28 }
29 }
30 if(!flag)
31 break;
32 }
33 }
34 int main()
35 {
36 //freopen("in.txt", "r", stdin);
37 scanf("%d%d", &T, &N);
38 C = 0;
39 int a, b ,c;
40 for(int i = 0; i < T; i++)
41 {
42 scanf("%d%d%d", &a, &b, &c);
43 E[C].from = a, E[C].to = b, E[C].cost = c;
44 C++;
45 E[C].from = b, E[C].to = a, E[C].cost = c;
46 C++;
47 }
48 Bellman_Ford();
49 printf("%d\n", dist[N]);
50 return 0;
51 }

Bellman_Ford

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <vector>
5 #include <fstream>
6 #include <vector>
7 #include <queue>
8 using namespace std;
9 typedef pair<int, int> P;
10 const int MAXN = 2e3+14;
11 const int INF = 0x3f3f3f3f;
12 struct edge
13 {
14 int from, to, cost;
15 edge(int f, int t, int c)
16 {
17 from = f, to = t, cost = c;
18 }
19 };
20 vector<edge> G[MAXN];
21 priority_queue<P> pq;
22 int T, N;
23 int dist[MAXN];
24
25 void Dijkstra(int s)
26 {
27 memset(dist, INF, sizeof(dist));
28 dist[s] = 0;
29 pq.push(P(0, s));
30 while(!pq.empty())
31 {
32 P p = pq.top();
33 pq.pop();
34 int v = p.second;
35 if(dist[v] < p.first)
36 continue;
37 for(int i = 0; i < G[v].size(); i++)
38 {
39 edge e = G[v][i];
40 if(dist[e.to] > dist[v] + e.cost)
41 {
42 dist[e.to] = dist[v] + e.cost;
43 pq.push(P(dist[e.to], e.to));
44 }
45
46 }
47 }
48 }
49
50 int main()
51 {
52 //freopen("in.txt", "r", stdin);
53 scanf("%d%d", &T, &N);
54 int a, b ,c;
55 for(int i = 0; i < T; i++)
56 {
57 scanf("%d%d%d", &a, &b, &c);
58 G[a].push_back(edge(a, b, c));
59 G[b].push_back(edge(b, a, c));
60 }
61 Dijkstra(1);
62 printf("%d\n", dist[N]);
63 return 0;
64 }

Dijkstra

POJ_2387 Til the Cows Come Hom 【最短路】的更多相关文章

  1. Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)

    Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...

  2. POJ2387 Til the Cows Come Home (最短路 dijkstra)

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  3. POJ-2387 Til the Cows Come Home ( 最短路 )

    题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  4. Til the Cows Come Home(最短路模板题)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Bessie is ...

  5. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

  6. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

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

  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. POj2387——Til the Cows Come Home——————【最短路】

    A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & ...

随机推荐

  1. Java 并发机制底层实现 —— volatile 原理、synchronize 锁优化机制

    本书部分摘自<Java 并发编程的艺术> 概述 相信大家都很熟悉如何使用 Java 编写处理并发的代码,也知道 Java 代码在编译后变成 Class 字节码,字节码被类加载器加载到 JV ...

  2. leetcode 122 123 309 188 714 股票买卖 动态规划

    这类问题有一个通法 https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/yi-ge-tong-y ...

  3. ARM cache 工作方式

    综述 现在的CPU的速度远远高于现代存储器的速度所以出现了cache.按和CPU距离分为L1级和L2级(SM)等.这里主要记录一下自己对cache的工作方式的学习理解. cache工作方式 cahe是 ...

  4. 云原生系列3 pod核心字段

    pod是容器化的基础,好比大楼的地基. Pod跟容器的关系 类比一下: POD: 物理机容器: 物理机上的一个进程: 容器只是Pod的一个普通字段. Pod的作用范围 跟容器的linux namesp ...

  5. Laravel Homestead 安装 使用教程详解!

    1 Laravel Homestead 1 安装: 1 下载: http://www.vagrantup.com/downloads.html 1 配置: 1 1 测试: 1 1 ********** ...

  6. Git常用命令速查表 & Git Basics & github : release 发布!

    Git常用命令速查表 & Git Basics  & github : release  发布! Git常用命令速查表: 1 1 1 1 1 http://git-scm.com/bo ...

  7. VSCode & outline & source code

    VSCode & outline & source code Dart 源码学习 outline 速览 dart-core List class instance-methods ht ...

  8. 微信小程序 TypeScript bug

    微信小程序 TypeScript bug 执行自定义预览前预处理命令失败! internal/modules/cjs/loader.js:584 throw err; ^ Error: Cannot ...

  9. Flutter 在同一页面显示List和Grid

    import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends State ...

  10. socket通信框架——boost asio

    boost asio是一个封装了基本socket的跨平台通信框架.它支持异步访问,并支持tcp的自动封闭控制等操作. 一个简单的通信协议可以为: header body body长 数据 通过boos ...