题意:有向无环图1到n期望路径长度


连高斯消元都不用了...

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef unsigned long long ll;
const int N=1e5+5;
inline int read() {
    char c=getchar(); int x=0, f=1;
    while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
    while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int n, m, de[N], u, v;
struct edge{int v, w, ne;}e[N<<1];
int cnt=1, h[N];
inline void ins(int u, int v, int w) {e[++cnt]=(edge){v, w, h[u]}; h[u]=cnt;}
double f[N]; int vis[N];
double dfs(int u) {
	if(vis[u]) return f[u];
	vis[u]=1;
	for(int i=h[u];i;i=e[i].ne) f[u] += (double)(dfs(e[i].v) + e[i].w)/de[u];
	return f[u];
}
int main(){
	freopen("in","r",stdin);
	n=read(); m=read();
	for(int i=1; i<=m; i++) u=read(), v=read(), ins(u, v, read()), de[u]++;
	printf("%.2lf", dfs(1));
}

相关文章:

  • 2021-10-25
  • 2021-12-28
  • 2022-02-03
  • 2021-06-02
  • 2021-08-15
  • 2021-07-21
  • 2021-10-23
  • 2022-01-02
猜你喜欢
  • 2021-10-29
  • 2022-12-23
  • 2021-08-29
  • 2021-07-06
  • 2022-12-23
  • 2022-01-28
  • 2021-08-13
相关资源
相似解决方案