社区讨论

萌新求助:关于特判

P4284[SHOI2014] 概率充电器参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo82dsg3
此快照首次捕获于
2023/10/27 11:39
2 年前
此快照最后确认于
2023/10/27 11:39
2 年前
查看原帖
我在代码中的某些位置,判断了一下当前这个点当前是否有大于等于1的概率被点亮,但是这样子的话就会WA,WA掉一半的分。
不知道为啥,求助路过大佬。
下面注释掉的两行是我加的判断,注释掉之后就能过,不注释就会WA得很惨。
CPP
#include <iostream>
using namespace std ;

inline int read ( ) {
	char ch = getchar ( ) ;
	int x = 0 ;
	while ( ch < '0' || ch > '9' )
		ch = getchar ( ) ;
	while ( ch >= '0' && ch <= '9' )
		x = x * 10 + ch - 48 , ch = getchar ( ) ;
	return x ;
}

const int N = 500005 ;
const double eps = 1e-8 ;
int n , a[N] ;
double f[N] , g[N] , ans ;

int sign ( double x ) { return x < -eps ? -1 : x > eps ; }

struct Edge {
	int nxt , to , len ;
} edge[N<<1] ;

int cnt , head[N] ;
void insert ( int u , int v , int w ) {
	edge [ ++ cnt ] = { head [ u ] , v , w } ;
	head [ u ] = cnt ;
	edge [ ++ cnt ] = { head [ v ] , u , w } ;
	head [ v ] = cnt ;
}

void dfs1 ( int x , int la ) {
	f [ x ] = a [ x ] / 100.0 ;
	for ( int i = head [ x ] ; i ; i = edge [ i ] .nxt ) {
		int y = edge [ i ] .to ;
		if ( y == la ) continue ;
		dfs1 ( y , x ) ;
		double w = edge [ i ] .len / 100.0 ;
//		if ( sign ( f [ x ] - 1 ) < 0 )
			f [ x ] = f [ x ] + f [ y ] * w - f [ x ] * f [ y ] * w ;
	}
}

void dfs2 ( int x , int la ) {
//	if ( sign ( f [ x ] - 1 ) < 0 )
		f [ x ] = f [ x ] + g [ x ] - f [ x ] * g [ x ] ;
	for ( int i = head [ x ] ; i ; i = edge [ i ] .nxt ) {
		int y = edge [ i ] .to ;
		if ( y == la ) continue ;
		double w = edge [ i ] .len / 100.0 ;
		if ( sign ( 1 - f [ x ] * w ) > 0 )
			g [ y ] = ( f [ x ] - f [ y ] * w ) / ( 1 - f [ y ] * w ) * w ;
		else g [ y ] = w ;
		dfs2 ( y , x ) ;
	}
}

int main ( ) {
	n = read ( ) ;
	for ( int i = 1 ; i < n ; ++ i ) {
		int u = read ( ) , v = read ( ) , p = read ( ) ;
		insert ( u , v , p ) ;
	}
	for ( int i = 1 ; i <= n ; ++ i )
		a [ i ] = read ( ) ;
	dfs1 ( 1 , 0 ) ;
	dfs2 ( 1 , 0 ) ;
	for ( int i = 1 ; i <= n ; ++ i )
		ans += f [ i ] ;
	printf ( "%.6lf\n" , ans ) ;
	return 0 ;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...