【NOIP模拟赛19.8.26】死宅与陷阱

题目

【NOIP模拟赛19.8.26】死宅与陷阱_第1张图片

思路

考场想用dijkstra求最短路得方法算出最大概率,但是错了:因为如果有两条路可以到达该点,那么到达这个点的概率应该是走这两条路的概率之和,而不是选择概率大的一条路。所以T1只有5分qwq

正解

直接暴力dfs算概率,最后把概率排序增加陷阱

代码

#include 
using namespace std;
#define N 100005
#define db double

int n,m,p,s,t,tot;
int v[N],head[N],to[N],Next[N],du[N];
db val[N],d[N],ans;
bool vis[N];

inline int read(){
	int x=0,f=0; char ch=getchar();
    while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
inline void add(int x,int y,db z){
	to[++tot] = y;
	val[tot] = z;
	Next[tot] = head[x];
	head[x] = tot;
	++du[y];
}
inline void Dfs(int x,db last){
	if(x != s) d[x] += last;
	ans += (v[x]*last);
	for(int i=head[x]; i; i=Next[i]){
		int y = to[i];
		double z = val[i];
		Dfs(y,last*z);
	}
}
int main()
{
//	freopen("trap.in","r",stdin);
//	freopen("trap.out","w",stdout);
	n=read(); m=read(); p=read(); s=read(); t=read();
	for(int i=1; i<=n; i++)
		v[i] = read();
	for(int i=1,ai,bi; i<=m; i++){
		db ci;
		ai=read();bi=read();scanf("%lf",&ci);
		add(ai,bi,ci);
	}
	Dfs(s,1);
	sort(d+1,d+1+n);
	for(int i=n; i>=n-t+1; i--)
		ans += (d[i]*p);
	printf("%.3lf",ans);
	

你可能感兴趣的:(题解,模拟赛,模拟赛)