P4316 绿豆蛙的归宿

1:思路:拓扑排序,

2:(核心)

p[j]+=p[t]*1.0/v[t].size();
			ans+=p[t]*1.0/v[t].size()*x.second;

ACode: 

#include
using namespace std;
const  int N=1e5+10;
int n,m,ru[N];
double p[N];
vector>v[N];
void topsort() {
	queueq;
	q.push(1);
	p[1]=1.0;//初始化
	double ans=0;
	while(!q.empty()) {
		int t=q.front();
		q.pop();
		for(auto x:v[t]) {
			int j=x.first;
			p[j]+=p[t]*1.0/v[t].size();
			ans+=p[t]*1.0/v[t].size()*x.second;
			if(--ru[j]==0) {
				q.push(j);
			}
		}
	}
	cout<>n>>m;
	for(int i=1; i<=m; i++) {
		int x,y,z;
		cin>>x>>y>>z;
		v[x].push_back({y,z});
		ru[y]++;
	}
	topsort();
}
signed main() {


	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int tt=1;
//	cin>>tt;
	while(tt--) solve();
	return 0;
}
//3

over~

你可能感兴趣的:(算法,c++)