POJ 3160 强联通缩点+dp

来源:互联网 发布:淘宝卖家怎么发微淘 编辑:程序博客网 时间:2024/05/01 13:46

Father Christmas flymouse
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 2609 Accepted: 873

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 integersN and M not exceeding 30 000 and 150 000 respectively on the first line, meaning that there wereN team members living in N distinct rooms and M direct paths. On the nextN 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 thei-th room. Then follow M lines, each containing two integers i and j indicating a directed path from the i-th room to thej-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 214210 11 0

Sample Output

35

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

POJ Monthly--2006.12.31, Sempr


分析:强联通缩点+dp(记忆话搜索)


代码:

#pragma comment(linker,"/STACK:102400000,102400000")#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <vector>#include <string>#include <math.h>#include <queue>#include <stack>#include <map>#include <set>using namespace std;typedef long long ll;   //ŒÇµÃ±ØÒªµÄʱºòžÄ³ÉÎÞ·ûºÅconst int maxn=150005;   //µãÊýconst int maxm=1000005;   //±ßÊýconst int INF=1000000000;struct EdgeNode{    int from;    int to;    int next;}edge[maxm];int head[maxn],cnt;void add(int x,int y){    edge[cnt].from=x;edge[cnt].to=y;edge[cnt].next=head[x];head[x]=cnt++;    //printf("%d %d\n",x,y);}void init(){    cnt=0;    memset(head,-1,sizeof(head));}int dfn[maxn],low[maxn],ins[maxn],cixu,scc_count,n,ru[maxn],chu[maxn],sd[maxn];stack<int>S;int zhi[maxn],dp[maxn],a[maxn];void dfs(int u){    int v,x;    dfn[u]=low[u]=++cixu;    S.push(u);    ins[u]=1;    for(int i=head[u];i!=-1;i=edge[i].next)    {        v=edge[i].to;        if(!dfn[v]){            dfs(v);            low[u]=min(low[u],low[v]);        }        else if(ins[v])            //Èç¹ûÒÑŸ­Ëѹý£¬²¢ÇÒÔÚÕ»ÀïÃ棬Èç¹û²»ÔÚÕ»ÀïÃ棬˵Ã÷žÃµãÊôÓÚÆäËûµÄÇ¿Á¬Íš·ÖÁ¿ÀïÃæ            low[u]=min(low[u],dfn[v]);   //ŽËŽŠlowŽúÌædfnÒ²ÊÇ¿ÉÒԵģ¬µ«ÊÇÔÚtarjanÇóÎÞÏòÍŒµÄʱºò²»ÄÜ    }    if(dfn[u]==low[u])          //±íÊŸuΪŽËÇ¿Á¬Íš·ÖÁ¿µÚÒ»žö±»·¢Ïֵĵ㣬ÊôÓÚžÃÇ¿Á¬Íš·ÖÁ¿µÄËùÓеÄÆäËûµã¶ŒÔÚÕ»ÖУ¬ËûµÄÉÏÃæ    {        scc_count++;        do        {            x=S.top();            S.pop();            sd[x]=scc_count;            if(a[x]>0)zhi[scc_count]+=a[x];            ins[x]=0;        }while(x!=u);    }}void tarjan(){    for(int i=1;i<=n;i++)sd[i]=i;   //Ëõµã±êŒÇÊý×é    memset(ins,0,sizeof(ins));    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(zhi,0,sizeof(zhi));    cixu=scc_count=0;    for(int i=1;i<=n;i++)        //¿ÉÄÜËùžøµÄ͌Ϊ²»Á¬ÍšµÄÍŒ£¬·Ö¿é£¬ÒòŽËÐèÒª±éÀúËùÓÐµÄµã    {        if(!dfn[i])            dfs(i);    }}void SD(){    int x,y,len=cnt;    init();    memset(ru,0,sizeof(ru));    memset(chu,0,sizeof(chu));    for(int i=0;i<len;i++){        x=sd[edge[i].from]; y=sd[edge[i].to];        if(x==y)continue;        add(x,y);        chu[x]++; ru[y]++;    }}int dfs1(int u){    int ret=0;    if(dp[u])return dp[u];    for(int i=head[u];i!=-1;i=edge[i].next){        int v=edge[i].to;        ret=max(ret,dp[v]);    }    dp[u]=zhi[u]+ret;    return dp[u];}void solve(){    int ans=0;    memset(dp,0,sizeof(dp));    for(int i=1;i<=scc_count;i++){        if(dp[i]==0)dfs1(i);    }    for(int i=1;i<=scc_count;i++)ans=max(ans,dp[i]);    printf("%d\n",ans);}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int m,i,x,y;    while(~scanf("%d%d",&n,&m))    {        init();        for(i=1;i<=n;i++)scanf("%d",&a[i]);        for(i=1;i<=m;i++){            scanf("%d%d",&x,&y);            x++; y++;            add(x,y);        }        tarjan();        SD();        solve();    }    return 0;}




0 0
原创粉丝点击