传送门
文章目录
- 题意:
- 思路:
题意:
思路:
首先要发现这是一个DAGDAGDAG图,让后我们可以用拓扑在图上跑期望dpdpdp。
定义f[i]f[i]f[i]表示iii到nnn的期望路径长度,知道终止状态f[n]=0f[n]=0f[n]=0,所以我们需要逆推答案f[1]f[1]f[1],那么我们就需要建反图。转移就是:f[i]=∑f[j]+w[j]deg[i]f[i]=\sum \frac{f[j]+w[j]}{deg[i]}f[i]=∑deg[i]f[j]+w[j]
其中deg[i]deg[i]deg[i]是iii点的出度。也就是iii点的期望走∑f[j]+w[j]deg[i]\sum \frac{f[j]+w[j]}{deg[i]}∑deg[i]f[j]+w[j]步。
直接建反图让后拓扑序跑就好啦。
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,M=N*2,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,m;
int d[N],deg[N];
double f[N];
int e[M],ne[M],h[N],w[M],idx;void add(int a,int b,int c)
{e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d%d",&n,&m);memset(h,-1,sizeof(h));for(int i=1;i<=m;i++){int a,b,c; scanf("%d%d%d",&a,&b,&c);add(b,a,c); d[a]++; deg[a]++;}queue<int>q;for(int i=1;i<=n;i++) if(!d[i]) q.push(i);while(q.size()){int u=q.front(); q.pop();for(int i=h[u];~i;i=ne[i]){int x=e[i];f[x]+=(f[u]+w[i])/deg[x];if(--d[x]==0) q.push(x);}}printf("%.2f\n",f[1]);return 0;
}
/**/