【洛谷 P2656】采蘑菇

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
int n,m;
stack<int>s;
int ss;
struct edge
{
    int f,t,s;
    double c;
}es[200000+5],es1[200000+5];
int first[80005],nxt[200000+5],tot=0;
int first1[80005],nxt1[200000+5],tot1=0;
void build(int f,int t,int start,double change)
{
    es[++tot]=(edge){f,t,start,change};
    nxt[tot]=first[f];
    first[f]=tot;
}
void build1(int f,int t,int start,double change)
{
    es1[++tot1]=(edge){f,t,start,change};
    nxt1[tot1]=first1[f];
    first1[f]=tot1;
}
int dfn[80005],low[80005],scc[80005],size[80005];
int pq[80005];
int numscc=0,dfs_clock=0;
void dfs(int x)
{
    low[x]=dfn[x]=++dfs_clock;
    s.push(x);
    for(int i=first[x];i;i=nxt[i])
    {
        int w=es[i].t;
        if(!dfn[w])
        {
            dfs(w);
            low[x]=min(low[x],low[w]);
        } 
        else if(!scc[w])
        {
            low[x]=min(low[x],dfn[w]);
        }
    }
    if(dfn[x]==low[x])
    {
        ++numscc;
        while(1)
        {
            int u=s.top();
            s.pop();
            size[numscc]++;
            scc[u]=numscc;
            if(u==x) break;
        }
    }
}
int doit(int s,double c)
{
    int ret=s;
    while(s)
    {
        s*=c;
        ret+=s;
    }
    return ret;
}
int dis[80005];
bool vis[80005];
queue<int>q;
void spfa()
{
    q.push(scc[ss]);
    dis[scc[ss]]=pq[scc[ss]];
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        vis[x]=0;
        for(int i=first1[x];i;i=nxt1[i])
        {
            int w=es1[i].t;
            if(dis[w]<dis[x]+es1[i].s+pq[w])
            {
                dis[w]=dis[x]+es1[i].s+pq[w];
                if(!vis[w])
                {
                    q.push(w);
                    vis[w]=1;
                }
            }
        }
    }
}
int main()
{
    int f,start,t;
    double change;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d%lf",&f,&t,&start,&change);
        build(f,t,start,change);
    }
    scanf("%d",&ss);
    dfs(ss);
    for(int i=1;i<=n;i++)
      for(int j=first[i];j;j=nxt[j])
      {
        int w=es[j].t;
        if(scc[w]==scc[i])
        {
            pq[scc[w]]+=doit(es[j].s,es[j].c);
        }
        if(scc[w]!=scc[i])
        {
            build1(scc[i],scc[w],es[j].s,es[j].t);
        }
      }//处理点权 重建边
    spfa();//跑最长路
    int ans=0;
    for(int i=1;i<=n;i++)
      ans=max(ans,dis[i]);
    printf("%d\n",ans); 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值