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

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

思路:对于一个有向图,图中的强连通一定可以相互抵达,所以Flymouse可以访问强连通分量中的任意元素,对于集合中的负值不要,只要正值就可以保证得到的值最大,所以我们将强连通缩点后形成一个DAG图,搜索一下就可以得到最大值。


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <algorithm> using namespace std; const int Max = 30010; vector<int>Map[Max]; vector<int>G[Max]; vector<int>P[Max]; int va[Max]; //节点价值 int dfn[Max],low[Max],vis[Max],dep;//遍历的顺序,回溯,标记,遍历的顺序。 int pre[Max],num,a[Max];// 集合,数目,集合价值 stack<int>S; int n,m; void init() //初始化
{
for(int i=0;i<=n;i++)
{
Map[i].clear(); P[i].clear(); G[i].clear();
} memset(vis,0,sizeof(vis)); memset(a,0,sizeof(a)); dep = 0 ; num = 0;
} void Tarjan(int u)
{
dfn[u] = low[u] =dep++; vis[u]=1; S.push(u); for(int i=0;i<Map[u].size();i++)
{
if(vis[Map[u][i]]==1)
{
low[u] = min(low[u],dfn[Map[u][i]]);
} else if(vis[Map[u][i]]==0)
{
Tarjan(Map[u][i]); low[u] = min(low[u],low[Map[u][i]]);
}
} if(dfn[u]==low[u])
{
while(!S.empty()) //缩点
{
int v = S.top(); S.pop(); pre[v] = num; vis[v] = 2; a[num]+=va[v]; G[num].push_back(v);//记录集合的点 if(u==v)
{
break;
}
}
num++;
}
} int dfs(int u)
{
if(!vis[u])
{
int ans = 0; vis[u]=1; for(int i=0;i<P[u].size();i++)
{
ans = max(ans,dfs(P[u][i]));
} a[u] += ans ;
} return a[u];
} int main()
{
while(~scanf("%d %d",&n,&m))
{ init(); for(int i=0;i<n;i++) //先输入价值
{
scanf("%d",&va[i]); va[i]=va[i]<0?0:va[i];//小于零的归零,为不访问
} int u,v; for(int i=0;i<m;i++) //建图
{
scanf("%d %d",&u,&v); Map[u].push_back(v);
} for(int i=0;i<n;i++)//强连通缩点
{
if(vis[i]==0)//从未被遍历的点搜索
{
Tarjan(i);
}
} for(int i=0;i<num;i++) //重新建图
{
memset(vis,0,sizeof(vis)); for(int j=0;j<G[i].size();j++)
{
int u=G[i][j];//集合中的点 for(int k=0;k<Map[u].size();k++)
{
if(pre[Map[u][k]] != i && !vis[pre[Map[u][k]]])
{
P[i].push_back(pre[Map[u][k]]); vis[pre[Map[u][k]]] = 1;
}
}
}
} int ans= 0 ; memset(vis,0,sizeof(vis)); for(int i=0;i<num;i++)//搜索最大的值
{
ans = max(ans,dfs(i));
} printf("%d\n",ans); }
return 0;
}

Father Christmas flymouse--POJ3160Tarjan的更多相关文章

  1. POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3241   Accep ...

  2. POJ 3126 --Father Christmas flymouse【scc缩点构图 &amp;&amp; SPFA求最长路】

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3007   Accep ...

  3. Father Christmas flymouse

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3479   Accep ...

  4. L - Father Christmas flymouse

    来源poj3160 After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ...

  5. poj 3160 Father Christmas flymouse

    // 题目描述:从武汉大学ACM集训队退役后,flymouse 做起了志愿者,帮助集训队做一些琐碎的事情,比如打扫集训用的机房等等.当圣诞节来临时,flymouse打扮成圣诞老人给集训队员发放礼物.集 ...

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

    和上一道题一样,可以用DAG上的动态规划来做,也可以建立一个源点,用spfa来做 #include<cstdio> #include<cstring> #include< ...

  7. POJ——T3160 Father Christmas flymouse

    Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3496   Accepted: 1191 缩点,然后每个新点跑一边SPFA ...

  8. POJ:3160-Father Christmas flymouse

    Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...

  9. 【转】Tarjan&LCA题集

    转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...

随机推荐

  1. rabbitmq method之basic.consume

    basic.consume指的是channel在 某个队列上注册消费者,那在这个队列有消息来了之后,就会把消息转发到给此channel处理,如果 这个队列有多个消费者,则会采用轮转的方式将消息分发给消 ...

  2. 使用dbghelp生成dump文件以及事后调试分析

    前言 在产品的实际应用环境中,如果我们的程序在客户那里出现了问题,例如程序异常了,而这个时候的现象又不能还原或者很难还原重现,那么只有使用dump文件来保存程序的当前运行信息,例如调用堆栈等,同时使用 ...

  3. Glide请求图片能携带Cookie的哟!

    在Web编程中我们都很熟知一个概念,当有了seesion登录状态时,你可以访问一些资源但如果你没有登录的话很多资源是无法访问的. 在android的WebApi中当然一样拥有这个概念.比如,用户的头像 ...

  4. QList

    #include <QCoreApplication> #include<QList> #include<QDebug> int main(int argc, ch ...

  5. DWR实现后台推送消息到web页面

    DWR简介 DWR(Direct Web Remoting)可用于实现javascript直接调用java函数和后台直接调用页面javascript代码,后者可用作服务端推送消息到Web前端. (服务 ...

  6. MySQL的几个概念:主键,外键,索引,唯一索引

    概念: 主键(primary key) 能够唯一标识表中某一行的属性或属性组.一个表只能有一个主键,但可以有多个候选索引.主键常常与外键构成参照完整性约束,防止出现数据不一致.主键可以保证记录的唯一和 ...

  7. 简介 jCanvas:当 jQuery遇上HTML5 Canvas

    https://github.com/caleb531/jcanvas HTML5 可以直接在你的网页中使用 <canvas> 元素及其相关的 JavaScript API绘制的图形. 在 ...

  8. VS2013 ViewData ViewBag Ajax等关键词报错(当前上下文不存在名称)而且不提示也点不出来,但是可以正常运行,

    这个多数问题是因为 视图 的Web.config 内的配置问题 在Views文件夹下  有一个Web.config文件,把里面的版本号(System.Web.Mvc, Version=5.2.2.0) ...

  9. eclipse自动补全快捷键失效,sysout用不了!

    好久没写Java代码了,使用新版Neon的Eclipse Java EE IDE开发时,自动补全各种失败,sysout也各种用不了, 开始还以为是电脑卡比呢,原来是版本的快捷键不同了,修改方法如下! ...

  10. SQLServer中系统存储过程sp_spaceused

    sp_spaceused 执行sp_spaceused存储过程的时候可以不用带参数,直接执行,或者exec sp_spaceused都可以,返回两个结果集: 列名 数据类型 描述 database_n ...