A - Til the Cows Come Home POJ - 2387
A - Til the Cows Come Home POJ - 2387
最短路
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 2010;
typedef pair<int, int> PII; // first 存距离 second 存编号
int T, N;
int head[maxn], cnt;
int dis[maxn];
bool st[maxn]; // 第 i 个点的最短路是否确定,是否需要更新
struct node {int to, w, next;
}e[maxn];void add(int u, int v, int w) {e[++cnt].next = head[u];e[cnt].to = v;e[cnt].w = w;head[u] = cnt;
}void init() {memset(dis, 0x3f3f3f3f, sizeof(dis));memset(head, 0, sizeof(head));memset(e, 0, sizeof(e));memset(st, 0, sizeof(st));cnt = 0;
}int dijkstra() {dis[N] = 0; // 第 n 个点到起点的距离priority_queue<PII, vector<PII>, greater<PII> > heap; // 小根堆heap.push({0,N});while(heap.size()) {PII t = heap.top();heap.pop();int ver = t.second, dist = t.first;if(st[ver]) continue; st[ver] = 1;for(int i = head[ver]; i; i = e[i].next) {int to = e[i].to;if(dis[to] > dist + e[i].w) {dis[to] = dist + e[i].w;heap.push({dis[to], to});}}}}int main() {// freopen("test.in", "r", stdin);while(~scanf("%d%d", &T, &N)) {init();for(int i = 1; i <= T; i++) {int u, v, w;scanf("%d%d%d", &u, &v, &w);add(u, v, w); add(v, u, w); }dijkstra();if(dis[1] != 0x3f3f3f3f) {printf("%d\n", dis[1]);}}return 0;
}
A - Til the Cows Come Home POJ - 2387相关推荐
- DIJSPFA-Til the Cows Come Home POJ - 2387
Til the Cows Come Home POJ - 2387 用dij和spfa写了做个对比,看了一篇很好的文章对这两个算法有了更好的理解**<Dijkstra+heap和SPFA的区别& ...
- 【最短路问题】Til The Cows Come Hone HDU 2387
题意:给一张图,1为起点,n为终点 求有权图单源最短路. 解题思路:dijkstra+堆优化.用优先队列来实现最小堆.新学到了优先队列和pair的用法 优先队列: priority_queue < ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- Poj 2387 Til the Cows Come Home 迪杰斯特拉(普通+优化)
Til the Cows Come Home 迪杰斯特拉(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n( ...
- 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 ...
- Til the Cows Come Home(简单的最短路)
Til the Cows Come Home Bessie 在外地,想要在 Farmer John 叫醒她早上挤奶之前回到谷仓尽可能多地睡觉.Bessie 需要她的美容觉,所以她想尽快回来. Farm ...
- Til the Cows Come Home(最短路-Dijkstra)
Til the Cows Come Home(最短路-Dijkstra) judge:https://vjudge.net/contest/297882#problem/A Time limit:10 ...
- POJ 2387 Til the Cows Come Home
传送门:http://poj.org/problem?id=2387 这是最短路径问题,本题有重边,但是spfa能解决这个问题: 实现代码: SPFA: 1 #include <iostream ...
- 【POJ】2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 题意:求从1到n的最短路 题解:板子题.spfa. 代码: 1 #include<iostream> 2 #incl ...
最新文章
- Rust 2018 即将到来:设法从 Rust 2015 过渡
- 基于USR-WiFi模块的 ESP32,ESP8266 Thonny调试器
- libpython3.7m.so.1.0: cannot open shared object file: No such file or directory
- Linux疑难杂症解决方案100篇(十一)-ubuntu crontab 详细规则及不执行时的解决方法
- 程序员数学基础【二、时间复杂度】(Python版本)
- 《剑指offer》链表分割
- 机器学习实战 | 数据探索
- redis 参数配置总结
- Linux 基金会成立持续交付基金会
- CVPR2021全新Backbone | ReXNet在CV全任务以超低FLOPs达到SOTA水平
- 写论文参考文献,如何查看一些书籍的随书光盘?如何查找一些书籍的原文阅读?如何高效合理的 运用高等学校数字图书馆、大学图书馆? 这里将给你答案
- 使用PYQT5打开海康威视工业相机并获取图像进行显示
- 本地搭建Git服务器,两台电脑共享代码如此简单
- ASM磁盘状态为forcing
- 让ambarella s2l 支持32M SPI Flash(W25Q256)
- FFA 2021 专场解读 - 平台建设
- 在线取名应用!给你自己取个好听的网名吧!
- Requests+Xpath 爬取豆瓣读书TOP并生成txt,csv,json,excel文件
- 最小树形图——朱刘算法学习小记
- 高通batterydata电池曲线数据学习