zoukankan      html  css  js  c++  java
  • bzoj 3470: Freda’s Walk【拓扑排序+期望dp】

    dfs会T,只好正反两遍拓扑了……

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<vector>
    using namespace std;
    const int N=100005;
    int n,m,h[N],cnt,d1[N],d2[N];
    double f[N],p[N],s[N],ans;
    struct qwe
    {
        int ne,no,to;
        double va;
    }e[N];
    vector<pair<int,double> >a[N];
    int read()
    {
        int r=0,f=1;
        char p=getchar();
        while(p>'9'||p<'0')
        {
            if(p=='-')
                f=-1;
            p=getchar();
        }
        while(p>='0'&&p<='9')
        {
            r=r*10+p-48;
            p=getchar();
        }
        return r*f;
    }
    void add(int u,int v,int w)
    {
        cnt++;
        e[cnt].ne=h[u];
        e[cnt].no=u;
        e[cnt].to=v;
        e[cnt].va=w;
        h[u]=cnt;
    }
    int main()
    {
        n=read(),m=read();
        for(int i=1;i<=m;i++)
        {
            int x=read()+1,y=read()+1,z=read();
            s[x]+=z;
    		a[y].push_back(make_pair(x,z)),d1[x]++;
            add(x,y,z),d2[y]++;
        }
    	queue<int>q;
    	for(int i=1;i<=n;i++)
    		if(!d1[i])
    			q.push(i);
    	while(!q.empty())
    	{
    		int u=q.front();
    		q.pop();
    		for(int i=0;i<a[u].size();i++)
    		{
    			f[a[u][i].first]+=(f[u]+1.0)*a[u][i].second/s[a[u][i].first];
    			if(!(--d1[a[u][i].first]))
    				q.push(a[u][i].first);
    		}
    	}
    	p[1]=1;
    	for(int i=1;i<=n;i++)
    		if(!d2[i])
    			q.push(i);
    	while(!q.empty())
    	{
    		int u=q.front();
    		q.pop();
    		for(int i=h[u];i;i=e[i].ne)
    		{
    			p[e[i].to]+=p[u]*e[i].va/s[u];
    			if(!(--d2[e[i].to]))
    				q.push(e[i].to);
    		}
    	}
        ans=f[1];
        for(int i=1;i<=cnt;i++)
            ans=max(ans,f[1]+((f[e[i].no]-(f[e[i].to]+1)*e[i].va/s[e[i].no])*s[e[i].no]/(s[e[i].no]-e[i].va)-f[e[i].no])*p[e[i].no]);
        printf("%.6f
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    Android发送信息模拟系统
    Android SharedPreferences
    Android中SQLiteDatabase操作【附源码】
    poj 2453
    pku 1020
    poj 2594 Treasure Exploration
    pku 2092 Grandpa is Famous
    zoj Weekend Party
    poj 3259 Wormholes
    poj 2455 Secret Milking Machine
  • 原文地址:https://www.cnblogs.com/lokiii/p/9687736.html
Copyright © 2011-2022 走看看