[概率DP] BZOJ 3036 绿豆蛙的归宿

好水的题


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define V G[p].v
using namespace std;
typedef pair<int,int> abcd;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x){
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=100005;

struct edge{
	int u,v,w,next;
};

edge G[2*N];
int head[N],inum;

inline void add(int u,int v,int w,int p){
	G[p].u=u; G[p].v=v; G[p].w=w; G[p].next=head[u]; head[u]=p;
}

int n,m,deg[N];
double f[N];
int vst[N];

inline double dfs(int u){
	if (vst[u]) return f[u];
	vst[u]=1;
	for (int p=head[u];p;p=G[p].next)
		f[u]+=dfs(V)+G[p].w;
	if (deg[u]) f[u]/=deg[u];
	return f[u];
}

int main()
{
	int iu,iv,iw;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n); read(m);
	for (int i=1;i<=m;i++)
		read(iu),read(iv),read(iw),deg[iu]++,add(iu,iv,iw,++inum);
	printf("%.2lf\n",dfs(1));
	return 0;
}


你可能感兴趣的:([概率DP] BZOJ 3036 绿豆蛙的归宿)