题目链接

给一个图, 然后给出每条边的权值和一个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. python核心编程-第三章-个人笔记

    1.语句和语法 (1)反斜杠"\"表示语句继续.python良好的编程习惯是一行最后不超过80个字符,一行字符过多时便须用到反斜杠换行继续该语句. PS:在使用小括号.中括号.大括 ...

  2. 1003 Crashing Balloon

    考察DFS的应用,判断两个数的因子. #include <stdio.h> int f1,f2; void DFS(int m,int n,int k){ ){ f2=; ) f1=; } ...

  3. UVA 12657 Boxes in a Line

    双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...

  4. nginx 日志格式

    log_format main '$http_host $server_addr $remote_addr [$time_local] "$request" ' '$request ...

  5. hdu 1050 Moving Tables_贪心

    题意:你搬n个桌子,桌子从一个地方搬到另一个地方,走廊只允许同时一个桌子通过,教室分布在两边,奇数在一边,偶数在一边,当桌子不冲突时可以同时搬运,冲突时要等别的那个桌子搬完再搬. 思路:因为奇数桌子在 ...

  6. leetcode Permutation

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  7. 升级Xcode7后所要更改的配置

    http://blog.csdn.net/huxiaoqiao163/article/details/48711077

  8. #include <amp.h>

    parallel_for_each(av.extent, [=](concurrency::index<1>idx)restrict(amp) {av[idx] += 1; }); //[ ...

  9. ping网络故障

    网络的应用已渐渐深入我们的工作和生活,它带给了我们各方面的便利.因此,这种种的便利致使很多人对网络产生依赖性.那么,当电脑不能上网时,我们如何才能准确地判断电脑问题出在哪里?又如何能快捷地解决这故障? ...

  10. java分页数据导出excel

    /** * 订单导出(用于统计利润) * @return */ public String orderExport() throws IOException{ if (queryOrderList_c ...