L - Father Christmas flymouse
来源poj3160
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.
要走过节点最大,用tarjan算强连通,然后算出强连通里面的值是多少,把他看成一个点,建一个新的树,dfs
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=30005;
struct Edge {
int v,next;
}edge[5*N];
int dfn[N],low[N],sum[N];
int stack[N],node[N],visit[N],cnt,tot,index,value[N];
int belong[N],bcnt;
vector<int>tree[N];
vector<int>v;
void add_edge(int x,int y)
{
edge[cnt].next=node[x];
edge[cnt].v = y;
node[x]=cnt++;
return ;
}
int dfs(int x,int y)
{
int maxi=y;
rep(i,0,tree[x].size())
{
maxi=max(maxi,dfs(tree[x][i],y+sum[tree[x][i]]));
}
return maxi;
}
void tarjan(int x)//代表第几个点在处理。递归的是点。
{
dfn[x]=low[x]=++tot;// 新进点的初始化。
stack[++index]=x;//进站
visit[x]=1;//表示在栈里
for(int i=node[x];i!=-1;i=edge[i].next)
{
if(!dfn[edge[i].v]) {//如果没访问过
tarjan(edge[i].v);//往下进行延伸,开始递归
low[x]=min(low[x],low[edge[i].v]);//递归出来,比较谁是谁的儿子/父亲,就是树的对应关系,涉及到强连通分量子树最小根的事情。
}
else if(visit[edge[i].v ]){ //如果访问过,并且还在栈里。
low[x]=min(low[x],dfn[edge[i].v]);//比较谁是谁的儿子/父亲。就是链接对应关系
}
}
if(low[x]==dfn[x]) //发现是整个强连通分量子树里的最小根。
{
bcnt++;
do{
belong[stack[index]]=bcnt;
visit[stack[index]]=0;
index--;
}while(x!=stack[index+1]);//出栈
}
}
int in[N];
void solve(int n)
{
tot=index=bcnt=0;
v.clear();
rep(i,0,n+1)
tree[i].clear();
mm(dfn,0);
mm(in,0);
mm(sum,0);
mm(low,0);
mm(belong,0);
rep(i,1,n+1)
if(!dfn[i])
tarjan(i);
rep(i,1,n+1)//计算入度
{
if(value[i]>=0)
sum[belong[i]]+=value[i];
for(int j=node[i];j!=-1;j=edge[j].next)
{
if(belong[i]!=belong[edge[j].v])
{
in[belong[edge[j].v]]++;
tree[belong[i]].push_back(belong[edge[j].v]);//连边
}
}
}
int ans=0;
rep(i,1,bcnt+1)
{
if(!in[i])
{
ans=max(ans,dfs(i,sum[i]));
}
}
pf("%d\n",ans);
}
int main()
{
int n,m;
while(~sf("%d%d",&n,&m))
{
cnt=0;
mm(node,-1);
rep(i,1,n+1)
sf("%d",&value[i]);
while(m--)
{
int x,y;
sf("%d%d",&x,&y);
add_edge(x+1,y+1);
}
solve(n);
}
return 0;
}
L - Father Christmas flymouse的更多相关文章
- POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3241 Accep ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- Father Christmas flymouse
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3479 Accep ...
- poj 3160 Father Christmas flymouse
// 题目描述:从武汉大学ACM集训队退役后,flymouse 做起了志愿者,帮助集训队做一些琐碎的事情,比如打扫集训用的机房等等.当圣诞节来临时,flymouse打扮成圣诞老人给集训队员发放礼物.集 ...
- poj 3160 Father Christmas flymouse【强连通 DAG spfa 】
和上一道题一样,可以用DAG上的动态规划来做,也可以建立一个源点,用spfa来做 #include<cstdio> #include<cstring> #include< ...
- POJ——T3160 Father Christmas flymouse
Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3496 Accepted: 1191 缩点,然后每个新点跑一边SPFA ...
- Father Christmas flymouse--POJ3160Tarjan
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...
- POJ:3160-Father Christmas flymouse
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...
- 【转】Tarjan&LCA题集
转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...
随机推荐
- Windows7下安装与破解IntelliJ IDEA2017
IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各类版本工具( ...
- 通过脚本调用MSBuild编译项目时指定Configuration(解決方案配置)和Platform(解決方案平台),Rebuid(重新生成解决方案),Clean(清理解决方案)
为了方便打包测试,自己PowerShell写了一个编译和发布的脚本,调用msbuild通过命令行来编译当前解决方案 后来发现一个问题,用VS编译解决方案,我通过 项目属性-Build设置 Releas ...
- 时间格式yyyyMMddHHmmss的大小写,和字母含义
字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996 ; 96 M 年中的月份 Month July ; Jul ; 07 w 年中的周数 Numb ...
- MyBatis(四):mybatis中使用in查询时的注意事项
准备工作 1)创建测试表jobitem CREATE TABLE "jobitem" ( "id" ) NOT NULL AUTO_INCREMENT COMM ...
- SpringBoot(十一):springboot2.0.2下配置mybatis generator环境,并自定义字段/getter/settetr注释
Mybatis Generator是供开发者在mybatis开发时,快速构建mapper xml,mapper类,model类的一个插件工具.它相对来说对开发者是有很大的帮助的,但是它也有不足之处,比 ...
- 解决Android Studio出现GC overhead limit exceeded
方法一: 修改项目目录下的gradle.properties,增加如下配置信息(红色文字中需要根据自己电脑的配置修改内存大小,其余的配置用于加快gradle的编译速度) org.gradle.daem ...
- Docker 管理工具 Portainer部署
Docker 管理工具 Portainer部署 一.官网 官网:http://www.portainer.io 演示地址:http://demo.portainer.io 用户名:admin 密码:t ...
- Deep Learning.ai学习笔记_第一门课_神经网络和深度学习
目录 前言 第一周(深度学习引言) 第二周(神经网络的编程基础) 第三周(浅层神经网络) 第四周(深层神经网络) 前言 目标: 掌握神经网络的基本概念, 学习如何建立神经网络(包含一个深度神经网络), ...
- go微服务框架go-micro深度学习(一) 整体架构介绍
产品嘴里的一个小项目,从立项到开发上线,随着时间和需求的不断激增,会越来越复杂,变成一个大项目,如果前期项目架构没设计的不好,代码会越来越臃肿,难以维护,后期的每次产品迭代上线都会牵一发而动全身.项目 ...
- php 日期处理 DateTime
获取所有的时区: print_r(timezone_abbreviations_list ()); 获取毫秒级时间戳 // php7.1+ always has microseconds enable ...