In the country there are exactly n cities numbered with positive integers from 1 to n. In each city there is an airport is located. Also, there is the only one airline, which makes m flights. Unfortunately, to use them, you need to be a regular custo…
题面传送门 题意: 有一张 \(n\) 个点 \(m\) 条边的有向图,你初始在 \(1\) 号点,边上有边权 \(c_i\) 表示只有当你经过至少 \(c_i\) 条边的时候你才能经过第 \(i\) 条边. 求从 \(1\) 号点开始最少走过多少条边才能到达 \(n\) 号点. \(n,m \leq 150,c_i\leq 10^9\) 注意到题目中 \(c_i\) 的数据范围可以达到 \(10^9\),我们显然不能一步步枚举可达的位置. 但是 \(m\) 的数据范围很小,说明转移矩阵最多改变…
题目链接  Flights for Regular Customers 首先按照$d$的大小升序排序 然后分成$m$个时刻,每条路径一次处理过来. $can[i][j]$表示当前时刻$i$能否走到$j$ $can$通过上一条路径后的$can$和当前的可行路径矩阵的$d$次幂得到. 这由$floyd$求解即可.考虑到$d$很大,用矩阵快速幂加速. TLE on test 10 矩阵乘法的时候用$bitset$优化. 更新答案的时候,我们枚举每个点. 若第$1$个点可以走到第$i$个点,则更新答案.…
题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$d_i$由小到大排序 设$f_i$表示加上$1\sim i-1$的所有边走$d_i$次后各点间的联通情况 $G$表示只连$1\sim i-1$的边的邻接矩阵 这些我们可以用一个$01$邻接矩阵来存储 则有 $f_i=f_{i-1}*G^{d_i-d_{i-1}}$ 这很明显是一个矩阵快速幂的过程 之…
这破题调了我一天...错了一大堆细节T T 首先显然可以将边权先排序,然后逐个加进图中. 加进图后,倍增跑跑看能不能到达n,不能的话加新的边继续跑. 倍增的时候要预处理出h[i]表示转移矩阵的2^0~i的或和,转移是h[i]=h[i-1]*h[i-1]. 注意两个矩阵包含0~i和0~j相乘的时候,得到的矩阵是0~i*j的,而两个矩阵包含0~i和0~j或起来的时候,得到的矩阵是j~i+j的. 倍增的时候因为必须答案单调,所以当前的值必须或上之前的值. #include<iostream> #in…
题目链接 http://codeforces.com/contest/576/problem/D 题解 把边按\(t_i\)从小到大排序后枚举\(i\), 求出按前\((i-1)\)条边走\(t_i\)步能到达的点的集合,以它们为起点求\(n\)号点的最短路. 前者等于前\((i-2)\)条边走\(t_{i-1}\)步能到达的点集乘上前\((i-1)\)条边邻接矩阵的\((t_i-t_{i-1})\)次幂. 因为只关心是否存在,故可以使用bitset优化. 时间复杂度\(O(mn^3+\frac…
分析 https://www.cnblogs.com/onioncyc/p/8037056.html 写的好像有点问题 但是大致就是这个意思 代码很好理解 代码 #include<bits/stdc++.h> using namespace std; #define bt bitset<160> const int inf = 0x3f3f3f3f; ][]; bt ans[],a[],c[]; struct node { int x,y,z; }; node d[]; inlin…
%%%cxhscst2's blog Codeforces 576D Flights for Regular Customers(矩阵加速DP) 代码非常优美 + 简洁,学习到了 Code: #include <bits/stdc++.h> #define N 160 #define inf 0x3f3f3f3f #define maxn 1000000 #define setIO(s) freopen(s".in","r",stdin) using n…
[题目]D. Flights for Regular Customers [题意]给定n个点m条边的有向图,每条边有di表示在经过该边前必须先经过di条边,边可重复经过,求1到n的最小经过边数.n,m<=150,di<=10^9,time=4s. [算法]floyd+矩阵快速幂 [题解]需要计算步数,很容易联想到将floyd中每一步拆成矩阵乘法的经典做法. 令a[d][i][j]表示恰好d步能否从 i 走到 j(邻接矩阵) ,令b[d][i][j]表示当前已走d步时允许通过的边(连边矩阵).…
「CF576D」 Flights for Regular Customers 对不起我又想网络流去了 你看这长得多像啊,走过至少多少条边就是流量下界,然后没上界 但是这个题求的最少走多少条边啊...完全不一样好吧... 然后又开始想最短路相关算法,然后觉得分层图可以直接跑,然后发现 \(d_i\le 10^9\),直接爆炸. 然后就不会了. 注意到恰好走过 \(k\) 条边的最短路是可以通过 \(\texttt{Floyd}\) 求得的.那如果我走 \(k\) 条边能够到达某个点,那么我从这个点…
CF - 392 C. Yet Another Number Sequence 题目传送门 这个题看了十几分钟直接看题解了,然后恍然大悟,发现纸笔难于描述于是乎用Tex把初始矩阵以及转移矩阵都敲了出来 \(n\le 1e17\) 这个数量级求前缀和,发现递推关系之后矩阵快速幂是可以求出来的,所以就尝试把\(A_i(k)\) 的递推式求出来. \[A_{i-1}(k) = F_{i-1} * (i-1) ^ k\\ A_{i-2}(k) = F_{i-2} * (i-2) ^ k \] \[\be…
n<=150个点,m<=150条路,每条路Ai,Bi,Di表示Ai到Bi有一条有向边,使用他前至少要走Di条路,问1到n最少走几条路. 又是n^4过150的题.... 不同于传统的最短路,这次的最短路包括了m个图,并且状态和走的路径数有关.所以要一个状态Can(i,j)表示能否到达点i走j步. 由于有m个图,我们就一个一个图来看.把边从小到大加入图,每加入某个值的一组边即可构成一个新图.在进入一个新图之前,我需要知道:在上个图的最后一步,从1走能走到哪些点,已这些点为起点进行下一步的探索.新来…
对每条边来说,可以走这条边的限制解除是按\(d\)的顺序,所以先对每条边按\(d\)排序. 然后考虑每两条边之间的处理,用一个矩阵表示当前走\(d\)步是否可以从一个点到另一个点,称其为状态矩阵,用另一个矩阵表示当前解除了限制的边,称其为边矩阵. 每次新加入一条边时,让状态矩阵乘上当前边矩阵的\(d_i-d_{i-1}\)次方,即可更新走当前步数\(d\)步点与点之间到达的状态,这一过程可以用矩阵快速幂和\(bitset\)进行优化. 然后用\(floyd\)处理出以当前解除限制的边的最短路,若…
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo 1000000007 (109 + 7). Input The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single i…
http://codeforces.com/problemset/problem/241/E 首先检测哪些点会出现在从起点到终点的路上,可以用dfs或者迭代, 然后,对于所有的边,设f为边起点,t为边终点,dp[i]为从起点出发到i点所必须花费的时间,则当dp[t]>dp[f]+2,也就是超出限制时,把dp[t]限制到dp[f]+2处,对于dp[f]>dp[t]+1,限制dp[f]到dp[t]+1处 因为这个图没有圈,所以如果存在满足题意的边权方案,那么每次使得一个点的dp值满足要求,n次之后…
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has nmandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the com…
The mobile application store has a new game called "Subway Roller". The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n co…
Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to…
Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible attack by hackers on a major computer network. In this network are n vertices, some pairs of vertices are connected by m undirected channels. It is pla…
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The fee…
题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/stdc++.h> using namespace std; ; char s[maxn]; int vis[maxn], Stack[maxn], top; int main(){ scanf(); ); ;i<=n;i++){ if(s[i]=='('){ Stack[++top]=i; } els…
Regular Bracket Sequence time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output A string is called bracket sequence if it does not contain any characters other than "(" and ")". A b…
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi. As Jaehyun's house is not large enough to have a bookshelf, he k…
New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads…
一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) 所以一个城市作为首都,可能会有若干边需要改变方向 现在问,选择哪些城市作为首都,需要改变方向的边最少. 输出最少需要改变方向的边数 输出可以作为首都的编号 树形DP 先假定城市1作为首都 令tree(i)表示以i为根的子树 dp[i]表示在tree(i)中,若以i为首都的话,需要改变的边数 第一次…
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space are marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the midd…
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t…
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept…
1.http://codeforces.com/problemset/problem/149/D 2.题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-1这一区间内有多少种上色方案,很明显的区间DP dp[l][r][i][j]表示l-r区间两端颜色分别是i,j的方案数 0代表不上色,1代表上红色,2代表上蓝色 对于l-r区间,有3种情况 1.if(…
Three displays time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent t…