点击打开链接
和poj3592一个套路 缩完点跑最长路 但是要注意 缩点时不能把负值加上!
using namespace std;
struct node
{
int v;
int w;
int next;
};
stack <int> stk;
queue <int> que;
node edge1[150010],edge2[150010];
int per[30010],dis[30010],first1[30010],first2[30010],dfn[30010],low[30010],book[30010],belong[30010],val[30010],degree[30010];
int n,m,num,tot;
void addedge(node* edge,int* first,int u,int v,int w)
{
edge[num].v=v;
edge[num].w=w;
edge[num].next=first[u];
first[u]=num++;
return;
}
void dfs(int cur)
{
int i,v;
stk.push(cur);
num++;
dfn[cur]=num,low[cur]=num,book[cur]=1;
for(i=first1[cur];i!=-1;i=edge1[i].next)
{
v=edge1[i].v;
if(!dfn[v])
{
dfs(v);
low[cur]=min(low[cur],low[v]);
}
else if(book[v])
{
low[cur]=min(low[cur],dfn[v]);
}
}
if(dfn[cur]==low[cur])
{
tot++;
while(!stk.empty())
{
v=stk.top();
stk.pop();
book[v]=0;
belong[v]=tot;
if(per[v]>=0) val[tot]+=per[v];
if(cur==v) break;
}
}
return;
}
void tarjan()
{
int i,u,v;
while(!stk.empty()) stk.pop();
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(book,0,sizeof(book));
memset(belong,0,sizeof(belong));
memset(val,0,sizeof(val));
num=0,tot=0;
for(i=1;i<=n;i++)
{
if(!dfn[i]) dfs(i);
}
memset(first2,-1,sizeof(first2));
memset(degree,0,sizeof(degree));
num=0;
for(u=1;u<=n;u++)
{
for(i=first1[u];i!=-1;i=edge1[i].next)
{
v=edge1[i].v;
if(belong[u]!=belong[v])
{
addedge(edge2,first2,belong[u],belong[v],val[belong[v]]);
degree[belong[v]]++;
}
}
}
return;
}
int spfa()
{
int i,u,v,w,maxx;
while(!que.empty()) que.pop();
for(i=1;i<=tot;i++)
{
dis[i]=-N;
book[i]=0;
}
for(i=1;i<=tot;i++)
{
if(!degree[i])
{
que.push(i);
dis[i]=val[i];
book[i]=1;
}
}
while(!que.empty())
{
u=que.front();
que.pop();
book[u]=0;
for(i=first2[u];i!=-1;i=edge2[i].next)
{
v=edge2[i].v,w=edge2[i].w;
if(dis[v]<dis[u]+w)
{
dis[v]=dis[u]+w;
if(!book[v])
{
que.push(v);
book[v]=1;
}
}
}
}
maxx=0;
for(i=1;i<=tot;i++)
{
maxx=max(maxx,dis[i]);
}
return maxx;
}
int main()
{
int i,u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&per[i]);
}
memset(first1,-1,sizeof(first1));
num=0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
u++,v++;
addedge(edge1,first1,u,v,0);
}
tarjan();
printf("%d\n",max(0,spfa()));
}
return 0;
}