【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse

Father Christmas flymouse

Time Limit: 1000MS Memory Limit: 131072K

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.

Source

POJ Monthly–2006.12.31, Sempr



题意: 给出一张有向图,图上各点有权值,权值可能为负,选择图上一个顶点出发,每个点的权值可以选择加或者不加1,求可能得到的最大权值和。

思路: 首先对于一个强连通分量,从任意一个点进入都可以到达分量中所有的点,因此先用Tarjan算法进行缩点。由于负权点根本不用加上权值,因此负权点权值设为02。

然后尝试对每个入度为0的点进行dfs,求出最大值。

代码如下

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 3e4 + 5, M = 15e4 + 5;
struct Edge {
    int v, next;
    Edge(){}
    Edge(int _v, int _next):
        v(_v), next(_next){}
}e[M];
int tot, deep, scc_cnt, n, m;
int head[N], dfn[N], scc_id[N], w[N];
int line[M][2], in[N], sum[N];
stack <int> s;
vector <int> ee[N];

void init()
{
    tot = deep = scc_cnt = 0;
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(scc_id, 0, sizeof(scc_id));
    memset(in, 0, sizeof(in));
    memset(sum, 0, sizeof(sum));
}

void add(int u, int v)
{
    e[tot] = Edge(v, head[u]);
    head[u] = tot++;
}

int dfs(int u)
{
    int lowu = dfn[u] = ++deep;
    s.push(u);
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(!dfn[v]) {
            int lowv = dfs(v);
            lowu = min(lowu, lowv);
        }
        else if(!scc_id[v]) {
            lowu = min(lowu, dfn[v]);
        }
    }
    if(lowu == dfn[u]) {
        scc_cnt++;
        while(1) {
            int x = s.top(); s.pop();
            scc_id[x] = scc_cnt;
            sum[scc_cnt] += w[x];
            if(x == u) break;
        }
    }
    return lowu;
}

void tarjan()
{
    for(int i = 0; i < n; i++) {
        if(!dfn[i]) dfs(i);
    }
}

int dfs2(int u, int tmp)
{
    int sz = ee[u].size();
    int maxi = tmp;
    for(int i = 0; i < sz; i++) {
        int v = ee[u][i];
        maxi = max(maxi, dfs2(v, tmp + sum[v]));
    }
    return maxi;
}

int solve()
{
    int ans = 0;
    for(int i = 1; i <= scc_cnt; i++) ee[i].clear();
    for(int i = 0; i < m; i++) {
        int u = scc_id[line[i][0]], v = scc_id[line[i][1]];
        if(u != v) {
            in[v]++;
            ee[u].PB(v);
        }
    }
    for(int i = 1; i <= scc_cnt; i++) {
        if(!in[i]) {
            ans = max(ans, dfs2(i, sum[i]));
        }
    }
    return ans;
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    while(~scanf("%d%d", &n, &m)) {
        init();
        for(int i = 0; i < n; i++) {
            scanf("%d", &w[i]);
            if(w[i] < 0) w[i] = 0;
        }
        int u, v;
        for(int i = 0; i < m; i++) {
            scanf("%d%d", &u, &v);
            add(u, v);
            line[i][0] = u; line[i][1] = v;
        }
        tarjan();
        int ans = solve();
        printf("%d\n", ans);
    }
    return 0;
}

  1. 题意描述不清楚,假如整张图都是负权,答案是0。 ?
  2. 不得不怀疑出题人是否脑残。 ?
时间: 2024-10-11 01:33:00

【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse的相关文章

POJ 3160 Father Christmas flymouse

Father Christmas flymouse Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ID: 316064-bit integer IO format: %lld      Java class name: Main After retirement as contestant from WHU ACM Team, flymouse volunteered

poj 3160 Father Christmas flymouse 强连通+dp

首先我们可以确定的是,对于val值小于0的节点都变成0.   假设一个集合内2个房间都能任意到达,那么我就可以吧集合内的所有点的价值都取到,并且可以达到任一点.实际上集合内的每个点是相同的,这样的集合就是一个强连通分量. 那么我们就可以用tarjin算法进行强连通缩点, 最后形成一个dag的图.在dag的图上面进行dp.可以先用拓扑排序后dp.或者建反响边记忆化搜索 . VIEW  CDDE //#pragma comment(linker, "/STACK:102400000,10240000

poj 3160 Father Christmas flymouse【强连通 DAG spfa 】

和上一道题一样,可以用DAG上的动态规划来做,也可以建立一个源点,用spfa来做 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<stack> 6 #include<vector> 7 using namespace std; 8 9 const int maxn = 5005; 10 int n,

POJ——T3160 Father Christmas flymouse

Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3496   Accepted: 1191 缩点,然后每个新点跑一边SPFA 思路不难 ,注意细节~ 1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 6 using namespace std; 7 8 const

【连通图|强连通分量+缩点】POJ-1236 Network of Schools

Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes softwa

TarJan 算法求解有向连通图强连通分量

[有向图强连通分量] 在有向图G中,如果两个 顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components). 下图中,子图{1,2,3,4}为一个强连通分量,因为顶点1,2,3,4两两可达.{5},{6}也分别是两个强连通分量. 大体来说有3中算法Kosaraju,Trajan,Gabow这三种!后续文章中将相继

【连通图|强连通分量+最长路】POJ-3592 Instantaneous Transference

Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous transfer. When an object uses this magic fu

POJ 2186:Popular Cows(强连通分量)

[题目链接] http://poj.org/problem?id=2186 [题目大意] 给出一张有向图,问能被所有点到达的点的数量 [题解] 我们发现能成为答案的,只有拓扑序最后的SCC中的所有点, 那么我们从其中一个点开始沿反图dfs,如果能访问到全图, 则答案为其所在SCC的大小,否则为0. [代码] #include <cstdio> #include <algorithm> #include <vector> #include <cstring>

POJ 3177 Redundant Paths(强连通分量)

题目链接:http://poj.org/problem?id=3177 题目大意是一个无向图给你n个点m条边,让你求出最少加多少条边 可以让任意两个点相通两条及以上的路线(每条路线点可以重复,但是每条路径上不能有重边),简单来说就是让你加最少的边使这个图变成一个双连通图. 首先用tarjan来缩点,可以得到一个新的无环图,要是只有一个强连通分量,那本身就是一个双连通图.要是多个强连通分量,那我们可以考虑缩点后度数为1的点(肯定是由这个点开始连新边最优),那我们假设数出度数为1的点的个数为cnt,