Visit the Park

该博客探讨了一种无向图中基于权值和行进序列的概率计算问题。通过给出的样例解析,解释了如何计算从一个节点到另一个节点的期望值,并利用数学期望的性质来求解总期望。AC代码展示了如何实现这个算法,包括路径数量的计算和概率的求和。
摘要由CSDN通过智能技术生成

Visit the Park

题目链接
题意 :
有n个点,m条边的无向图(多路径),每条边有一个权值 ,后给定一个行进序列,问概率是多少?期望值的求法:字符串拼接的样子将每段路的期望值合并

思路 :

3 5 3
1 2 1
1 2 2
2 1 2
2 3 4
3 2 1
1 2 3

在这里插入图片描述
样例解析 :
1 – > 2 有三条路,值为1 2 2
2 – > 3 有两条路,值为1 4
1 x 1 x 10 + 1 = 11
1 x 1 x 10 + 4 = 14
1 x 2 x 10 + 1 = 21
1 x 2 x 10 + 4 = 24

数学期望等于 = 每一种结果 x 概率求和
AC代码 :


#include <iostream>
#include <map>
#include <cstring>
#include <algorithm>
#include <math.h>

#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
#define ll long long
#define mod 998244853
map<int, map<int, map<int, int>>> mp;
int arr[300005];

ll ksm(ll a, ll b,ll p) {
    ll res = 1;
    while (b) {
        if (b & 1)
            res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res % p;
}

ll inv(ll a, ll p){
	return ksm(a, p - 2, p);
}
 
int main(){
	IOS;
	int n, m, k;
	while(cin >> n >> m >> k){
		mp.clear();
		
		for(int i = 0; i < m; i++){
			int u, v, w;
			cin >> u >> v >> w;
			mp[u][v][w]++;
			mp[v][u][w]++;
		}
		
		for(int i = 1; i <= k; i++){
			cin >> arr[i];
		}
		ll sum = 0; //求总的预算
		ll cnt = 1; //求总路径数量 
		bool f = 0;  
		for(int i = 2; i <= k; i++){
			if(mp[arr[i - 1]][arr[i]].size() == 0){
				f = 1;
				break;
			}
			ll ans = 0;
			for(auto l : mp[arr[i - 1]][arr[i]]){
				ans += l.second;
				ans %= mod;
			}
			cnt *= ans;
			cnt %= mod; 
		}
		if(f){
			cout << "Stupid Msacywy!" << endl;
		}else{
			sum = 0;
			for(int i = 2; i <= k; i++){
				ll ans = 0;
				sum *= 10;
				sum %= mod;
				for(auto l : mp[arr[i - 1]][arr[i]]){
					ans += l.second;
					ans %= mod;
				}
				for(auto l : mp[arr[i - 1]][arr[i]]){
					sum += (((l.first * l. second % mod) * cnt % mod)* inv(ans, mod) % mod) % mod;
					sum %= mod;
				}
			}
			cnt = inv(cnt, mod);
			cout << sum * cnt % mod << endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值