求助!!!我调了整个晚上
查看原帖
求助!!!我调了整个晚上
343851
themarine1982楼主2021/3/26 22:42
#include <iostream>
#include <queue>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std; 
const int N=100005;
struct node{
	int end;
	int val;
}gan[N];
int start[N*2];//入边 
int end_[N*2]; //出边 
double p[N],f[N]; //概率
vector <node> vec[N];
queue <int> q;
double ans; 
void kp(){
	q.push(1); //将起点入队 
	p[1]=1; //起点的概率置一 
	while(!q.empty()){
		int ji=q.front();//取队头 
		q.pop(); //出队 
		if(start[ji]==0&&end_[ji==0]){
			return;
		} 
		for(int i=0;i<vec[ji].size();i++){
			start[vec[ji][i].end]--;
			p[vec[ji][i].end]+=p[ji]/end_[ji];
			for(int j=0;j<end_[vec[ji][i].end];j++){
					ans=ans+p[vec[ji][i].end]*vec[ji][i].val;
			}
			if(start[vec[ji][i].end]==0){
				q.push(vec[ji][i].end);
			}
		}
	}
}
int main(int argc, char** argv) {
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int a,b,c;
		cin>>a>>b>>c;
		vec[a].push_back({b,c});
		end_[a]++;
		start[b]++;
	}
	kp();
	printf("%.2f",ans);
	return 0;
}
2021/3/26 22:42
加载中...