热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

POJ3126--FatherChristmasflymouse【scc缩点构图&&SPFA求最长路】

FatherChristmasflymouseTimeLimit:1000MSMemoryL

Father Christmas flymouse
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 3007   Accepted: 1021

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.

题目大意:flymouse要去给人送礼,每个人分布在不同的地方,且他们之间有m条单向边,另外每个人获得礼物后flymouse会得到一个舒心值(可正可负)。起始舒适指数为0,现flymouse从某个人开始,沿单向边访问下一个人(其中点可以访问多次,但舒心值只能获得一次),求他能获得的最大舒心值。

大致思路:可以先利用强连通缩点,这样在【一个SCC】里的点必然会都访问,这里需注意,访问不代表获得该点舒心值,比如该点是负的,那么就不会获得该点舒心值,而仅仅是借用此点访问下个点,缩点后建立新图,新建的图不一定是连通图,所以虚拟一个起点,有向连接所有的SCC,SPFA跑一遍最长路, 可以求出到每个点的最长距离,取最大值就是我们要的答案。

具体思路:

思路:用sumnum数组记录每个SCC的最大舒适指数,用dist数组存储虚拟源点0到节点的最远距离。
一:对有向图求出所有的SCC。
二:求出每个SCC的sumnum值。(累加所有为正的舒适指数)
三:缩点并对每边 建立边权sumnum [v]。
四:设置虚拟源点0,连接所有的SCC,边权为对应SCC的sumnum值。
五:以0为源点跑一次SPFA,对dist数组排序后,输出最大的dist值。

#include 
#include 
#include 
#include 
#include 
#define maxn 30000 + 3000
#define maxm 150000 + 15000
#define INF 0x3f3f3f3f
using namespace std;
int n, m;

struct node {
    int u, v, next;
};

node edge[maxn];
int head[maxn], cnt;
int low[maxn], dfn[maxn];
int dfs_clock;
int Stack[maxn], top;
bool Instack[maxn];
int Belong[maxn];
int scc_clock;
int num[maxn];//记录每个点的舒适度
vectorscc[maxn];

void initedge(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v){
    edge[cnt] = {u, v, head[u]};
    head[u] = cnt++;
}

void getmap(){
    for(int i = 1; i <= n; ++i)
        scanf("%d", &num[i]);
    while(m--){
        int a, b;
        scanf("%d%d", &a, &b);
        a++, b++;
        addedge(a, b);
    }
}

void Tarjan(int u, int per){
    int v;
    low[u] = dfn[u] = ++dfs_clock;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].next){
        int v = edge[i].v;
        if(!dfn[v]){
            Tarjan(v, u);
            low[u] = min(low[u], low[v]);
        }
        else if(Instack[v])
            low[u] = min(low[u], dfn[v]);
    }
    if(dfn[u] == low[u]){
        scc_clock++;
        scc[scc_clock].clear();
        do{
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc_clock;
            scc[scc_clock].push_back(v);
        }
        while( v != u);
    }
}

void find(){
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(Belong, 0, sizeof(Belong));
    memset(Stack, 0, sizeof(Stack));
    memset(Instack, false, sizeof(false));
    dfs_clock = scc_clock = top = 0;
    for(int i = 1; i <= n ; ++i){
        if(!dfn[i])
            Tarjan(i, i);
    }
}


struct NODE {
    int u, v, w, next;
};

NODE Map[maxn];
int head1[maxn], cnt1;
int vis[maxn], dist[maxn];
int sumnum[maxn];//记录每个缩点能得到的最大舒适度

void initMap(){
    cnt1 = 0;
    memset(head1, -1, sizeof(head1));
}

void addMap(int u, int v, int w){
    Map[cnt1] = {u, v, w, head1[u]};
    head1[u] = cnt1++;
}

//求出每个缩点能得到的最大舒适度
//作为到达这个缩点的权值
void change(){
    memset(sumnum, 0, sizeof(sumnum));
    for(int i = 1; i <= scc_clock; ++i){
        for(int j = 0; j  0)
                sumnum[i] += num[scc[i][j]];
        }
    }
}

void suodian(){//缩点 && 新建图
    for(int i = 0; i q;
    for(int i = 0; i <= scc_clock; ++i){
        dist[i] = -INF;
        vis[i] = 0;
    }
    vis[x] = 1;
    dist[x] = 0;
    q.push(x);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = head1[u]; i != -1; i = Map[i].next){
            int v = Map[i].v;
            int w = Map[i].w;
            if(dist[v]  
 


推荐阅读
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 李逍遥寻找仙药的迷阵之旅
    本文讲述了少年李逍遥为了救治婶婶的病情,前往仙灵岛寻找仙药的故事。他需要穿越一个由M×N个方格组成的迷阵,有些方格内有怪物,有些方格是安全的。李逍遥需要避开有怪物的方格,并经过最少的方格,找到仙药。在寻找的过程中,他还会遇到神秘人物。本文提供了一个迷阵样例及李逍遥找到仙药的路线。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • Go语言实现堆排序的详细教程
    本文主要介绍了Go语言实现堆排序的详细教程,包括大根堆的定义和完全二叉树的概念。通过图解和算法描述,详细介绍了堆排序的实现过程。堆排序是一种效率很高的排序算法,时间复杂度为O(nlgn)。阅读本文大约需要15分钟。 ... [详细]
author-avatar
白珍茜登陆_585
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有