poj 3160【tarjan縮點+拓撲排序+DP】

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.