您的位置:首页 > 其它

Codeforces 702E Analysis of Pathes in Functional Graph(倍增)

2016-08-26 12:34 295 查看
给你一个图,每个点都有一条出边

然后边有权值,让你输出走k步,从i出发走的总距离和其中最小的边是多少

k很大,图可以预处理,倍增法

代码:

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           100005
#define   MAXN          1000005
#define   maxnode       205
#define   sigma_size    26
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-4;
const LL    mod    = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/

int pre[MAX][40];
int w[MAX][40];
LL s[MAX][40];
LL ans1;
int ans2;

void solve(int u,LL k){
if(k==0){
return ;
}
int t=0;
while((1LL<<t)<=k) t++;
t--;
ans1+=s[u][t];
ans2=min(ans2,w[u][t]);
solve(pre[u][t],k-(1LL<<t));
}
int main(){
//freopen("in.txt","r",stdin);
int n;
LL k;
while(cin>>n>>k){
for(int i=0;i<n;i++) scanf("%d",&pre[i][0]);
for(int i=0;i<n;i++) scanf("%d",&w[i][0]),s[i][0]=w[i][0];
for(int i=1;(1LL<<i)<=k;i++){
for(int j=0;j<n;j++){
pre[j][i]=pre[pre[j][i-1]][i-1];
w[j][i]=min(w[j][i-1],w[pre[j][i-1]][i-1]);
s[j][i]=s[j][i-1]+s[pre[j][i-1]][i-1];
}
}
for(int i=0;i<n;i++){
ans1=0;
ans2=INF;
solve(i,k);
printf("%I64d %d\n",ans1,ans2);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: