标签:des blog io ar os sp for div on
Description
Input
Output
Sample Input
Sample Output
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=15000;
const int maxe=102000;
typedef pair<double,double> PP;
struct edge
{
    double cc;
    int to,next;
} P[maxe];
int head[maxn],si;
PP thing[maxn];
int nn;
int c[maxn],topo[maxn],t; //about topo
double dp[maxn];
void add_edge(int s,int t,double cc)
{
    P[si].to=t;
    P[si].cc=cc;
    P[si].next=head[s];
    head[s]=si++;
}
bool dfs(int u)
{
    c[u]=-1;
    for(int i=head[u];i!=-1;i=P[i].next){
        int v=P[i].to;
        if(c[v]<0) return false;
        else if(!c[v]&&!dfs(v)) return false;
    }
    c[u]=1; topo[--t]=u;
    return true;
}
bool toposort()
{
    t=nn;
    memset(c,0,sizeof(c));
    for(int i=0;i<nn;i++){
        if(!c[i]) if(!dfs(i)) return false;
    }
    return true;
}
void Dp(int u)
{
    if(dp[u]!=0.0) return ;
    dp[u]=thing[u].first;
    for(int i=head[u];i!=-1;i=P[i].next){
        int v=P[i].to;
        Dp(v);
        dp[u]=max(dp[u],dp[v]*P[i].cc);
    }
}
int main()
{
    int mm,kk,s,t;
    double cc,ans;
    while(scanf("%d",&nn),nn!=0){
        memset(head,-1,sizeof(head));
        si=0;
        for(int i=0;i<nn;i++) scanf("%lf%lf",&thing[i].first,&thing[i].second);
        scanf("%d",&mm);
        while(mm--){
            scanf("%d",&kk);
            kk--;
            scanf("%d",&s);
            s--;
            for(int i=0;i<kk;i++){
                scanf("%lf%d",&cc,&t);
                add_edge(s,--t,cc);
                s=t;
            }
        }
        toposort();
        for(int i=0;i<=nn;i++) dp[i]=0.0;
        for(int i=0;i<nn;i++){
            if(dp[topo[i]]==0.0) Dp(topo[i]);
        }
        ans=0;
        for(int i=0;i<nn;i++) ans+=thing[i].second*dp[i];
        printf("%.2f\n",ans);
    }
    return 0;
}
hdu 3696 10 福州 现场 G - Farm Game
标签:des blog io ar os sp for div on
原文地址:http://www.cnblogs.com/xuesu/p/4087960.html