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;
}