【POJ - 3160】Father Christmas flymouse(Tarjan缩点,DAG最长路)

题干:

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从武汉大学ACM集训队退役后,做起了志愿者,在圣诞节来临时,Flymouse要打扮成圣诞老人给集训队员发放礼物。集训队员住在校园宿舍的不同寝室,为了节省体力,Flymouse决定从某一个寝室出发,沿着有向路一个接一个的访问寝室并顺便发放礼物,直至能到达的所有寝室走遍为止。对于每一个寝室他可以经过无数次但是只能进入一次,进入房间会得到一个数值(数值可正可负),他想知道他能获得最大的数值和。

解题报告:

   因为每一个scc中可以无限次走过,但是不一定进入房间(也就是获得对应点的权值),所以我们只需要记录每一个scc中有正贡献的点。然后对缩点完的新图的每个起点进行DP求最长路就可以了。

注意代码姿势非常重要,比如你如果最后建新图的边是遍历每一条边,就必须要新开一个TOT,否则会无限循环。当然,如果你不是遍历到tot,而是遍历到m,那就不存在这个问题了。还有就是set去重时的姿势,别写错了。还有初始化,别落下tot和TOT。。其他的就没啥了,挺简单的一道题。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 3e5 + 5;
int n,m;
set<int> ss[MAX];
struct Edge {int to,fr;int ne;
} e[MAX*10],E[MAX*10];
int head[MAX],DFN[MAX],LOW[MAX],stk[MAX],vis[MAX],col[MAX],clk,index,scc;
int HEAD[MAX];
int val[MAX],VAL[MAX],IN[MAX],tot,TOT,TMP[MAX];
int DIS[MAX],VIS[MAX];
void add(int u,int v) {//放弃传head数组进来了,因为还得重新弄个tot。  所以还不如直接写两个函数 。以后公用函数时多看看是否有重名的全局变量!!这一般都是坑 e[++tot].fr = u;e[tot].to = v;e[tot].ne = head[u];head[u] = tot;
}
void ADD(int u,int v) {//放弃传head数组进来了,因为还得重新弄个tot。  所以还不如直接写两个函数 。以后公用函数时多看看是否有重名的全局变量!!这一般都是坑 E[++TOT].fr = u;E[TOT].to = v;E[TOT].ne = HEAD[u];HEAD[u] = TOT;
}
void init() {for(int i = 1; i<=n; i++) {head[i] = HEAD[i] = -1;//其实不用HEAD的初始化 下面有了 DFN[i] = LOW[i] = col[i] = vis[i] = 0;DIS[i] = VAL[i] = VIS[i] = IN[i] = TMP[i] = 0;}clk=index=scc=0;tot=TOT=0;
}
void Tarjan(int x) {DFN[x] = LOW[x] = ++clk;stk[++index] = x;vis[x] = 1;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].to;if(DFN[v] == 0) {Tarjan(v);LOW[x] = min(LOW[x],LOW[v]);}else if(vis[v]) LOW[x] = min(LOW[x],DFN[v]);}if(DFN[x] == LOW[x]) {scc++;while(1) {int tmp = stk[index];index--;vis[tmp]=0;col[tmp] = scc;if(val[tmp] > 0) VAL[scc] += val[tmp];if(tmp == x) break;}}
}
void dfs(int cur,int rt) {if(VIS[cur]) return;VIS[cur] = 1;for(int i = HEAD[cur]; ~i; i = E[i].ne) {int v = E[i].to;if(v == rt) continue;dfs(v,cur);DIS[cur] = max(DIS[cur],DIS[v]);}DIS[cur] += VAL[cur];
}
int main()
{while(~scanf("%d%d",&n,&m)) {init();for(int i = 1; i<=n; i++) scanf("%d",val+i);for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);u++,v++;add(u,v);}for(int i = 1; i<=n; i++) {if(DFN[i] == 0) Tarjan(i); }for(int i = 1; i<=scc; i++) HEAD[i] = -1,ss[i].clear();for(int i = 1; i<=tot; i++) {int u = e[i].fr;int v = e[i].to;u = col[u],v = col[v];if(u == v) continue; if(ss[u].find(v) == ss[u].end()) {ADD(u,v),IN[v]++;ss[u].insert(v);}}int cnt = 0;for(int i = 1; i<=scc; i++) {if(IN[i] == 0) TMP[++cnt] = i;}for(int i = 1; i<=cnt; i++) {dfs(TMP[i],-1);}int ans = 0;for(int i = 1; i<=cnt; i++) {ans = max(ans,DIS[TMP[i]]);}printf("%d\n",ans);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440467.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

docker php 乱码,如何解决docker安装zabbix5.0界面乱码

如何解决docker安装zabbix5.0界面乱码&#xff1f;zabbix图形界面乱码如下&#xff1a;解决&#xff1a;docker部署zabbix-web和源码安装zabbix-web一样&#xff0c;字体都是存储在/usr/share/zabbix/assets/fonts/1、从windown拷贝simkai.ttf(楷体)文件到docker的zabbix-web里…

【POJ - 1330】Nearest Common Ancestors(lca,模板题)

题干&#xff1a; A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor o…

java redis 流水线,Redis系列(1) —— 流水线

写在前面去年下半年&#xff0c;出于学习Redis的目的&#xff0c;在看完《Redis in Action》一书后&#xff0c;开始尝试翻译Redis官方文档。尽管Redis中文官方网站有了译本&#xff0c;但是看别人翻译好的和自己翻译英文原文毕竟还是有很大的不同。这一系列文章之前发布在GitB…

【HDU - 6187】Destroy Walls(思维,最大生成树)

题干&#xff1a; Long times ago, there are beautiful historic walls in the city. These walls divide the city into many parts of area. Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castl…

mseq matlab,Matlab生成M序列

版权声明&#xff1a;本文为CSDN博主「laomai」的原创文章原文链接&#xff1a;https://blog.csdn.net/laomai/article/details/24342493找了好多代码&#xff0c;这个步骤详细可以傻瓜操作&#xff0c;存着备用~实验环境为matlab2013b1、首先编写一个mseq.m文件,内容为:functi…

【HDU - 6185】Covering(矩阵快速幂优化二维dp,高斯消元,轮廓线dp打表)

题干&#xff1a; Bobs school has a big playground, boys and girls always play games here after school. To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets. Me…

Java工厂构造函数参数,javascript 面向对象之构造函数、工厂方式、字面量

什么是面向对象编程面向对象的英文全称:Object Oriented Programming , 简称OOP首先我们要先了解面向过程的编程思想, 就是代码从上到下都没有封装的意思&#xff0c;某写代码裸露在外、没有模块化、代码杂乱无章的写法. 并且还不好维护&#xff0c;也不便于后期二次修改面向对…

【HDU - 6186】CS Course(按位与,按位或,按位异或的区间统计,二进制拆位)

题干&#xff1a; Little A has come to college and majored in Computer and Science. Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework. Here is the problem: You are giving n non-negative integers a1,a2,⋯,ana1,a2,…

mysql不能存字母,使用不常見的字母/符號時,MySql數據庫不能正確存儲數據

I have a simple form that sends (via php) some variables to a mySql database.我有一個簡單的表單,通過PHP發送一些變量到mySql數據庫。The problem is that its focused to Portuguese audience, and we use several unusual letters, like "" and ""…

【HDU - 6184】Counting Stars(三元环计数,二分,优化暴力,O(m*sqrt(m)),图论)

题干&#xff1a; Little A is an astronomy lover, and he has found that the sky was so beautiful! So he is counting stars now! There are n stars in the sky, and little A has connected them by m non-directional edges. It is guranteed that no edges connec…

php 取oracle图片,在PHP中将图片存放ORACLE中_php

我这里提供一个用php操纵blob字段的例子给你&#xff0c;希望能有所帮助&#xff01;这个例子是把用户上传的图片文件存放到BLOB中。假设有一个表&#xff0c;结构如下&#xff1a;CREATE TABLE PICTURES (ID NUMBER,http://www.gaodaima.com/44856.html在PHP中将图片存放oracl…

【HDU - 6183】Color it(CDQ分治 或 动态开点线段树)

题干&#xff1a; Do you like painting? Little D doesnt like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to maintain following operations. The speci…

php create()方法,ThinkPHP中create()方法自动验证实例

ThinkPHP中create()方法自动验证实例2020-06-16 04:24:32自动验证是ThinkPHP模型层提供的一种数据验证方法&#xff0c;可以在使用create创建数据对象的时候自动进行数据验证。原理&#xff1a;create()方法收集表单($_POST)信息并返回&#xff0c;同时触发表单自动验证&#x…

【蓝桥杯官网试题 - 历届试题】格子刷油漆(dp)

题干&#xff1a; 问题描述 X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形&#xff08;如下图所示&#xff09;&#xff0c;现需要把这些格子刷上保护漆。   你可以从任意一个格子刷起&#xff0c;刷完一格&#xff0c;可以移动到和它相邻的格子&#xff08;对角相邻也…

php 解析 ini文件,php解析.ini文件

1.myphp.ini文件autostart false font_size 12font_color red$string[access] 进入;$string[accesshelp] 进入帮助;$string[accesskey] 进入验证 {$a};$string[accessstatement] 进入声明;$string[activitynext] 下一个活动;$string[activityprev] 前一个活动;$string…

【CSU - 1004】Xi and Bo(并查集,裸题)

题干&#xff1a; Bo has been in Changsha for four years. However he spends most of his time staying his small dormitory. One day he decides to get out of the dormitory and see the beautiful city. So he asks to Xi to know whether he can get to another bus …

oracle软件静默安装程序,【oracle】静默安装 oracle 11gr2

【序言】oracle 提供了静默安装方法在不适用图形界面的情况下安装 oracle 软件 ,创建db,配置netca,快速完成oracle 的部署。在以下情形中可以使用静默安装a OUI 的 GUI 界面远程交互比较慢 .b 数据库服务器无法使用图形界面访问.c 批量部署oracle (标准环境统一情况下可以使用o…

【2050 Programming Competition - 2050 一万人码 】非官方部分题解(HDU)

1001 开场白 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12147 Accepted Submission(s): 3502 Problem Description 来自世界各地的年青人在 https://2050.org.cn 握手团聚&#xff0c; 他们是航空…

oracle数据库建表视频,Oracle数据库的创建表全

CREATE TABLE "库名"."表名" ("FEE_ID" VARCHAR2(10 BYTE) constraint ABS_FEE_ID primary key,--主键&#xff0c;必须要有序列"BANK_GROUP_ID" VARCHAR2(5 BYTE),"ABS_PRODUCT_ID" VARCHAR2(30 BYTE))TABLESPACE "表…

oracle的脚本语言是什么意思,Oracle中的sql脚本语言中的循环语句介绍

--sql脚本语言的循环介绍&#xff1a;--1.goto循环点。declarex number;beginx:0;--变量初始化&#xff1b;<>--设置循环点。x:x1;dbms_output.put_line(x);--循环体if x<9 then --进入循环的条件。goto repeat_loop; --用goto关键字引导进入循环。end i…