BZOJ2306 [Ctsc2011]幸福路径

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:BZOJ2306

正解:倍增+floyd

解题报告:

  这道题很有意思啊...

  显然不能直接算贡献,考虑概率衰减的很快,我们可以做到一定的次数就视为得到了最优解,精度够了就行。

  而这个$floyd$的过程可以用倍增加速,考虑用$f[t][i][j]$表示从i出发走$2^i$步之后走到$j$的最优值。

  那么转移就是$f[t][i][j]=max(f[t-1][i][k]+f[t-1][k][j]*p^{2^t})$。

  我开始没想清楚为啥是$2^t$而不是$2^{t-1}$,知道我从$t=1$、$t=2$发现我又ZZ了…

  注意这个状态是不含有起点的贡献的!这样可以避免算重,所以最后加一个起点的权值即可。

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 150;
const LB inf = 1e30;
int n,m,S;
LB f[MAXN][MAXN],g[MAXN][MAXN],p,a[MAXN],ans;

inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}

inline void work(){
	n=getint(); m=getint(); for(int i=1;i<=n;i++) cin>>a[i];
	S=getint(); cin>>p; int x,y;
	for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) f[i][j]=-inf;
	for(int i=1;i<=n;i++) f[i][i]=0; 
	for(int i=1;i<=m;i++) { x=getint(); y=getint(); f[x][y]=a[y]*p/*!!!*/; }

	for(LB tmp=p;tmp>(LB)(1e-8);tmp*=tmp) {
		for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) g[i][j]=-inf;
		for(int k=1;k<=n;k++)
			for(int i=1;i<=n;i++)
				for(int j=1;j<=n;j++)
					g[i][j]=max(g[i][j],f[i][k]+f[k][j]*tmp);
		memcpy(f,g,sizeof(g));
	}
	for(int i=1;i<=n;i++) ans=max(ans,f[S][i]);
	printf("%.1Lf",ans+a[S]);
}

int main()
{
    work();
    return 0;
}

  

原文地址:https://www.cnblogs.com/ljh2000-jump/p/6511695.html