德克萨斯纯朴的民众们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是它们并不是很擅长生产富含奶油的乳制品。农夫John此时身先士卒地承担起向德克萨斯运送大量的营养冰凉的牛奶的重任,以减轻德克萨斯人忍受酷暑的痛苦。John已经研究过可以把牛奶从威斯康星运送到德克萨斯州的路线。这些路线包括起始点和终点一共有 T 个城镇,为了方便标号为 1 到 T。除了起点和终点外的每个城镇都由双向道路连向至少两个其它的城镇。每条道路有一个通过费用(包括油费,过路费等等)。给定一个地图,包含 C 条直接连接 2 个城镇的道路。每条道路由道路的起点 Rs,终点 Re 和花费 Ci 组成。求从起始的城镇 Ts 到终点的城镇 Te 最小的总费用。

输入格式

第一行: 4 个由空格隔开的整数: T,C,Ts,Te;第 2 到第 C+1 行:第 i+1 行描述第 i 条道路,包含 3 个由空格隔开的整数: Rs,Re,Ci。

输出格式

一个单独的整数表示从 Ts 到 Te 的最小总费用。数据保证至少存在一条道路。

数据范围

1 ≤ T ≤ 2500,
1 ≤ C ≤ 6200,
1 ≤ Ts,Te,Rs,Re ≤ T,
1 ≤ Ci ≤ 1000

输入样例:

7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1

输出样例:

7

dijkstra ,vector存图

//http://t.csdn.cn/xFUZm
#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
bool check(int i, int j);
using pii = pair<int, int>;
const int N = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;int n, m, Ts, Te, dist[N];
bool st[N];
vector<pii> g[N];void init() {cin >> n >> m >> Ts >> Te;int a, b, c;while (m--) {cin >> a >> b >> c;g[a].emplace_back(b, c);g[b].emplace_back(a, c);}return;
}int dijkstra() {memset(dist, 0x3f, sizeof dist);priority_queue<pii, vector<pii>, greater<pii>> qu;qu.emplace(0, Ts);dist[Ts] = 0;while (qu.size()) {auto [d, x] = qu.top();qu.pop();if (st[x]) continue;st[x] = 1;for (auto& [nx, nd] : g[x]) {if (dist[nx] > d + nd) {dist[nx] = d + nd;qu.emplace(dist[nx], nx);}}}return dist[Te];
}void solve() {cout << dijkstra();return;
}int main(void) {ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;int TT = 1;//cin >> TT;for (int ii = 1; ii <= TT; init(), solve(), ii++, cout << "\n") {}return 0;
}

dijkstra

#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
bool check(int i, int j);const int N = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;int n, m, h[N], ne[N], w[N], idx, Ts, Te, dist[N], e[N];bool st[N];void add(int a, int b, int c) {e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}void init() {memset(h, -1, sizeof h);memset(dist, 0x3f, sizeof dist);cin >> n >> m >> Ts >> Te;for (int i = 0; i < m; i++) {int a, b, c;cin >> a >> b >> c;add(a, b, c);add(b, a, c);}return;
}using pii = pair<int, int>;int dijkstra() {priority_queue<pii, vector<pii>, greater<pii>> qu;qu.emplace(0, Ts);dist[Ts] = 0;while (qu.size()) {auto [d, x] = qu.top();qu.pop();if (st[x]) continue;st[x] = 1;for (int i = h[x]; i != -1; i = ne[i]) {if (d + w[i] < dist[e[i]]) {dist[e[i]] = d + w[i];qu.emplace(dist[e[i]], e[i]);}}}return dist[Te];
}void solve() {cout << dijkstra();return;
}int main(void) {ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;int TT = 1;//cin >> TT;for (int ii = 1; ii <= TT; init(), solve(), ii++, cout << "\n") {}return 0;
}

spfa

#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
bool check(int i, int j);const int N = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
using pii = pair<int, int>;vector<pii> g[N];
int n, m, Ts, Te, dist[N];
bool st[N];void init() {memset(dist, 0x3f, sizeof dist);cin >> n >> m >> Ts >> Te;while (m--) {int a, b, c;cin >> a >> b >> c;g[a].emplace_back(b, c);g[b].emplace_back(a, c);}return;
}void spfa() {queue<int> qu;qu.emplace(Ts);dist[Ts] = 0;st[Ts] = true;while (qu.size()) {int x = qu.front();qu.pop();st[x] = false;for (auto& [nx, td] : g[x]) {if (dist[x] + td < dist[nx]) {dist[nx] = dist[x] + td;if (st[nx]) continue;qu.emplace(nx);}}}
}void solve() {spfa();cout << dist[Te];return;
}int main(void) {ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;int TT = 1;//cin >> TT;for (int ii = 1; ii <= TT; init(), solve(), ii++, cout << "\n") {}return 0;
}

热浪(单源最短路问题)相关推荐

  1. 算法提高课-图论-差分约束- AcWing 1169. 糖果:spfa求单源最短路、差分约束

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 差分约束系统 差分约束系统是一种特殊的N元一次不等式组.它包含N个变量X1,...,XnX_1,...,X_nX1​,...,Xn​ ...

  2. 系兄弟就来砍我 有向图单源最短路

    系兄弟就来砍我 时间限制: 1 Sec  内存限制: 128 MB 题目描述 渣渣灰因为一句"大家好,我系渣渣辉,系兄弟就来砍我"引得众粉丝纷纷拿两米长的大刀寻找. 现有n个据点, ...

  3. 【算法】Bellman-Ford算法(单源最短路径问题)(判断负圈)

    单源最短路问题是固定一个起点,求它到其他所有点的最短路的问题. 算法: 设 d[i] 表示 起点 s 离点 i 的最短距离. 固定起点s,对所有的点 , i = s , d[i] 置为 0 :i != ...

  4. 【单源最短路】Dijkstra算法求最短路

    题目描述 给定一个 n 个点 m 条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出 1 号点到 n 号点的最短距离,如果无法从 1 号点走到 n 号点,则输出 −1. 输入格式 第一 ...

  5. 最短路问题之单源最短路-Dijkstra算法

    一个点(源点)到其余各个顶点的最短路径,叫做单源最短路经. 例如求下图中的1号顶点到2,3,4,5,6号顶点的最短路径. 使用二维数组e来存储顶点之间边的关系,初始值如下表. e 1 2 3 4 5 ...

  6. acwing单源最短路的建图模式总结

    .根据边权的范围以及问题求解的需要,最短路问题可以分为以下 4 种情形,分别用不同的算法求解. • 单源最短路径(固定一个顶点为原点,求源点到其他每个顶点 的最短路径) • 1. 边权非负:Dijks ...

  7. 【算法】单源最短路径和任意两点最短路径总结(补增:SPFA)

    [Bellman-Ford算法] [算法]Bellman-Ford算法(单源最短路径问题)(判断负圈) 结构: #define MAX_V 10000 #define MAX_E 50000 int ...

  8. NOWCODER 吃火锅(单源最短路dijkstra()算法)

    链接:https://ac.nowcoder.com/acm/problem/213225 来源:牛客网 题意: 有n个城市,m条双向道路. 第i条路线可用于从城市v[i]到城市u[i](以及从u[i ...

  9. 贪心算法单源点最短路径例题c语言源代码,Dijkstra算法是解单源最短路径问题的一个贪心算法...

    问题描述 给定一个带权有向图 G=(V,E) ,其中每条边的权是一个非负实数. 另外,还给定 V 中的一个项点,称为源. 现在我们要计算从源到所有其他各项点的最短路径长度. 这里的长度是指路上各边权之 ...

最新文章

  1. 假如有Thread1、Thread2、ThreaD3、Thread4四条线程分别统计C、D、E、F四个盘的大小,所有线程都统计完毕交给Thread5线程去做汇总,应当如何实现?...
  2. bzoj4429: [Nwerc2015] Elementary Math小学数学
  3. ElementUI中的el-table实现递增的序号列
  4. 【iBoard电子学堂】【iCore双核心板】资料光盘A盘更新,版本号为A6
  5. 运维监控再添新品,F5联合智维数据推出应用质量主动拨测解决方案
  6. VS2015+qt5.11入门(实现计算机的加法和登录操作)
  7. 【XML】Java对象数据的XML和JSON表示
  8. ant-design tree 设置默认选中状态_快速掌握文件夹位置的更改和文件的默认打开方式及重命名的操作...
  9. Redis:分布式锁setnx(只 实现了 互斥性和容错性)
  10. 基于pytorch实现图像分类——理解自动求导、计算图、静态图、动态图、pytorch入门
  11. yaml文件解析:c++篇
  12. Mac下使用数据库将Excel数据转换存入.plist
  13. 阿里高级技术专家:研发效能的追求永无止境 1
  14. 更新linux gcc版本到gcc 4.4.2
  15. SOC核心处理器单元解构分析
  16. 科技的性感:三星冰洗如何演绎时尚生活?
  17. 深度学习之ISAR超分辨率成像
  18. CSR蓝牙4.0与 APT-X高保真音频技术
  19. 解决电脑本地网络连接显示红叉又可上网问题
  20. JUC-BlockingQueue二

热门文章

  1. 数字华容道java_从零开发HarmonyOS(鸿蒙)手机小游戏——数字华容道
  2. 【服务端知识点】MAC OSX 安装MongoDB
  3. 北航计算机控制系统实验报告,北航计算机控制系统实验报告.doc
  4. 安装并使用Ghidra的Eclipse插件的过程
  5. 合唱队——最少出列人数
  6. Ubuntu安装过程中出现“没有定义根文件系统”,Ubuntu安装过程中无法读取Windows分区
  7. vue-easy-print批量分页打印
  8. 企业微信开发(自建应用h5)
  9. sakimichan网页版_PS笔刷推荐:光系、烟雾、铅笔、炭刷、油漆、涂鸦
  10. KEIL5护眼背景色以及字体颜色