#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;
}
【洛谷 P2656】采蘑菇
最新推荐文章于 2021-08-31 17:16:55 发布