题意很简单,给一张图,把基本的求起点到终点最短路改成求经过k条边的最短路. 求最短路常用的算法是dijkstra,SPFA,还有floyd. 考虑floyd的过程: c[i][j]=min(c[i][j],a[i][k]+b[k][j]); 自然而然联想到矩阵乘法,每次加入一个点就相当于多加一条边,那么加k次就是k条边的最短路. 但是k可能很大(见数据范围),那么显然直接循环矩乘k次是行不通的,于是就想到了矩阵快速幂. 和普通快速幂一样的方式,只不过是把乘法替换成矩乘. 代码如下: #inclu…
题面 解题思路 ## floyd+矩阵快速幂,跟GhostCai爷打赌用不用离散化,最后完败..GhostCai真是tql ! 有个巧妙的方法就是将节点重新编号,因为与节点无关. 代码 #include<bits/stdc++.h> using namespace std; const int MAXN = 1005; int n,t,s,e; int edge[MAXN][MAXN]; int num[MAXN],tot; struct Mat{ int a[105][105]; Mat o…
题目描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture. Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each…
https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离散化大可不必sort+unique,因为我们甚至并不在乎相对大小,只是要一个唯一编号,可以开一个桶记录 之所以进行k-1次快速幂,是因为邻接矩阵本身就走了一步了. #include<iostream> #include<cstring> #include<cstdio> u…
题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) 条边的距离. 然后跑堆优化 \(Dijkstra\) 或者 SPFA 即可. 复杂度 \(O(N*nlog(n))\). 其中 \(N\) 代表要走的边条数, \(n\) 代表节点数 . 但是这题 \(N\) 有 \(10^6\) ... 洛谷上可以过掉 \(49\) 分. 不过只要 \(N\)…
题目链接: https://www.luogu.org/problemnew/show/P2886 Update 6.16 最近看了下<算法导论>,惊奇地发现在在介绍\(APSP\) \((All Pairs Shortest Path)\)时的第一个方法就是此法,同时在思考题中看到了不少熟悉的问题,看来<算法导论>还是要多看一下 思路: 看到这题想了很久,想不到比较优的做法,然后看了书上的解法 感觉太妙了,将图论与矩阵加速递推结合了起来从而轻而易举地解决了这道题,实在是神奇. 首…
题目描述 给出一张无向连通图,求S到E经过k条边的最短路. 输入输出样例 输入样例#1: 2 6 6 4 11 4 6 4 4 8 8 4 9 6 6 8 2 6 9 3 8 9 输出样例#1: 10题解:法1:dp+floyd+倍增f[i][j][p]为从i到j经过2^p条边显然f[i][j][p]=min(f[i][k][p-1]+f[k][j][p-1])如果n不是2的幂也没事,将n进行二进制分解,再用dp转移ans[x][i]=min(ans[!x][j]+f[i][j][p]) n的二…
BZOJ 1706权限题. 倍增$floyd$. 首先这道题有用的点最多只有$200$个,先离散化. 设$f_{p, i, j}$表示经过$2^p$条边从$i$到$j$的最短路,那么有转移$f_{p, i, j} = min(f_{p - 1, i, k} + f_{p - 1, k, j})$. 然后做一个类似于快速幂的东西把$n$二进制拆分然后把当前的$f$代进去转移. 可以设一个$g_{i, j}$表示当前从$i$到$j$的最短路,为了保证转移顺序的正确,可以把$g$抄出来到$h$中,然后…
传送门 矩阵快速幂,本质是floyd 把 * 改成 + 即可 注意初始化 因为只有100条边,所以可以离散化 #include <cstdio> #include <cstring> #include <algorithm> #define N 101 #define min(x, y) ((x) < (y) ? (x) : (y)) int n, t, s, e, cnt; int x[N], y[N], z[N], a[N]; struct Matrix {…
P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget…
P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a line. The line begins with no cows and then, as time progresses, one by one, the cows join the line on the left or right side. Every once in a while, s…
P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Far…
P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at…
问题描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture. Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each…
最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <cstdlib> using namespace std; struct edge{ int u…
本题就是求两点间只经过n条边的最短路径,定义广义的矩阵乘法,就是把普通的矩阵乘法从求和改成了取最小值,把内部相乘改成了相加. 代码包含三个内容:广义矩阵乘法,矩阵快速幂,离散化: 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int INF=0x3f3f3f3f; 4 const int N=120; 5 int Hash[1000005],cnt=0;//用于离散化 6 struct matrix{ 7 int m[N][N…
如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里:  简陋的博客    百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行(牛线),问FJ此时的行号是多少.之后,FJ会给牛一个行号,牛必须按照新行号排列成线. 行号是通过以字典序对行的所有排列进行编号来分配的.比如说:FJ有5头牛,让他们排为行号3,排列顺序为: 1:1 2 3 4 5 2:1 2 3 5 4 3:1 2 4 3 5 因此,牛将在牛线1 2 4 3 5中. 之后,…
题目背景 [Usaco2008 Jan] 题目描述 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 unique among the competito…
https://www.luogu.org/problem/show?pid=2419 题目背景 [Usaco2008 Jan] 题目描述 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 ski…
想找原题请点击这里:传送门 原题: 题目背景 [Usaco2008 Jan] 题目描述 N ( ≤ N ≤ ) cows, conveniently numbered ..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 unique among…
题目描述 Bessie has taken heed of the evils of sloth and has decided to get fit by jogging from the barn to the pond several times a week. She doesn't want to work too hard, of course, so she only plans to jog downhill to the pond and then amble back to…
传送门 题目大意:n头牛在单行道n个位置,开始用不同的速度跑步. 当后面的牛追上前面的牛,后面的牛会和前面的牛以一样的速度 跑,称为一个小团体.问:ts后有多少个小团体. 题解:模拟 倒着扫一遍,因为某头牛后面的牛对这头牛的速度没影响. 计算出ts后牛的终点,如果能撞上前面最慢的牛,那么小团体数+1 注意开long long 一开始不理解为什么倒着扫, 因为如果正着扫,看第i头牛能否撞上i+1头, 我们不确定第i+1头牛的速度,可能第i+1头牛 速度很快,追上i+2头牛速度减缓,从而被第i头牛追…
本蒟蒻又来发题解了, 一道较水的模拟题. 题意不过多解释, 思路如下: 在最开始的时候求出每头牛在t秒的位置(最终位置 然后,如果后一头牛追上了前一头牛,那就无视它, 把它们看成一个整体. else 就++ ans: 上代码: #include<bits/stdc++.h> using namespace std; //要开long long long long n, t, ans = 1, last[100010]; struct node { long long s, p; }a[1000…
对于一个能够确定名次的点,可以注意到,对于该点,入度和出度的数量加起来等于N-1(这样还是不够准确的确切的说是,能够到达这个点的数量和这个点能够到达的数量的和 floyd不仅可以求两个点之间的最短路径,还能求两个点彼此是否能够相互到达最后对于一个可以确定名次的点,能够到达的所有的点 加上 能够到达该点的所有点的和必须等于n-1当然,我们可以通过二进制来简化这个过程 最后处理结果时,设一个变量flag, 因为该点能够到达本身,flag初值赋为1对于两个点i, j首先f[i][j] | f[j][i…
Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn = 1000000 + 233; const long long N = 1000000000000000000; long long pos[maxn], speed[maxn], T , dest[maxn]; int A[maxn], n; int cmp(int i,in…
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory. Game i starts with an integer N_i (1 <= N_i <= 1,000,000). Bessie goes first, and then the two players…
题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, he is a bit superstitious, and only labels cows with binary numbers that have exactly K "1" bits (1 <= K <= 10). The leading bit of each la…
题意就是给一张无向有边权的图.起点.终点,求起点到终点经过n条边的最短路.n<=10^6,点的编号<=10^3,边数<=10^2. 这个边数让人不由自主地想到了floyd,然后发现floyd每次相当于加入了一个点(注意,这里的“一次”也是O(点数^3)的,但是在这一次floyd的过程中不会更新结果.)也就是说,第一次floyd求出来了两点之间只走一条边的最短路,第二次求出来了两点之间只走两条边的最短路……,第n次求出来了只走n条边的最短路.这时候就会发现,n遍不在过程中更新答案的floy…
传送门啦 倍增 $ Floyd $ 注意结构体里二维数组不能开到 $ 2000 $ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define Re register using namespace std; inline int read(){ char ch = getchar(); int f = 1 , x = 0; while(ch &g…
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N (1 ≤ N ≤ 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 ≤ M ≤ 10,000) one-way paths (no path connects a pasture to…