poj3160Father Christmas flymouse

Father Christmas flymouse
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 3770 Accepted: 1247

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

题意:Flymouse从武汉大学ACM集训队退役后,做起了志愿者,在圣诞节来临时,Flymouse要打扮成圣诞老人给集训队员发放礼物。集训队员住在校园宿舍的不同寝室,为了节省体力,Flymouse决定从某一个寝室出发,沿着有向路一个接一个的访问寝室并顺便发放礼物,直至能到达的所有寝室走遍为止。对于每一个寝室他可以经过无数次但是只能进入一次,进入房间会得到一个数值(数值可正可负),他想知道他能获得最大的数值和。
思路:先用tarjan进行缩点,然后建图,求最长路,记住每个房间的数值可能为负数,负数的时候当成0来算。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int N=30000+50;
const int inf=0x3f3f3f3f;
int dfn[N],low[N],belong[N];
bool instack[N];
int w[N];
int sw[N];
int head[N];
int vis[N];
int dis[N];
int in[N],out[N];
int cnt,index,num;
vector<int>g[N];
vector<int>e[N];
stack<int>s;
int m,n;
void init()
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(belong,0,sizeof(belong));
    memset(instack,false,sizeof(instack));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(sw,0,sizeof(sw));
    cnt=index=0;
    for(int i=0;i<=n;i++)
    {
        g[i].clear();
        e[i].clear();
    }
    while(!s.empty())
        s.pop();
}
void tarjan(int u)
{
    dfn[u]=low[u]=++index;
    s.push(u);
    instack[u]=true;
    int v;
    for(int i=0;i<g[u].size();i++)
    {
        v=g[u][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        cnt++;
        do{
            v=s.top();
            s.pop();
            belong[v]=cnt;
            instack[v]=false;
        }while(u!=v);
    }
}
int spfa(int x)
{
    queue<int>q;
    while(!q.empty())q.pop();
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=cnt;i++)
    {
        dis[i]=-inf;
    }
    dis[x]=0;
    vis[x]=1;
    q.push(x);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=0;i<e[u].size();i++)
        {
            int v=e[u][i];
            if(dis[v]<dis[u]+sw[v])
            {
                dis[v]=dis[u]+sw[v];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    int ans=-inf;
    for(int i=1;i<=cnt;i++)
    {
        ans=max(ans,dis[i]);
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int i,j;
        init();
        for(i=1;i<=n;i++)
        {
            scanf("%d",&w[i]);
        }
        int a,b;
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            a++,b++;
            g[a].push_back(b);
        }
        for(i=1;i<=n;i++)
        {
            if(!dfn[i])
            {
                tarjan(i);
            }
        }
        for(i=1;i<=n;i++)
        {
            sw[belong[i]]+=(w[i]>0?w[i]:0);
        }
        for(i=1;i<=n;i++)
        {
            for(j=0;j<g[i].size();j++)
            {
                int v=g[i][j];
                if(belong[v]!=belong[i])
                {
                    in[belong[v]]++;
                    e[belong[i]].push_back(belong[v]);
                }
            }
        }
        for(i=1;i<=cnt;i++)
        {
            if(in[i]==0)
            {
                e[0].push_back(i);
            }
        }
        printf("%d\n",spfa(0));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值