poj 3160【tarjan缩点+拓扑排序+DP】

http://poj.org/problem?id=3160

方法:tarjan缩点+topsort+dp,注意这句话. 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.(就是说,他从一个点出发,一直走下去,假如缩点后有多个孤立点,他只能选择其中一个)

code:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <limits.h>

using namespace std;

#define LL long long
#define pi acos(-1)
#define N  30010
#define INF INT_MAX
#define eps 1e-8
//*********************************************
//poj 3160 tarjan + top
//*********************************************
vector<int> v[N];
vector<int> top[N];
stack<int> st;
int low[N],dfn[N],in[N],belong[N];
int val[N],vv[N],vvm[N];
bool inStack[N],vis[N];
int time,num;
int n,m;
int min(int a,int b)
{
    return a>b?b:a;
}
int max(int a,int b)
{
    return a>b?a:b;
}
void tarjan(int u)
{
    int i;
    vis[u]=1;
    st.push(u);
    inStack[u]=1;
    time++;
    low[u]=time,dfn[u]=time;
    for(i=0;i<v[u].size();i++)
    {
        int x=v[u][i];
        if(!vis[x])
        {
            tarjan(x);
            low[u]=min(low[u],low[x]);
        }
        else
        if(inStack[x])
            low[u]=min(low[u],dfn[x]);
    }
    if(low[u]==dfn[u])
    {
        num++;
        while(1)
        {
            int x=st.top();
            st.pop();
            belong[x]=num;
            vv[num]+=val[x];
            inStack[x]=0;
            if(x==u)break;
        }
    }
}
void work()
{
    int i,j,k;
    for(i=0;i<n;i++)
        for(j=0;j<v[i].size();j++)
        {
            int x=v[i][j];
            if(belong[i]!=belong[x])
            {
                in[belong[x]]++;
                top[belong[i]].push_back(belong[x]);
            }
        }
    queue<int> q;
    for(i=1;i<=num;i++)
    if(in[i]==0)
    {
        q.push(i);
        vvm[i]=vv[i];
    }
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(i=0;i<top[x].size();i++)
        {
            int y=top[x][i];
            in[y]--;
            vvm[y]=max(vvm[y],vvm[x]+vv[y]);
            if(in[y]==0)
            q.push(y);
        }
    }
    int ans=0;
    for(i=1;i<=num;i++)
        ans=max(ans,vvm[i]);
    printf("%d\n",ans);
}


int main()
{freopen("a.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j,k;
        for(i=0;i<=n;i++)
        {
            vis[i]=inStack[i]=low[i]=dfn[i]=vv[i]=vvm[i]=belong[i]=val[i]=in[i]=0;
            v[i].clear();
            top[i].clear();
        }
        while(!st.empty())st.pop();
        for(i=0;i<n;i++)
        {
            scanf("%d",&val[i]);
            if(val[i]<0)val[i]=0;
        }
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            v[a].push_back(b);
        }
        time=0,num=0;
        for(i=0;i<n;i++)
        if(!vis[i])
        tarjan(i);
        work();
    }
    return 0;
}


你可能感兴趣的:(poj 3160【tarjan缩点+拓扑排序+DP】)