Poj 3160 [Tarjan contraction point + topological sorting + dp]

Source: Internet
Author: User

Http://poj.org/problem? Id = 3160

Method: Tarjan contraction point + topsort + dp. Pay attention to this sentence. 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 cocould 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.