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

61 篇文章 0 订阅

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值