POJ3160-Father Christmas flymouse (强连通缩点+dfs)

7 篇文章 0 订阅
3 篇文章 0 订阅

Father Christmas flymouse
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 3764 Accepted: 1245
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 N distinct 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.
题目:POJ3160
题意:给你一张有向图,图中每个节点都有一定的权值(可能为负),当你经过这些点的时候,可以选择是否获得权值,每条边可以重复走,但是单向边,现让你选定一个起点,输出可能获得的最大权值。
思路:每个节点的权值可以选择是否获得,所以我们可以预处理一下,权值为负的节点权值为0,很显然,当我们走到一个强连通分量时,我们可以获得里面的所有权值,所以我们先用Tarjan算法缩点,得到一个DGA,然后DFS每个起点,找到最大的权值即可。
AC代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=30005;
const double eps=1e-4;
int dfn[maxn],dep[maxn],color[maxn],degree[maxn],p[maxn],ps[maxn],num,nums,stacks[maxn],vis[maxn],cont,lid,k,n,m;
ll value[maxn],values[maxn],dp[maxn],ans;
struct edge
{
    int en,next;
} e[5*maxn],es[5*maxn];
void init()
{
    met(p,-1);
    num=0;
    ans=0;
    met(ps,-1);
    nums=0;
    k=0;
    cont=1;
    lid=-1;
    met(vis,0);
    met(dp,0);
    met(dfn,0);
    met(dep,0);
    met(color,0);
    met(degree,0);
    met(value,0);
    met(values,0);
}
void add(int st,int en)
{
    e[num].en=en;
    e[num].next=p[st];
    p[st]=num++;
}
void adds(int st,int en)
{
    es[nums].en=en;
    es[nums].next=ps[st];
    ps[st]=nums++;
}
void tarjan(int u)
{
    vis[u]=1;
    dfn[u]=dep[u]=cont++;
    stacks[++lid]=u;
    for(int i=p[u]; i!=-1; i=e[i].next)
    {
        int v=e[i].en;
        if(vis[v]==0)tarjan(v);
        if(vis[v]==1)dep[u]=min(dep[u],dep[v]);
    }
    if(dfn[u]==dep[u])
    {
        k++;
        do
        {
            color[stacks[lid]]=k;
            values[k]+=value[stacks[lid]];
            vis[stacks[lid]]=-1;
        }
        while(stacks[lid--]!=u);
    }
}
void dfs(int u,ll cost)
{
    dp[u]=max(dp[u],cost+values[u]);
    ans=max(ans,dp[u]);
    for(int i=ps[u];i!=-1;i=es[i].next)
    {
        int v=es[i].en;
        dfs(v,dp[u]);
    }
}
int main()
{
    while(~scann(n,m))
    {
        init();
        for(int i=0;i<n;i++)
        {
            scanl(value[i]);
            if(value[i]<0)value[i]=0;//预处理
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scann(x,y);
            add(x,y);
        }
        for(int i=0;i<n;i++)
        {
            if(!vis[i])tarjan(i);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=p[i];j!=-1;j=e[j].next)
            {
                int v=e[j].en;
                if(color[i]!=color[v])adds(color[v],color[i]),degree[color[i]]++;
            }
        }
        for(int i=1;i<=k;i++)
        {
            if(degree[i]==0)dfs(i,0);//这里我们DFS入度为0的点为起点即可
        }
        prinl(ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值