题目链接

给一个图, 然后给出每条边的权值和一个k值。 让你求出从每个点出发, 走k次能获得的边权的和以及边权的最小值。

用倍增的思想, 求出每个点走一次能到达的点, 权值和以及最小值, 走两次..四次..八次。 这个很容易计算。然后枚举一下所有点就可以了。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int nxt[][], mn[][];
ll sum[][];
int main()
{
int n;
ll k;
cin>>n>>k;
for(int i = ; i < n; i++) {
scanf("%d", &nxt[i][]);
}
for(int i = ; i < n; i++) {
scanf("%d", &mn[i][]);
sum[i][] = mn[i][];
}
for(int i = ; i < ; i++) {
for(int j = ; j < n; j++) {
nxt[j][i] = nxt[nxt[j][i-]][i-];
mn[j][i] = min(mn[j][i-], mn[nxt[j][i-]][i-]);
sum[j][i] = sum[j][i-] + sum[nxt[j][i-]][i-];
}
}
for(int j = ; j < n; j++) {
ll ansSum = , tmpk = k;
int ansMin = inf, pos = j;
for(int i = ; i >= ; i--) {
ll tmp = 1LL<<i;
if(tmpk >= tmp) {
tmpk -= tmp;
ansSum += sum[pos][i];
ansMin = min(ansMin, mn[pos][i]);
pos = nxt[pos][i];
}
}
printf("%I64d %d\n", ansSum, ansMin);
}
return ;
}

codeforces 702E Analysis of Pathes in Functional Graph 倍增的更多相关文章

  1. CodeForces 702E Analysis of Pathes in Functional Graph

    倍增预处理. 先看一下这张图的结构,因为出度都是$1$,所以路径是唯一的,又因为每个点都有出度,所以必然有环,也就是一直可以走下去. 接下来我们需要记录一些值便于询问: 设$t[i][j]$表示从$i ...

  2. codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制

    http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...

  3. Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph

    E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...

  4. CF702E Analysis of Pathes in Functional Graph

    倍增练习题. 基环树上倍增一下维护维护最小值和权值和,注意循环的时候$j$这维作为状态要放在外层循环,平时在树上做的时候一个一个结点处理并不会错,因为之前访问的结点已经全部处理过了. 时间复杂度$O( ...

  5. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  6. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  7. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

  8. Codeforces 739D - Recover a functional graph(二分图匹配)

    Codeforces 题面传送门 & 洛谷题面传送门 首先假设我们已经填好了所有问号处的值怎样判断是否存在一个合法的构造方案,显然对于一种方案能够构造出合法的基环内向森林当且仅当: \(\fo ...

  9. Codeforces 841D Leha and another game about graph - 差分

    Leha plays a computer game, where is on each level is given a connected graph with n vertices and m  ...

随机推荐

  1. Android Bitmap圆角

    代码如下: public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeig ...

  2. java命令行运行带外部jar

    假设:java 代码路径为com.jdw.test,其中调用了外部jar包 则需要将jar包解压后,放入com同级目录 然后再com目录启动命令行 java com.jdw.test.HelloWor ...

  3. 混合使用Azure LB和ILB访问相同web服务(2)

    那么现在开始,我们配置下两台WEB服务器的Internal Load  balancer: 打开Powershell,检查当前两台虚拟机的端点配置: Get-AzureVM  -ServiceName ...

  4. JQuery 的基本命令

    调用jquery的格式 <script>   $(document).ready(function() {     }); </script>   笔记 1.现在让我们开始写第 ...

  5. 一个失误导致微信下载图片接口Token失效

    公司的应用调了一个微信上传下载图片的接口,本来在线上跑的好好的,什么问题没有,但是这两天总是不定时的出现下载下来的图片损坏,拿着Token和serverid去接口测试网页验证,返回的是Token失效了 ...

  6. 【额 原来ms sqlserver 中的视图果然是“虚表”哈】

    在视图中查询数据的时候,会不会使用实体表中的列上的索引呢?会 .... 测试结果 测试脚本 ; BEGIN INSERT INTO Teachers ( TeacherName, Sex, Money ...

  7. grok 正则解析日志例子<1>

    <pre name="code" class="html">下面是日志的样子 55.3.244.1 GET /index.html 15824 0. ...

  8. haproxy hdr和path

    path : string This extracts the request's URL path, which starts at the first slash and ends before ...

  9. iis 回收工作进程时出错的解决办法

    第一种解决方案: iis6系统默认的工作进程回收时间是29个小时有很多问题是在回收工作进程后出现很多问题如典型的500错误等经过我做服务器的一段时间的观察大家可以不用回收工作进程而是把应用程序池的最大 ...

  10. 《windows程序设计》学习_1:初识windows程序

    #include<windows.h> int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szC ...