POJ 3160 Father Christmas flymouse 强连通缩点+spfa最长路

Father Christmas flymouse
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 2479 Accepted: 823

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 andM 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 jindicating 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.

Source



缩点重新建图之后,建立一个超级源点,与入度为0的点连边,然后跑spfa最长路,枚举与叶子结点的距离,取最大值

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;


const int maxn=33333;

int pos[maxn];

int sum[maxn];

struct node{
    int u,to,next,w;
}e[maxn*10],e1[maxn*10];

int id[maxn],q[maxn],dfn[maxn],low[maxn],num[maxn];

int n,m,tsp,qe,cnt,edge,head[maxn];

int edge1,head1[maxn],dis[maxn],vis[maxn];
int in[maxn],out[maxn];

void init()
{
    memset(head,-1,sizeof(head));
    edge=0;
}

void init1()
{
    CLR(sum),CLR(in),CLR(out);
    edge1=0;
    MST(head1,-1);
}

void addedge(int u,int v)
{
    e[edge].u=u;
    e[edge].to=v;
    e[edge].next=head[u];
    head[u]=edge++;
}

void addedge1(int u,int v,int w)
{
    e1[edge1].u=u;
    e1[edge1].to=v;
    e1[edge1].w=w;
    e1[edge1].next=head1[u];
    head1[u]=edge1++;
}

void tarjan(int u)
{
    int i,v;
    dfn[u]=low[q[qe++]=u]=++tsp;
    for(i=head[u];i>=0;i=e[i].next)
        if(!dfn[v=e[i].to])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else
        {
            if(id[v]<0)
            {
              low[u]=min(low[u],dfn[v]);
            }
        }
    if(low[u]==dfn[u])
    {
        ++cnt;
        do{
             v=q[--qe];
             ++num[id[v]=cnt];

        }while(v!=u);
    }
}


void solve()
{
    int i;
    tsp=qe=cnt=0;
    for(i=0;i<=n;++i)
        id[i]=-1,dfn[i]=0;
    for(i=1;i<=n;++i)
        if(!dfn[i])
            tarjan(i);
}

int spfa(int s)
{
    CLR(dis);
    CLR(vis);
    queue<int>q;
    q.push(s);
    vis[s]=1;
    dis[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head1[u];~i;i=e1[i].next)
        {
            int v=e1[i].to;
            if(dis[v]<dis[u]+e1[i].w)
            {
                dis[v]=dis[u]+e1[i].w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    int res=0;
    FOR(i,1,cnt)
     if(!out[i])
        res=max(res,dis[i]);
    return res;
}


int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u,v;

        init();

        FOR(i,1,n)
         scanf("%d",&pos[i]);
        REP(i,m)
        {
            scanf("%d%d",&u,&v);
            addedge(u+1,v+1);
        }

        solve();

        init1();

        FOR(i,1,n)
        {
            if(pos[i]>0)
                sum[id[i]]+=pos[i];
        }

        FOR(u,1,n)
        {
            for(int j=head[u];~j;j=e[j].next)
            {
                v=e[j].to;
                if(id[u]!=id[v])
                {
                    in[id[v]]++;
                    out[id[u]]++;
                    addedge1(id[u],id[v],sum[id[v]]);
                }
            }
        }

        FOR(i,1,cnt)
        {
            if(!in[i])
            {
                addedge1(0,i,sum[i]);
            }
        }

        cout<<spfa(0)<<endl;

    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值