Codeforces 702E Analysis of Pathes in Functional Graph(倍增)
2016-08-26 12:34
295 查看
给你一个图,每个点都有一条出边
然后边有权值,让你输出走k步,从i出发走的总距离和其中最小的边是多少
k很大,图可以预处理,倍增法
代码:
然后边有权值,让你输出走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; }
相关文章推荐
- 【CF 702E】Analysis of Pathes in Functional Graph(倍增)
- Codeforces 702E - Analysis of Pathes in Functional Graph 二进制
- [CF 702E]Analysis of Pathes in Functional Graph
- Educational Codeforces Round 15 Analysis of Pathes in Functional Graph rmq 变形
- CF702E - Analysis of Pathes in Functional Graph
- Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
- Educational Codeforces Round 15 E. Analysis of Pathes in Functional Graph (倍增法)
- Educational Codeforces Round 15 E. Analysis of Pathes in Functio(倍增)★ ★
- Educational Codeforces Round 15 E. Analysis of Pathes in Functional Graph (倍增RMQ)
- Educational Codeforces Round 15 E Analysis of Pathes in Functional Graph(倍增)
- Number of Connected Components in an Undirected Graph
- 【论文笔记】Applications of Graph Theory in Computer Science
- [leetcode323]Number of Connected Components in an Undirected Graph
- Leetcode: Number of Connected Components in an Undirected Graph
- 【网络流】[Codeforces - 739D]Recover a functional graph
- codeforces 724G. Xor-matic Number of the Graph
- 《An Experimental Comparison of Partitioning Strategies in Distributed Graph Processing》——论文笔记
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- A model for the structural, functional, and deontic specification of organizations in multiagent systems
- iGraph的配置,An In-depth Comparison of Subgraph Isomorphism Algorithms in Graph Databases