AcWing 345. 牛站 Cow Relays】的更多相关文章

由于我太菜了,不会矩阵乘法,所以给同样不会矩阵乘法同学的福利 首先发现这题点很多边很少,实际上有用的点 \(<= 2 * T\)(因为每条边会触及两个点嘛) 所以我们可以把点的范围缩到 \(2 * T\)来,然后... 算法1 Bellman - Ford O(NT) 什么,限制边数?那不就是可爱的 \(BellmanFord\)吗? 看看复杂度,嗯嗯 \(10 ^ 8\) 海星,常数超小的我肯定不用吸氧的 #pragma GCC optimize(2) #include <cstdio>…
题目描述 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…
问题描述 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…
题目描述 给出一张无向连通图,求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的二…
题意很简单,给一张图,把基本的求起点到终点最短路改成求经过k条边的最短路. 求最短路常用的算法是dijkstra,SPFA,还有floyd. 考虑floyd的过程: c[i][j]=min(c[i][j],a[i][k]+b[k][j]); 自然而然联想到矩阵乘法,每次加入一个点就相当于多加一条边,那么加k次就是k条边的最短路. 但是k可能很大(见数据范围),那么显然直接循环矩乘k次是行不通的,于是就想到了矩阵快速幂. 和普通快速幂一样的方式,只不过是把乘法替换成矩乘. 代码如下: #inclu…
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$中,然后…
题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) 条边的距离. 然后跑堆优化 \(Dijkstra\) 或者 SPFA 即可. 复杂度 \(O(N*nlog(n))\). 其中 \(N\) 代表要走的边条数, \(n\) 代表节点数 . 但是这题 \(N\) 有 \(10^6\) ... 洛谷上可以过掉 \(49\) 分. 不过只要 \(N\)…
最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <cstdlib> using namespace std; struct edge{ int u…
https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离散化大可不必sort+unique,因为我们甚至并不在乎相对大小,只是要一个唯一编号,可以开一个桶记录 之所以进行k-1次快速幂,是因为邻接矩阵本身就走了一步了. #include<iostream> #include<cstring> #include<cstdio> u…
传送门 矩阵快速幂,本质是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 {…
题目链接: https://www.luogu.org/problemnew/show/P2886 Update 6.16 最近看了下<算法导论>,惊奇地发现在在介绍\(APSP\) \((All Pairs Shortest Path)\)时的第一个方法就是此法,同时在思考题中看到了不少熟悉的问题,看来<算法导论>还是要多看一下 思路: 看到这题想了很久,想不到比较优的做法,然后看了书上的解法 感觉太妙了,将图论与矩阵加速递推结合了起来从而轻而易举地解决了这道题,实在是神奇. 首…
题面 解题思路 ## 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…
2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed) [P2886 USACO07NOV]Cow Relays G - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 给出一张无向连通图,求S到E经过k条边的最短路. 分析: 对于floyed,在第k个点时,任意的i到j之间的最短路已经经过了(k-1)个点.当fa[i] [j]经过了x条边,fb[i] [j]经过了y条边,想要算出经过了x+y条边,只需要按照floyed的算…
Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 Description 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…
Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7335   Accepted: 2878 Description 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…
P2877 [USACO07JAN]牛校Cow School 01分数规划是啥(转) 决策单调性分治,可以解决(不限于)一些你知道要用斜率优化却不会写的问题 怎么证明?可以暴力打表 我们用$ask(l,r,dl,dr)$表示处理区间$[l,r]$时,这段区间的决策点已固定在$[dl,dr]$中 设$mid=(l+r)/2$,暴力处理$mid$的最优决策点$dm$ 再向下分治$ask(l,mid-1,dl,dm)$,$ask(mid+1,r,dm,dr)$ 对于本题,先按$t[i]/p[i]$从大…
Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:9207   Accepted: 3604 Description 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 t…
P2883 [USACO07MAR]牛交通Cow Traffic 对于每一条边$(u,v)$ 设入度为0的点到$u$有$f[u]$种走法 点$n$到$v$(通过反向边)有$f2[v]$种走法 显然经过这条边的方案数为$f[u]*f2[v]$ 两边递推处理$f$数组,然后枚举每条边取个$max$. #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namesp…
康托展开 康托展开为全排列到一个自然数的映射, 空间压缩效率很高. 简单来说, 康托展开就是一个全排列在所有此序列全排列字典序中的第 \(k\) 大, 这个 \(k\) 即是次全排列的康托展开. 康托展开是这样计算的: 对于每一位, 累计除了前面部分, 字典序小于本位的排列总数, 即 LL cantor(){ LL ans = 0; for(LL i = 1;i <= num;i++){ LL cnt = 0; for(LL j = i + 1;j <= num;j++){ if(ask[j]…
P2419 [USACO08JAN]牛大赛Cow Contest Floyd不仅可以算最短路,还可以处理点之间的关系. 跑一遍Floyd,处理出每个点之间是否有直接或间接的关系. 如果某个点和其他$n-1$个点都有关系,那么它的排名就是可确定的. #include<iostream> #include<cstdio> #include<cstring> #define re register using namespace std; ][],ans; int main(…
P2990 [USACO10OPEN]牛跳房子Cow Hopscotch 题目描述 The cows have reverted to their childhood and are playing a game similar to human hopscotch. Their hopscotch game features a line of N (3 <= N <= 250,000) squares conveniently labeled 1..N that are chalked o…
Cow Relays Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 80     Accepted Submission(s): 13 Description Farmer John has formed a relay team for a race by choosing K (2 ≤ K ≤ 40) of his cows.…
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…
P2419 [USACO08JAN]牛大赛Cow Contest 题目背景 [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 t…
P2883 [USACO07MAR]牛交通Cow Traffic 随着牛的数量增加,农场的道路的拥挤现象十分严重,特别是在每天晚上的挤奶时间.为了解决这个问题,FJ决定研究这个问题,以能找到导致拥堵现象的瓶颈所在. 牧场共有M条单向道路,每条道路连接着两个不同的交叉路口,为了方便研究,FJ将这些交叉路口编号为1..N,而牛圈位于交叉路口N.任意一条单向道路的方向一定是是从编号低的路口到编号高的路口,因此农场中不会有环型路径.同时,可能存在某两个交叉路口不止一条单向道路径连接的情况. 在挤奶时间到…
Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Description 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…
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…
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…
[USACO12FEB]牛券Cow Coupons(堆,贪心) 题目描述 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 of M units of money (1 <= M <= 10^14). Cow i costs P_i money (1 <= P_i <= 10^9), b…