Father Christmas flymouse
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 3479   Accepted: 1185

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

  1. 2 2
  2. 14
  3. 21
  4. 0 1
  5. 1 0

Sample Output

  1. 35

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

【思路】

最大点权值路径 tarjian缩点+spfa

缩点的权值只记录正的值。

【code】

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<queue>
  7. using namespace std;
  8. #define ME 150005
  9. #define NM 500009
  10. vector<int>vec[NM];
  11. int n,m,val[NM],head[NM],dfn[NM],low[NM],u,v;
  12. int col[NM],belong[NM],sumcol,ans,in[NM],out[NM];
  13. int sumedge,stack[NM],instack[NM],dis[NM],inq[NM];
  14. int tim,top;
  15. struct Edge {
  16. int x,y,nxt;
  17. Edge(int x=,int y=,int nxt=):
  18. x(x),y(y),nxt(nxt) {}
  19. } edge[ME];
  20. void add(int x,int y)
  21. {
  22. edge[++sumedge]=Edge(x,y,head[x]);
  23. head[x]=sumedge;
  24. }
  25. void fir()
  26. {
  27. memset(head,,sizeof(head));
  28. memset(stack,,sizeof(stack));
  29. memset(instack,,sizeof(instack));
  30. memset(dis,,sizeof(dis));
  31. memset(dfn,,sizeof(dfn));
  32. memset(low,,sizeof(low));
  33. memset(out,,sizeof(out));
  34. memset(in,,sizeof(in));
  35. memset(inq,,sizeof(inq));
  36. for(int i=; i<n; i++)
  37. vec[i].clear();
  38. top=;
  39. sumedge=;
  40. sumcol=;
  41. tim=;
  42. }
  43. void tarjian(int x)
  44. {
  45. dfn[x]=low[x]=++tim;
  46. stack[top++]=x;
  47. instack[x]=;
  48. for(int i=head[x]; i; i=edge[i].nxt)
  49. if(instack[edge[i].y])
  50. low[x]=min(low[x],dfn[edge[i].y]);
  51. else if(!dfn[edge[i].y]) {
  52. tarjian(edge[i].y);
  53. low[x]=min(low[x],low[edge[i].y]);
  54. } else {
  55. }
  56. if(low[x]==dfn[x]) {
  57. sumcol++;
  58. while(stack[top-]!=x) {
  59. col[stack[top-]]=sumcol;
  60. instack[stack[top-]]=false;
  61. top--;
  62. }
  63. stack[top]=sumcol;
  64. top--;
  65. }
  66. }
  67. void spfa()
  68. {
  69. queue<int>q;
  70. q.push();
  71. inq[]=;
  72. while(!q.empty()) {
  73. int now=q.front();
  74. q.pop();
  75. inq[now]=;
  76. for(int i=vec[now].size()-; i>=; i--) {
  77. int to=vec[now][i];
  78. if(dis[to]<dis[now]+belong[to]) {
  79. dis[to]=dis[now]+belong[to];
  80. if(!inq[to]) {
  81. inq[to]=;
  82. q.push(to);
  83. }
  84.  
  85. }
  86. }
  87. }
  88. }
  89. int main()
  90. {
  91. while(scanf("%d %d",&n,&m)) {
  92. fir();
  93. for(int i=; i<n; i++)
  94. {
  95. scanf("%d",&val[i]);
  96. val[i]=max(val[i],);
  97. }
  98.  
  99. for(int i=; i<=m; i++)
  100. {
  101. scanf("%d %d",&u,&v);
  102. add(u,v);
  103. }
  104. for(int i=; i<n; i++)
  105. if(!dfn[i])tarjian(i);
  106. for(int i=;i<n;i++)cout<<col[i]<<endl;
  107. for(int i=; i<n; i++)
  108. belong[col[i]]+=val[i];
  109. for(int i=; i<n; i++) {
  110. for(int j=head[i]; j; j=edge[j].nxt) {
  111. if(col[i]==col[edge[j].y])continue;
  112. vec[col[i]].push_back(col[edge[i].y]);
  113. out[col[i]]++;
  114. in[col[edge[i].y]]++;
  115. }
  116. }
  117. for(int i=;i<=sumcol;i++)
  118. if(!in[i])vec[].push_back(i);
  119. spfa();
  120. for(int i=; i<=sumcol; i++) {
  121. if(!out[i])ans=max(ans,dis[i]);
  122. }
  123. printf("%d\n",ans);
  124. }
  125. return ;
  126. }

Father Christmas flymouse的更多相关文章

  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. L - Father Christmas flymouse

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

  4. poj 3160 Father Christmas flymouse

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

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

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

  6. POJ——T3160 Father Christmas flymouse

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

  7. Father Christmas flymouse--POJ3160Tarjan

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

  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. Django之便签生成

    myblog_tag.py #coding:utf-8 __author__ = 'similarface'from django import template register=template. ...

  2. 利用xlrd模块实现Python读取Excel文档

    # -*- coding: cp936 -*- #python读取excel import xlrd def main(): xls=xlrd.open_workbook("d:\\11.x ...

  3. 计算机网络 --万维网www

    万维网是一个分布式的超媒体系统,客户程序向服务器程序发出请求,服务器程序向客户程序送回客户所需要的万维网文档.万维网必须解决的几个问题:1.怎样标志分布在整个因特网上的万维网文档?答:万维网使用统一的 ...

  4. JavaScript 原型解析

    1.什么是对象?     javascript中除了null和undefined之外都是Object的实例. 在Javascript中, 每定义一个函数, 将伴生一个原型对象. 原型就是用来为同一类对 ...

  5. android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件

    主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...

  6. windows下的常用命令

    net start ... 启动某个服务 net stop ... 停止某个服务 net start     查看所有启动的服务 services.msc  打开服务的界面 ipconfig     ...

  7. 九度OJ 1159:坠落的蚂蚁 (模拟、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1098 解决:277 题目描述: 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如 ...

  8. 九度OJ 1155:鸡兔同笼 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2032 解决:1369 题目描述: 一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外).已经知道了笼子里面脚的总数a,问笼子里面 ...

  9. Learning an Optimal Policy: Model-free Methods

    http://www.mit.edu/~9.54/fall14/slides/Reinforcement%20Learning%202-Model%20Free.pdf [基于所有.单个样本]

  10. 我的Android进阶之旅------>Android如何通过自定义SeekBar来实现视频播放进度条

    首先来看一下效果图,如下所示: 其中进度条如下: 接下来说一说我的思路,上面的进度拖动条有自定义的Thumb,在Thumb正上方有一个PopupWindow窗口,窗口里面显示当前的播放时间.在Seek ...