Father Christmas flymouse (tarjan缩点+DAG)

才学tarjan算法,写篇博客记录一下吧

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 Ndistinct 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.

题意:给你一个有向图,并且每个点有一个权值(可能是负数),你可以任选一个点作为起点,沿边行走,每经过一个点你可以选择是否选取它的权值,点可以重复走,但是权值只能取一次,问能取的权值最大和是多少?

题解:tarjan缩点+DAG,只要会tarjan就是一道简单的DAG问题,直接看AC代码吧

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
#define Root 1, m, 1
#define Node l, r, node
const int INF  =  0x3f3f3f3f;
const int O    =  1e6;
const int mod  =  20071027;
const int maxn =  3e4 + 5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

int head[maxn<<1], cnt ;
struct dd {int to, next; }e[maxn<<4];
void einit() { MT(head, -1); cnt = 0; }
void add(int u, int v) { e[cnt] = {v, head[u]}; head[u] = cnt ++; }

int n, m;
int a[maxn], b[maxn<<1];

int num, t, top;
int bel[maxn], low[maxn], dfn[maxn], sta[maxn], ins[maxn];

void tarjan(int u){
    low[u] = dfn[u] = ++ t;
    sta[++top] = u; ins[u] = 1;
    for(int i=head[u]; i!=-1; i=e[i].next) {
        int v = e[i].to;
        if(dfn[v] == -1) {
            tarjan(v); low[u] = min(low[u], low[v]);
        }
        else if(ins[v]) low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u]) {
        num ++ ;
        do {
            int p = sta[top]; bel[p] = num; ins[p] = 0;
        }while(sta[top--] != u);
    }
}

void solve(){
    top = t = 0; num = 30000; MT(b, 0);
    for(int i=0; i<n; i++) { bel[i] = dfn[i] = -1; ins[i] = 0; }
    for(int i=0; i<n; i++) if(dfn[i] == -1) tarjan(i);
    for(int u=0; u<n; u++) {
        if(a[u] > 0) b[bel[u]] += a[u]; // 缩点后的权值
        for(int i=head[u]; i!=-1; i=e[i].next){
            int v = e[i].to;
            if(bel[u] != bel[v]) add(bel[u], bel[v]); //若不在同一个连通分量内,重新建边
        }
    }
}

int dp[maxn<<1]; // 记忆化

int dfs(int u){ //DAG
    if(dp[u] != -1 ) return dp[u];
    dp[u] = b[u];
    for(int i=head[u]; i!=-1; i=e[i].next) {
        int v = e[i].to;
        dp[u] = max(dp[u], b[u] + dfs(v));
    }
    return dp[u];
}

int main(){
    while( ~scanf("%d%d", &n, &m)){
        for(int i=0; i<n; i++) scanf("%d", &a[i]);
        einit();
        while(m --) {
            int u, v; scanf("%d%d", &u, &v);
            add(u, v);
        }
        solve();
        int ans = 0; MT(dp, -1);
        for(int i=30001; i<=num; i++) ans = max(ans , dfs(i));
        printf("%d\n", ans);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值