zoukankan      html  css  js  c++  java
  • (tajan缩点+SPFA求最长路) poj 3160

    Father Christmas flymouse
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 2887   Accepted: 966

    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 Ndistinct rooms and M 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 j indicating 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

    Hint

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

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    #include<map>
    using namespace std;
    vector<int> e[30010],mp[30010];
    stack<int> s;
    int n,m;
    int Dfs[30010],use[30010],low[30010],isstack[30010],a[30010],sum[30010],in[30010];
    int top,newflag;
    bool vis[30010];
    int dist[30010];
    void init()
    {
        memset(Dfs,0,sizeof(Dfs));
        memset(use,0,sizeof(use));
        memset(low,0,sizeof(low));
        memset(isstack,0,sizeof(isstack));
        memset(sum,0,sizeof(sum));
        memset(in,0,sizeof(in));
        top=newflag=0;
        for(int i=1;i<=n;i++)
        {
            e[i].clear();
            mp[i].clear();
        }
        while(!s.empty())
            s.pop();
    }
    void tarjan(int u)
    {
        Dfs[u]=low[u]=++top;
        s.push(u);
        isstack[u]=1;
        for(int i=0;i<e[u].size();i++)
        {
            int v=e[u][i];
            if(!Dfs[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(isstack[v])
                low[u]=min(low[u],Dfs[v]);
        }
        if(low[u]==Dfs[u])
        {
            newflag++;
            int x;
            do
            {
                x=s.top();
                s.pop();
                use[x]=newflag;
                isstack[x]=0;
                sum[newflag]+=a[x];
            }while(x!=u);
        }
    }
    int spfa(int u)
    {
        int ans;
        queue<int> q;
        memset(dist,0,sizeof(dist));
        memset(vis,0,sizeof(vis));
        dist[u]=sum[u];
        vis[u]=1;
        ans=dist[u];
        q.push(u);
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            vis[x]=0;
            for(int i=0;i<mp[x].size();i++)
            {
                int v=mp[x][i];
                if(dist[v]<dist[x]+sum[v])
                {
                    dist[v]=dist[x]+sum[v];
                    ans=max(ans,dist[v]);
                    if(!vis[v])
                    {
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        }
        return ans;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            init();
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                if(a[i]<0)
                    a[i]=0;
            }
            for(int i=1;i<=m;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                x++,y++;
                e[x].push_back(y);
            }
            for(int i=1;i<=n;i++)
            {
                if(!Dfs[i])
                    tarjan(i);
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<e[i].size();j++)
                {
                    if(use[i]!=use[e[i][j]])
                    {
                        mp[use[i]].push_back(use[e[i][j]]);
                        in[use[e[i][j]]]++;
                    }
                }
            }
            int ans=0;
            for(int i=1;i<=newflag;i++)
            {
                if(in[i]==0)
                    ans=max(ans,spfa(i));
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    练手
    课余时间娱乐下
    2017-2-19,作业
    JavaScript(下)
    JavaScript(上)
    Day15:网络编程-HTTP
    小知识:静态导入
    多线程知识点:锁
    多线程知识点:同步
    Day16:反射技术
  • 原文地址:https://www.cnblogs.com/water-full/p/4527335.html
Copyright © 2011-2022 走看看