
0x60 图论
图的表示法(邻接矩阵、邻接表、链式向前星)、最短路算法(dijkstra、Floyd、spfa)、最小生成树算法(prim、kruskal)、拓扑排序(Kahn、dfs)
常欢愉皆胜意且顺遂
记得一定要努力,不然往后余生,做饭是你,洗衣是你,做家务是你……是你是你还是你
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
洛谷题单 208【图论2-2】最短路
文章目录P3371 【模板】单源最短路径(弱化版)【模板】单源最短路径(标准版)P3371 【模板】单源最短路径(弱化版)数据最大为1e4,所以需要用到堆优化和邻接表//邻接表写法#include <bits/stdc++.h>#define PUSH(x,y,z) G[x].push_back(P(y,z)) // 宏函数using namespace std;const int INF = 0x3f3f3f3f, N = 10005;typedef pair<int原创 2020-09-29 11:50:25 · 377 阅读 · 0 评论 -
G - MPI Maelstrom
BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to benchmark the new sy原创 2020-09-23 10:15:56 · 221 阅读 · 0 评论 -
E - Currency Exchange
Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of curr原创 2020-09-22 11:06:49 · 289 阅读 · 0 评论 -
hdu1874:畅通工程续(单源最短路模板)
作者:血小板自动机链接:https://blog.dalao.icu/archives/8.html题目传送门hdu1874:畅通工程续解题思路这题因为数据量比较小,可以使用多种最短路算法来解决,是一道经典的模板题,下面附上 floyd 算法、dijkstra 算法、Bellman-Ford 、spfa 算法、以及 dijkstra + heap 优化的代码。坑点:这题可能一个城市到另一个城市有多条路径,我们记录的时候,要记录最小的那条路径,不能记录最后的那条路径,解其他题目的时转载 2020-06-24 12:36:25 · 363 阅读 · 0 评论 -
H - Cow Contest
N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is uni...原创 2019-05-02 15:04:45 · 324 阅读 · 0 评论 -
D - Silver Cow Party
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get b...原创 2019-05-01 12:29:52 · 176 阅读 · 0 评论 -
C - Heavy Transportation
BackgroundHugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his cust...原创 2019-04-30 21:46:00 · 631 阅读 · 0 评论 -
B - Frogger
DescriptionFreddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of t...原创 2019-04-23 13:31:34 · 289 阅读 · 17 评论 -
A - Til the Cows Come Home
DescriptionBessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wa...原创 2019-04-19 15:15:46 · 133 阅读 · 0 评论 -
Dijkstra算法图文详解(转)
本文转载自:https://blog.youkuaiyun.com/lbperfect123/article/details/84281300Dijkstra算法Dijkstra算法算是贪心思想实现的,首先把起点到所有点的距离存下来找个最短的,然后松弛一次再找出最短的,所谓的松弛操作就是,遍历一遍看通过刚刚找到的距离最短的点作为中转站会不会更近,如果更近了就更新距离,这样把所有的点找遍之后就存下了起点到其...转载 2019-12-08 20:56:15 · 6631 阅读 · 0 评论 -
Floyd-傻子也能看懂的弗洛伊德算法(转)
暑假,小哼准备去一些城市旅游。有些城市之间有公路,有些城市之间则没有,如下图。为了节省经费以及方便计划旅程,小哼希望在出发之前知道任意两个城市之前的最短路程。上图中有4个城市8条公路,公路上的数字表示这条公路的长短。请注意这些公路是单向的。我们现在需要求任意两个城市之间的最短路程,也就是求任意两个点之间的最短路径。这个问题这也被称为“多源最短路径”问题。现在需要一个数据结构来存储图的信息,...转载 2019-03-21 20:20:09 · 618 阅读 · 0 评论 -
T83722 女王搬水泥(最小生成树)
题目背景“传说有个魔仙堡,有个女王不得了”,但是再不得了的女王也有为难的时候。在某年某月某日某魔仙堡发生了地震???是的没错,是地震。地震的到来是女王sama忽然生起来了体验普通人类生活的心思(????),又是她决定以身作则,带领魔仙堡的人民搬水泥(划重点!!!),我们身娇体柔的女王sama于是就操作了起来,但是由于政务繁忙,她时间又不是很多,于是懒惰的她想知道最快什么时候可以跑路。题目描述...原创 2019-08-12 00:17:57 · 149 阅读 · 0 评论 -
kuangbin带你飞 最小生成树专题
InputThe input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n &l...原创 2019-08-04 22:17:10 · 380 阅读 · 0 评论 -
洛谷P3366 最小生成树模板
题目描述如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz输入输出格式输入格式:第一行包含两个整数N、M,表示该图共有N个结点和M条无向边。(N<=5000,M<=200000)接下来M行每行包含三个整数Xi、Yi、Zi,表示有一条长度为Zi的无向边连接结点Xi、Yi输出格式:输出包含一个数,即最小生成树的各边的长度之和;如果该图不连通则输出orz输入输...原创 2019-12-09 19:03:33 · 664 阅读 · 0 评论 -
D - Constructing Roads(最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if t...原创 2019-08-09 15:00:15 · 218 阅读 · 0 评论 -
N - 畅通工程再续(最小生成树)
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小...原创 2019-08-08 21:16:17 · 190 阅读 · 0 评论 -
最小生成树算法简介及代码模板
prim算法每次取未使用的、距离最小生成树最近的点1、vis数组:存储每个已经加入最小生成树的点,dis数组:表示最小生成树到剩下的所有点的最小距离。2、先在图中找到第一个点v1,放入最小生成树。更新数组dis,为v1到剩余所有点的距离(dis[i] = ma[1][i])。3、再从剩余的点中找到距离最小生成树最近的点v’,将其放入最小生成树,更新数组vis,并更新dis的值(dis[i] = min(dis[i], ma[i][v’]))。4、以此类推,直到最后所有的点均放入最下生成树,此时就求原创 2020-07-16 11:13:45 · 473 阅读 · 0 评论 -
图的存储(邻接矩阵、邻接表和链式向前星)
图的表示邻接矩阵邻接矩阵使用n×nn \times nn×n的二维矩阵来表示图。用g[i][j]g[i][j]g[i][j]来表示点iii和点jjj的关系,可以是0、10、10、1来表示两点是否可达,也可以是数字来表示两点间的距离。邻接表邻接表是表示点iii可到达的点,可以用动态数组来进行存储。如果要存储带权图,可以将边用结构体来表示。#include <cstdio>#inclu...原创 2020-02-01 23:58:49 · 578 阅读 · 0 评论 -
拓扑排序算法(kahn、dfs)
#include <cstdio>#include <cstring>#include <queue> using namespace std;const int N = 2005;int t, n, m;int in[N]; //每个节点的度 queue<int> q;vector<int> edge[N], ans;...原创 2020-01-12 21:25:31 · 412 阅读 · 0 评论 -
欧拉回路
欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?Input测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结束。Output每个...原创 2020-03-14 15:50:33 · 245 阅读 · 0 评论 -
迷宫城堡(tarjan算法)
为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间。Gardon需要请你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i到房间j,...原创 2020-03-14 15:22:02 · 375 阅读 · 0 评论