Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 2156 | Accepted: 728 |
Description
After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such as cleaning out the computer lab for training as extension of his contribution to the team. When Christmas came, flymouse played Father Christmas to give gifts to the team members. The team members lived in distinct rooms in different buildings on the campus. To save vigor, flymouse decided to choose only one of those rooms as the place to start his journey and follow directed paths to visit one room after another and give out gifts en passant until he could reach no more unvisited rooms.
During the days on the team, flymouse left different impressions on his teammates at the time. Some of them, like LiZhiXu, with whom flymouse shared a lot of candies, would surely sing flymouse’s deeds of generosity, while the others, like snoopy, would never let flymouse off for his idleness. flymouse was able to use some kind of comfort index to quantitize whether better or worse he would feel after hearing the words from the gift recipients (positive for better and negative for worse). When arriving at a room, he chould choose to enter and give out a gift and hear the words from the recipient, or bypass the room in silence. He could arrive at a room more than once but never enter it a second time. He wanted to maximize the the sum of comfort indices accumulated along his journey.
Input
The input contains several test cases. Each test cases start with two integers N and M not exceeding 30 000 and 150 000 respectively on the first line, meaning that there were N team members living in N distinct rooms andM direct paths. On the next N lines there are N integers, one on each line, the i-th of which gives the comfort index of the words of the team member in the i-th room. Then follow M lines, each containing two integers i and jindicating a directed path from the i-th room to the j-th one. Process to end of file.
Output
For each test case, output one line with only the maximized sum of accumulated comfort indices.
Sample Input
2 2
14
21
0 1
1 0
Sample Output
35
题目大意:flymouse要去给人送礼,每个人分布在不同的地方,且他们之间有m条单向边,另外每个人获得礼物后flymouse会得到一个舒心值。现flymouse从某个人开始,沿单向边访问下一个人(其中点可以访问多次,但舒心值只能获得一次),求他能获得的最大舒心值。
分析:这题首先很明显用spfa求最大值,但直接求必然不行,考虑题目特点,可以先利用强连通缩点,这样在一个强连通分量里的必然会都访问(这里需注意,访问不代表获得该点舒心值,比如该点是负的,那么就不会获得该点舒心值,而仅仅是借用此点访问下个点),缩点后建立新图,从每个入度为0的点跑一遍spfa,求最大值
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=30002;
const int M=150002;
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
struct edge{
int v,nxt;
}edge[M];
int dfn[N],low[N],belong[N],stack[N],in[N],head1[N],head2[N],scc,top,index,n,m,cnt;
bool instack[N],vis[N];
int dis[N],sum[N],a[N];
void add(int u,int v,int* head){
edge[cnt].v=v;
edge[cnt].nxt=head[u];
head[u]=cnt++;
}
void tarjan(int x){
int i,v;
dfn[x]=low[x]=++index;
stack[++top]=x,instack[x]=1;
for(i=head1[x];i!=-1;i=edge[i].nxt){
v=edge[i].v;
if(dfn[v]==-1){
tarjan(v);
low[x]=min(low[x],low[v]);
}else if(instack[v]){
low[x]=min(low[x],dfn[v]);
}
}
if(dfn[x]==low[x]){
scc++;
while(1){
v=stack[top--],instack[v]=0;
belong[v]=scc;
sum[scc]+=a[v];
if(v==x)break;
}
}
}
void pre(){
int i,u,v;
scc=index=top=cnt=0;
memset(dfn,-1,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(head1,-1,sizeof(head1));
memset(head2,-1,sizeof(head2));
memset(sum,0,sizeof(sum));
for(i=1;i<=n;i++){scanf("%d",a+i);if(a[i]<0)a[i]=0;}
while(m--){
scanf("%d%d",&u,&v);
add(u+1,v+1,head1);
}
for(i=1;i<=n;i++)if(dfn[i]==-1)tarjan(i);
}
void CreateMap(){
int i,j,v;
memset(in,0,sizeof(in));
for(i=1;i<=n;i++)for(j=head1[i];j!=-1;j=edge[j].nxt){
v=edge[j].v;
if(belong[i]!=belong[v]){
add(belong[i],belong[v],head2);
in[belong[v]]++;
}
}
}
int spfa(int s){
int i,u,v,res;
memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s),vis[s]=1;
res=dis[s]=sum[s];
while(!Q.empty()){
u=Q.front(),Q.pop(),vis[u]=0;
for(i=head2[u];i!=-1;i=edge[i].nxt){
v=edge[i].v;
dis[v]=max(dis[v],dis[u]+sum[v]);
res=max(res,dis[v]);
if(!vis[v])Q.push(v),vis[v]=1;
}
}
return res;
}
int main(){
while(~scanf("%d%d",&n,&m)){
pre();
int i,ans=0;
memset(dis,0,sizeof(dis));
CreateMap();
for(i=1;i<=scc;i++)if(in[i]==0)ans=max(ans,spfa(i));
printf("%d\n",ans);
}
return 0;
}