【CF 702E】Analysis of Pathes in Functional Graph(倍增)
来源:互联网 发布:人工智能需要哪些技术 编辑:程序博客网 时间:2024/06/07 03:39
【CF 702E】Analysis of Pathes in Functional Graph(倍增)
题目大意:
n个点n条边的有向图,每个点有且只有一个后继。图中一定会有环。
问从每个点出发,走k条边,总花费,还有这段路程里的最小边权。
每个边每个点可以走多次,从每个点出发走k步为止。
k很大,第一反应是找环,找出所有环,然后想办法统计每个点到环的路长,然后……麻烦到吐血= =还搞不出来……
virtual后百度了一发,一看到倍增……厉害啊!!!大脑一阵嗡嗡声,太强了!
类似rmq的思想,一维表示区间长度,然后转移就行了。查询的时候把k拆成几个2的整数次幂,就完了。强强强强强!太妙了。
代码如下:
#include <iostream>#include <cmath>#include <vector>#include <cstdlib>#include <cstdio>#include <climits>#include <ctime>#include <cstring>#include <queue>#include <stack>#include <list>#include <algorithm>#include <map>#include <set>#define LL long long#define Pr pair<int,int>#define fread(ch) freopen(ch,"r",stdin)#define fwrite(ch) freopen(ch,"w",stdout)using namespace std;const int INF = 0x3f3f3f3f;const int mod = 1e9+7;const double eps = 1e-8;const int maxn = 112345;struct Edge{ int v,mn; LL sum;} rmq[40][maxn];LL k;int n;void init(){ for(int j = 1; (1LL<<j) <= k; ++j) for(int i = 0; i < n; ++i) { rmq[j][i].sum = rmq[j-1][i].sum+rmq[j-1][rmq[j-1][i].v].sum; rmq[j][i].mn = min(rmq[j-1][i].mn,rmq[j-1][rmq[j-1][i].v].mn); rmq[j][i].v = rmq[j-1][rmq[j-1][i].v].v; //printf("pow:%lld sum:%lld mn:%d en:%d\n",1LL<<j,rmq[j][i].sum,rmq[j][i].mn,rmq[j][i].v); }}void query(int id){ LL tmp = k; LL sum = 0; int mn = 100000000; for(int j = 0; (1LL<<j) <= tmp; ++j) if((1LL<<j)&tmp) { tmp -= (1LL<<j); sum += rmq[j][id].sum; mn = min(mn,rmq[j][id].mn); id = rmq[j][id].v; } printf("%lld %d\n",sum,mn);}int main(){ //fread(""); //fwrite(""); int u,v,w; scanf("%d%lld",&n,&k); for(int i = 0; i < n; ++i) scanf("%d",&rmq[0][i].v); for(int i = 0; i < n; ++i) { scanf("%d",&rmq[0][i].mn); rmq[0][i].sum = rmq[0][i].mn; } init(); for(int i = 0; i < n; ++i) query(i); return 0;}
0 0
- 【CF 702E】Analysis of Pathes in Functional Graph(倍增)
- CodeForces 702E Analysis of Pathes in Functional Graph(倍增)
- [CF 702E]Analysis of Pathes in Functional Graph
- Codeforces 702E Analysis of Pathes in Functional Graph(倍增)
- Educational Codeforces Round 15 E. Analysis of Pathes in Functional Graph (倍增RMQ)
- Educational Codeforces Round 15 E Analysis of Pathes in Functional Graph(倍增)
- Codeforces 702E - Analysis of Pathes in Functional Graph 二进制
- Codeforces702E Analysis of Pathes in Functional Graph(倍增)
- Educational Codeforces Round 15 E. Analysis of Pathes in Functional Graph (倍增法)
- CF702E - Analysis of Pathes in Functional Graph
- Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
- Educational Codeforces Round 15 Analysis of Pathes in Functional Graph rmq 变形
- Educational Codeforces Round 15 E. Analysis of Pathes in Functio(倍增)★ ★
- Educational Codeforces Round 15 E. Analysis of Pathes in Functio(每个点走k步会到达哪个点,经过的边中的最小值是多少,经过的边的权值和)
- 图论(PolandBall and White-Red graph,cf 755E)
- CF 261 E. Pashmak and Graph
- Graph Cutting CF 405E DFS
- CF Clique in the Divisibility Graph (DP)
- codeforces 724B Batch Sort
- Java基础复习---字符串
- imread函数、namedWindow函数、imshow函数、imwrite函数
- c小知识
- MyEclipse 2016 CI 1 破解
- 【CF 702E】Analysis of Pathes in Functional Graph(倍增)
- 全栈开发、全能型程序猿养成系列(一)序言
- poj2492A Bug's Life
- 关于java字符串反转的实现
- android apk版本更新
- struts框架入门的第一小程序
- HttpClientUtil
- c++的POD
- 单链表的逆置-C++实现(ok)