搜索与图论
搜索与图论
DFS与BFS
树与图的遍历:拓扑排序
最短路
最小生成树
二分图:染色法、匈牙利算法
SoKeeGumpCN
瞎混
展开
-
洛谷 P2126 Mzc家中的男家丁(Kruskal)
Click here to Problem这个题出的有一点问题,数据量有点大,但是空间给的有点少了,Java好像过不去,只能用Cpp写,是一个最短路的板子。直接克鲁斯卡尔,有点忘了,基于并查集思想。cpp AC code#include <bits/stdc++.h>using namespace std;const int N = 2310, M = 400010, INF = 0x3f3f3f3f;int n, m;int p[N];struct Edge{原创 2021-01-14 22:30:59 · 170 阅读 · 0 评论 -
洛谷 P1194 买礼物 (Kruskal)
Click is the Problem洛谷的题感觉判的比较严,AcWing的模板还要再加修改才能过。这个题题面看上去不知道在说啥,样例也不好理解。如果看出是最短路就是一道模板题了,还是有点难度的。i 对 j 有优惠则建边,跑一遍kruskal即可。import java.io.*;import java.util.Arrays;import java.util.PriorityQueue;import java.util.Queue;import java.util.StringToken原创 2021-01-14 21:58:15 · 379 阅读 · 1 评论 -
关于程序设计最大值“0x3F3F3F3F“问题讨论
在程序设计特别是有向图算法中经常需要去表达不可达的距离,也就是无穷大来填充邻接矩阵,初学者包括我自己会使用类似9999999(一刀暴击?)或者干脆直接Integer.MAX_VALUE这样不专业的手法,前者只需要根据数据的范围合理划定即可(尽管看起来非常难受),而后者——如果算法中从存在松弛操作例如dist[k] = Math.min(dist[k], dist[t] + graph[t][k]); //源自于Dijkstra的更新部分dist[k] 便会超过Integer的最大范围从而变为负数,原创 2021-01-14 15:13:26 · 1125 阅读 · 0 评论 -
HDU1045 Fire Net(dfs)
Problem由于数据量较小,本题可以硬上dfs,遍历每个点能否放下棋子。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;class Main { static BufferedReader br = new...原创 2020-04-04 14:25:53 · 109 阅读 · 0 评论 -
HDU2680 Choose the best route(dijkstra)
problem迪杰,但是不知道为什么就是过不去,hdu这一点挺麻烦的。给出Cpp过题代码,哪来的我也记不得了。#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define INF 100000000using namespace std;i...原创 2020-04-04 14:16:03 · 155 阅读 · 0 评论 -
HDU2066 一个人的旅行(dijkstra) (坑爹的输入 fxxk)
Problem这个题真的是对Java快速读入一点都不友好,调试了一小时,反复看,测试数据都没有问题,最后是边界问题的输入有问题,这**杭电OJ也不给看测试数据。用的是迪杰。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.A...原创 2020-03-31 11:16:55 · 162 阅读 · 0 评论 -
HDU3790 最短路径问题 (多源汇dijkstra)
Problem多源汇迪杰算法,就是把模板的从第一点换成每次从start开始到end奇怪的是正常提交报超内存,居然用一次System.gc()就能过了???import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;class Main { static Bu...原创 2020-03-29 22:22:15 · 151 阅读 · 0 评论 -
HDU1874 畅通工程续(Floyd)
Problem这个畅通工程就不是最小生成树了,而是建好了路,怎么走的问题,最短路径算法。而且题目中说到会指明起点和终点,那么就是多源汇最短路,要用到Floyd算法。要注意的是可能存在自环和重边,初始化 i=j 的点初始为0,输入时做最小值判断。import java.io.BufferedReader;import java.io.IOException;import java.io....原创 2020-03-29 13:47:28 · 115 阅读 · 0 评论 -
HDU1010 Tempter of the Bone (dfs)
Problem这个题不可以用bfs!!!因为不一定是最短的路,一定要保证确定的时间点走到终点,是可能绕弯的。Javaimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;class Main { st...原创 2020-03-26 22:53:43 · 99 阅读 · 0 评论 -
HDU1879 继续畅通工程(kruskal)
Problem至少把kruskal练的很熟了已经。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;class Main { static BufferedReader br = new Buffer...原创 2020-03-26 22:51:10 · 122 阅读 · 0 评论 -
HDU1875 畅通工程再续(kruskal)
Problemimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;class Main { static BufferedReader br = new BufferedReader(new InputS...原创 2020-03-26 22:50:00 · 105 阅读 · 0 评论 -
HDU1863 畅通工程 (kruskal)
Problem我发现貌似有四个不同的畅通工程,我感觉只是细节问题,大同小异。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.math.BigDecimal;import java.util.Arrays;class Main {...原创 2020-03-26 22:49:00 · 141 阅读 · 0 评论 -
HDU1233 还是畅通工程 (kruskal)
Problem还是克鲁斯卡尔,挺顺利的。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;class Main { static BufferedReader br = new BufferedRea...原创 2020-03-24 21:30:48 · 164 阅读 · 0 评论 -
HDU1162 Eddy's picture(kruskal)
Problem同一个问题,只是这个题在二维坐标系中实现,需要手动计算欧几里得距离,并且这个题没有1-N的标号,需要自己设定,方法就是二重循环,n个点有n * (n - 1) >> 1条边。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;impor...原创 2020-03-24 18:36:15 · 133 阅读 · 0 评论 -
HDU3371 Connect the Cities (kruskal)
Problem练习图论中的最小生成树,全部用克鲁斯卡尔算法描述,因为比较简单。这个题是事先有一些模块已经连好了,事先处理好p数组就可以了,然后注意一些边界细节问题,毕竟HDU没有调试数据,只有Accepted or Wrong Answerimport java.io.BufferedReader;import java.io.IOException;import java.io.Inp...原创 2020-03-24 18:33:52 · 136 阅读 · 0 评论 -
HDU 1869 ( 六度分离 ) (Floyd)
ProblemFloyd算法,如果任何两个人的距离大于了7,六度理论即不成立,多源汇最短路径裸题。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.Scanner;clas...原创 2020-03-21 12:48:07 · 149 阅读 · 0 评论 -
HDU 2988 ( Dark roads ) (kruskal)
Problem最小生成树的裸题。克鲁斯卡尔算法描述import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;class Main { static BufferedReader br = new Buffe...原创 2020-03-21 12:46:32 · 167 阅读 · 0 评论 -
HDU 2544.最短路(bellman_ford)
Problem我发现HDU的题目水平是比较高的,但是有一个缺点是不便于调试,只给一个WA或者什么样的标记,而且有时候服务器也不稳定。前两天在复习图论最短路和最小生成树,这个题是最短路的裸题。这次用bellmanford算法解决。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStre...原创 2020-03-21 12:05:28 · 238 阅读 · 0 评论 -
AcWing 1233. 全球变暖(bfs)
Problem这个题依旧是一个bfs问题,做法是类似并查集那样,找到一个#就去搜索和这个#相连的所有#import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.ArrayDequ...原创 2020-02-29 16:02:59 · 228 阅读 · 0 评论 -
AcWing 1096. 地牢大师(三维bfs)
Problem类似这样的最值bfs问题已经记住了模板,就是用queue来解决的bfs和这一篇完全一样,就是扩展一下维度就可以了二维的情况import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import ...原创 2020-02-28 15:43:17 · 206 阅读 · 0 评论 -
AcWing 1224. 交换瓶子(图环 or 搜索)
Problem这个题可以用暴力来做,做法是从前往后遍历,因为保证1-N的数据,如果排好序那么所有的下标对应都是该数字(下标从1开始),那么遍历到哪位如不符,就往后找符合的那一位。第二个做法稍微优化了一下,所有互相交换的元素形成一个环,一次性完成一个环的交换下面Java描述暴力做法,Cpp描述图环做法#include <cstring>#include <iostream...原创 2020-02-28 12:42:18 · 201 阅读 · 0 评论 -
AcWing 1113. 红与黑(dfs)
Problemdfs,这个题没有要求一个最值,可以用dfs,模板题import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;class Main { static BufferedReader br = new BufferedReader(new Inpu...原创 2020-02-27 22:00:38 · 263 阅读 · 0 评论 -
AcWing 1101. 献给阿尔吉侬的花束(bfs)
标准的bfs,不过和花束一点关系都没有…Problembfs模板,再复习一遍import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.ArrayDeque;import ja...原创 2020-02-27 21:06:00 · 505 阅读 · 2 评论 -
AcWing 861.Maximun Matching of Bi-PartiteGraph (Bi-PartiteGraph) Described by Java
Last of the graph part.Problem in AcWingIt’s an algorithm like finding a girl friend, if one has a boy with her, try to let that boy to find another girl, if can, do it, else then, it’s her. This i...原创 2019-11-29 00:02:07 · 176 阅读 · 0 评论 -
AcWing 859. Kruskal-Minimum Spanning Tree (Kruskal) Described by Java
Kruskal is simpler than Prim, and easy to write.First step to sort the value by ascending orderAnd then check if the two points is not in one set, if it is, connect them, like union-find algorithm....原创 2019-11-27 05:13:02 · 117 阅读 · 0 评论 -
AcWing 858. Prim Minimum spanning tree(Prim) Described by Java
What is the minimun spanning tree?A minimum spanning tree (MST) or minimum weight spanning tree is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices ...原创 2019-11-25 03:47:56 · 100 阅读 · 0 评论 -
AcWing 854. Floyd Shortest Path (Floyd) Described by Java
What is Floyd?The Floyd algorithm, also known as the interpolation method, is an algorithm that uses the idea of dynamic programming to find the shortest path between multiple source points in a give...原创 2019-11-24 22:26:15 · 138 阅读 · 0 评论 -
AcWing 852. SPFA to judge negative loop(SPFA) Described by Java
Now use SPFA to judge if there has a negative loop in a graph.Add an array cnt[] to count the current point to index point , how many edges will be passed?And a new question is why the queue writte...原创 2019-11-24 19:08:22 · 91 阅读 · 0 评论 -
AcWing 853. Shortest path with edges limited (Bellman-ford) C++
Used to describe with Java.Go to Java#include <bits/stdc++.h>using namespace std;const int N = 5e2 + 10, M = 1e4 + 10;int dist[N], backup[N];bool st[N];int n, m, k;struct Edge { in...原创 2019-11-23 17:23:05 · 146 阅读 · 0 评论 -
AcWing 851. Shortest path solved by SPFA (SPFA) Described by Java
Last picture we saw such kind of ways to solve the shortest path.Now is SPFA, simple, save by queue, and more stable.Problem in AcWingThe st[] is to record if a point is in queue.import java.io.Bu...原创 2019-11-23 17:17:53 · 132 阅读 · 0 评论 -
AcWing 853. Shortest path with edges limited (Bellman-ford) Described by Java
This is a module of Bellman-ford, we can see the problem first.Problem Bellman-ford in AcWingThis is also a graph problem, only difference is it will has negative value, so we cannot use Dijkstra to...原创 2019-11-23 05:26:50 · 130 阅读 · 0 评论 -
AcWing 850. DijkstraⅡ(Dijkstra) Described by Java
This has more points, is sparse graph.So we use adjacency table to save.The pic can not upload successful…Problem in AcWingsame , turn the adjacency matrix to table.And using the PriorityQueue to...原创 2019-11-22 21:48:50 · 129 阅读 · 0 评论 -
AcWing 849. Dijkstra Ⅰ (Dijkstra algotithm) Described by Java
This is the simple Dijkstra module.AcWing in AcWingUsing more than 2 hours to debug , last other guys told me that u must use long to instead of integer… fuck me.import java.io.*;import java.util....原创 2019-11-22 06:22:35 · 156 阅读 · 0 评论 -
AcWing 848.Directly graph's topology sequence (BFS) Described by Java
Cause of this semester we will do the single chip microcomputer, It’s very very difficult, using C to program. I hope I will pass the Exam and Project! And maybe have not a long time to practice, but ...原创 2019-11-22 06:16:01 · 100 阅读 · 0 评论 -
AcWing 847.Level of points in the graph (bfs with tree) Described by Java
So this article is about bfs with tree. A module.Problem in AcWingLike the BFS, this problem is a naked problem.import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.Print...原创 2019-11-17 03:14:53 · 99 阅读 · 0 评论 -
AcWing 846.The center of gravity of the tree(dfs with tree) Described by Java
This module is about using bfs with tree, same principle.Problem in AcWingLike a list, each block we deal with each stack.Code :import java.io.BufferedReader;import java.io.IOException;import j...原创 2019-11-13 21:43:04 · 111 阅读 · 0 评论 -
AcWing AcWing 845. 8-grid shift(bfs) Described by Java
Fuck the problem debugging more than two hours , fuck u, u sick fuck.And finally I got the principle(module) of the fucking BFS. It fucked me a long time.The damn Problem in AcWingI can write the m...原创 2019-11-11 06:52:21 · 134 阅读 · 0 评论 -
AcWing 844. Go through the maze(bfs) Described by Java
Classic problem in AcWingBFS Module.And write the Queue in array and Pair<Integer, Integer> in Java.The Code :import java.io.BufferedReader;import java.io.IOException;import java.io.InputS...原创 2019-11-10 23:21:17 · 267 阅读 · 0 评论 -
AcWing 843. N-Queens(DFS) Described by Java
Classic problem-N-Queens Problem, using dfs to deal.Dfs always has a module, ago in Summer check-in training had trained it.How to maintain the hypotenuse:And the N-Queens Code :import java.io.Bu...原创 2019-11-10 18:06:43 · 102 阅读 · 0 评论 -
AcWing 842. Arrange numbers(DFS) Described by Java
Now I start the third chapter, searching and graph.The first module is dfs ,depth-first search.Here we will not talk too much about DFS and BFS theory.Classic Problem in AcWingAnd the DFS code :i...原创 2019-11-09 23:00:29 · 90 阅读 · 0 评论
分享