
最短路径
SYaoJun
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
P1144 最短路计数
分析题目链接:P1144 最短路计数思路:先用BFS求每个节点的最短路径长度,然后使用记忆化搜索统计方案数。注意:使用邻接表保存图的边信息,因为存在重边,如果使用邻接矩阵存储会覆盖重边。代码#include <iostream>#include <vector>#include <cstring>#include <queue>using namespace std;int n, m;const int N =原创 2021-11-26 20:33:55 · 320 阅读 · 0 评论 -
单源最短路模板题
C++版牛客网题目链接注意处理重边class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int 顶点数 * @param m int 边数 * @param graph intvector<vector<>> 一维3个数据,表示顶点到另外一个顶点的边长度是多少 * @return in原创 2021-05-12 14:36:51 · 181 阅读 · 0 评论 -
854. Floyd求最短路
Floyd三重循环,一般顶点数不超过200时间复杂度O(n3)O(n^3)O(n3)#include <iostream>#include <cstring>#include <algorithm>using namespace std;int n, m, Q;const int N = 210;const int INF = 1e9;int...原创 2019-10-30 13:43:31 · 161 阅读 · 0 评论 -
852. spfa判断负环
AcWing题目链接#include <iostream>#include <vector>#include <algorithm> #include <cmath>#include <cstring>#include <queue>#include <unordered_map>using nam...原创 2019-10-30 11:41:49 · 180 阅读 · 0 评论 -
851. spfa求最短路
SPFA使用队列来保存更新过的顶点(栈或者优先队列也可以)。只有更新过的顶点才会产生松弛操作。#include <iostream>#include <vector>#include <algorithm> #include <cmath>#include <cstring>#include <queue>#i...原创 2019-10-30 11:22:20 · 144 阅读 · 0 评论 -
853. 有边数限制的最短路
Bellman-Ford求解有负权边的单源最路径问题。时间复杂度O(nm)O(nm)O(nm)通常来说用SPFA注意备份距离数组的使用#include <iostream>#include <cstring>#include <algorithm>using namespace std;int m, n, k;const int N = 5...原创 2019-10-29 23:03:19 · 232 阅读 · 0 评论 -
过年回家
牛客网题目链接Dijkstra算法最短路径问题#include <iostream>#include <string>#include <vector>#include <cctype>#include <cstdio>#include <cstring>#include <algorithm>...原创 2019-10-24 23:16:39 · 158 阅读 · 0 评论 -
7-35 城市间紧急救援 (25 分)
这道题已经做了很多遍了,但是这次做的时候还是出现了一个bug主要是没有注意scanf的输入特性,读入边的时候,不能同时处理边读入边使用的行为。必须先读完然后再使用。#include <iostream>#include <cstdio>#include <algorithm>#include <queue>#include <ve...原创 2019-10-20 13:32:00 · 783 阅读 · 0 评论 -
7-4 Dijkstra Sequence (30 分)
考查知识点:Dijkstra算法手动模拟一下就能看出规律#include <iostream>#include <cstdio>#include <stack>#include <algorithm> #include <vector> using namespace std;const int N =1e3+3;co...原创 2019-09-27 09:25:15 · 540 阅读 · 0 评论 -
P3371 【模板】单源最短路径(弱化版)
注意:1.Dijkstra算法边数超过1000之后,就需要用堆优化, 而不能用朴素的Dijkstra算法2.起点如果不可达某个点,需要初始化为21474836473.存在重边和自环#include <cstdio>#include <iostream>#include <vector>#include <string>#include...原创 2019-09-11 09:12:01 · 178 阅读 · 0 评论 -
850. Dijkstra求最短路 II
Dijkstra堆优化时间复杂度O(mlogn)O(mlogn)O(mlogn)#include <iostream>#include <vector>#include <algorithm> #include <cmath>#include <cstring>#include <queue>#include ...原创 2019-09-10 19:51:37 · 129 阅读 · 0 评论 -
2673 最短路径
题目链接朴素Dijkstra算法包含重边和自环#include <iostream>#include <vector>#include <algorithm> #include <cmath>#include <cstring>using namespace std;const int N = 505;const in...原创 2019-09-02 16:04:26 · 259 阅读 · 0 评论 -
849. Dijkstra求最短路 I
题目链接朴素Dijkstra算法有自环和重边#include <iostream>#include <vector>#include <algorithm> #include <cmath>#include <cstring>using namespace std;const int N = 505;const int...原创 2019-09-02 15:57:02 · 121 阅读 · 0 评论 -
1087 All Roads Lead to Rome (30 分)
题目链接#include<bits/stdc++.h>using namespace std;typedef long long LL;const int maxn=1e2+5;const int INF = 0x3fffffff;int m,n,x; int k=0;int h[maxn]={0};vector<int> pre[maxn];vecto...原创 2019-08-08 07:51:27 · 232 阅读 · 0 评论 -
1150 Travelling Salesman Problem (25 分)
很繁琐的题目,难其实不难。#include<bits/stdc++.h>using namespace std;const int maxn=205;const int INF = 0x3fffffff; int G[maxn][maxn];int in[maxn],arr[maxn];int opti=0,optd=INF;int main(){ int n,m,u...原创 2019-07-25 08:11:39 · 150 阅读 · 0 评论 -
L2-001 紧急救援 (25 分)
紧急救援模板Dijkstra + DFS注意1.邻接矩阵要初始化2.松弛的路径也需要初始化3.DFS需要回溯4. id = tmpPath[i] 才表示顶点,不能直接用i#include<cstdio>#include<string>#include<iostream>#include<vector>using namespa...原创 2019-07-16 18:32:16 · 865 阅读 · 0 评论 -
最短路径问题
//这道题在HDU上好像超时了不知道什么原因#include #include using namespace std;const int N = 1005;const int INF =1000000000;int G[N][N],C[N][N];int d[N],c[N];bool vis[N];int n,m;void Dijkstra(int s){ fi原创 2018-01-30 01:59:53 · 240 阅读 · 0 评论 -
继续畅通工程【浙江大学】★★
题目描述 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。 输入描述: 测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1当N为0时输入结束。原创 2018-01-30 15:01:13 · 344 阅读 · 0 评论 -
Jungle Roads【北京大学】
题目描述 The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relen...原创 2018-03-01 17:15:18 · 299 阅读 · 0 评论 -
畅通工程【浙江大学】★
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。 输入描述: 测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M (N, M < =100 );随后的 N 行对应村...原创 2018-03-01 17:16:58 · 248 阅读 · 0 评论 -
连通图
#include<iostream>#define N 1005using namespace std;int father[N];int findFather(int x){ if(x == father[x]) return x; else{ int tp = findFather(father[x]); father[x] ...原创 2018-03-01 17:18:14 · 224 阅读 · 0 评论 -
最短路径算法
Floyd算法处理多源最短路径问题,即任意两个点的最短路径void Floyd(){ for(int k=0;k<n;k++){ //注意:k一定要放在最外层 for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(dis[i][k]!=INF && dis[k][j]!=INF &&...原创 2019-01-31 17:33:26 · 198 阅读 · 0 评论 -
1003 Emergency (25 分)
#include&lt;bits/stdc++.h&gt;using namespace std;const int maxn=505;const int INF = 1e9+5;int N,M,C1,C2,c1,c2,L;int weights[maxn],w[maxn]; //点权值 和 每个顶点的最大点权 int G[maxn][maxn]; //邻接矩阵图 int d[...原创 2019-01-29 23:06:45 · 204 阅读 · 0 评论 -
1030 Travel Plan (30 分)
#include<bits/stdc++.h>#include<vector>using namespace std;const int maxn=505;const int INF = 1e9+5;int N,M,S,D,c1,c2,dist,cost;int weights[maxn]; //点权值 int G[maxn][maxn]; //邻接矩阵...原创 2019-01-30 11:46:54 · 291 阅读 · 0 评论 -
1018 Public Bike Management (30 分)
因为是无向图,所以给边赋值的时候,就直接粘贴了,但是没有改变两个端点了位置,调试了一下午,终于找到问题原因了,可喜可贺,可歌可泣。其实代码写得清晰明了一些,可以节约大量的时间呀!#include&lt;iostream&gt;#include&lt;vector&gt;#include&lt;algorithm&gt;using namespace std;const int maxn...原创 2019-01-30 17:52:41 · 399 阅读 · 0 评论 -
7-9 旅游规划 (25 分)
单源最短路径使用Dijkstra求解#include<cstdio>#include<algorithm>using namespace std;int N,M,S,D;const int maxn=505;const int INF=1e9;int G[maxn][maxn];int C[maxn][maxn];int vis[maxn]={0};i...原创 2019-02-11 11:27:36 · 975 阅读 · 0 评论 -
1122 Hamiltonian Cycle (25 分)
题目叫做哈密尔顿环其实判断起来特别容易,首先判断是否所有的顶点都出现了一次,然后判断是否起点和终点都相同,再看每条边是否都在原来的图中存在,有个细节的地方说,这是一个简单的回路,所以不能有两个环或以上的环,因此每个顶点的出现次数最多不超过2,超过2的当然也只有起点和终点这个点,因为测试数据可能比较简单,所以暂且这样考虑就可以通过。#include<cstdio>#include&...原创 2019-02-26 10:59:14 · 268 阅读 · 0 评论 -
最短路【HDOJ2544】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544Dijkstra版本:#include #include using namespace std;const int N = 1005;const int INF =0xfffffff;int G[N][N];int d[N];bool vis[N];int n,m;vo原创 2018-01-30 00:40:53 · 239 阅读 · 0 评论