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

  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.

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

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

详细思路:

思路:用sumnum数组记录每一个SCC的最大舒适指数,用dist数组存储虚拟源点0到节点的最远距离。

一:对有向图求出全部的SCC。

二:求出每一个SCC的sumnum值。(累加全部为正的舒适指数)

三:缩点并对每<u。v>边 建立边权sumnum [v]。

四:设置虚拟源点0。连接全部的SCC。边权为相应SCC的sumnum值。

五:以0为源点跑一次SPFA。对dist数组排序后,输出最大的dist值。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <queue>
  6. #define maxn 30000 + 3000
  7. #define maxm 150000 + 15000
  8. #define INF 0x3f3f3f3f
  9. using namespace std;
  10. int n, m;
  11.  
  12. struct node {
  13. int u, v, next;
  14. };
  15.  
  16. node edge[maxn];
  17. int head[maxn], cnt;
  18. int low[maxn], dfn[maxn];
  19. int dfs_clock;
  20. int Stack[maxn], top;
  21. bool Instack[maxn];
  22. int Belong[maxn];
  23. int scc_clock;
  24. int num[maxn];//记录每一个点的舒适度
  25. vector<int>scc[maxn];
  26.  
  27. void initedge(){
  28. cnt = 0;
  29. memset(head, -1, sizeof(head));
  30. }
  31.  
  32. void addedge(int u, int v){
  33. edge[cnt] = {u, v, head[u]};
  34. head[u] = cnt++;
  35. }
  36.  
  37. void getmap(){
  38. for(int i = 1; i <= n; ++i)
  39. scanf("%d", &num[i]);
  40. while(m--){
  41. int a, b;
  42. scanf("%d%d", &a, &b);
  43. a++, b++;
  44. addedge(a, b);
  45. }
  46. }
  47.  
  48. void Tarjan(int u, int per){
  49. int v;
  50. low[u] = dfn[u] = ++dfs_clock;
  51. Stack[top++] = u;
  52. Instack[u] = true;
  53. for(int i = head[u]; i != -1; i = edge[i].next){
  54. int v = edge[i].v;
  55. if(!dfn[v]){
  56. Tarjan(v, u);
  57. low[u] = min(low[u], low[v]);
  58. }
  59. else if(Instack[v])
  60. low[u] = min(low[u], dfn[v]);
  61. }
  62. if(dfn[u] == low[u]){
  63. scc_clock++;
  64. scc[scc_clock].clear();
  65. do{
  66. v = Stack[--top];
  67. Instack[v] = false;
  68. Belong[v] = scc_clock;
  69. scc[scc_clock].push_back(v);
  70. }
  71. while( v != u);
  72. }
  73. }
  74.  
  75. void find(){
  76. memset(low, 0, sizeof(low));
  77. memset(dfn, 0, sizeof(dfn));
  78. memset(Belong, 0, sizeof(Belong));
  79. memset(Stack, 0, sizeof(Stack));
  80. memset(Instack, false, sizeof(false));
  81. dfs_clock = scc_clock = top = 0;
  82. for(int i = 1; i <= n ; ++i){
  83. if(!dfn[i])
  84. Tarjan(i, i);
  85. }
  86. }
  87.  
  88. struct NODE {
  89. int u, v, w, next;
  90. };
  91.  
  92. NODE Map[maxn];
  93. int head1[maxn], cnt1;
  94. int vis[maxn], dist[maxn];
  95. int sumnum[maxn];//记录每一个缩点能得到的最大舒适度
  96.  
  97. void initMap(){
  98. cnt1 = 0;
  99. memset(head1, -1, sizeof(head1));
  100. }
  101.  
  102. void addMap(int u, int v, int w){
  103. Map[cnt1] = {u, v, w, head1[u]};
  104. head1[u] = cnt1++;
  105. }
  106.  
  107. //求出每一个缩点能得到的最大舒适度
  108. //作为到达这个缩点的权值
  109. void change(){
  110. memset(sumnum, 0, sizeof(sumnum));
  111. for(int i = 1; i <= scc_clock; ++i){
  112. for(int j = 0; j < scc[i].size(); ++j){
  113. if(num[scc[i][j]] > 0)
  114. sumnum[i] += num[scc[i][j]];
  115. }
  116. }
  117. }
  118.  
  119. void suodian(){//缩点 && 新建图
  120. for(int i = 0; i < cnt; ++i){
  121. int u = Belong[edge[i].u];
  122. int v = Belong[edge[i].v];
  123. if(u != v){
  124. addMap(u, v, sumnum[v]);
  125. }
  126. }
  127. for(int i = 1; i <= scc_clock; ++i)
  128. addMap(0, i, sumnum[i]); //建立虚拟源点
  129. }
  130.  
  131. //SPFA跑最长路。求出到达每一个缩点的所能得到的最大舒适度
  132. void SPFA(int x){
  133. queue<int>q;
  134. for(int i = 0; i <= scc_clock; ++i){
  135. dist[i] = -INF;
  136. vis[i] = 0;
  137. }
  138. vis[x] = 1;
  139. dist[x] = 0;
  140. q.push(x);
  141. while(!q.empty()){
  142. int u = q.front();
  143. q.pop();
  144. vis[u] = 0;
  145. for(int i = head1[u]; i != -1; i = Map[i].next){
  146. int v = Map[i].v;
  147. int w = Map[i].w;
  148. if(dist[v] < dist[u] + w){
  149. dist[v] = dist[u] + w;
  150. if(!vis[v]){
  151. vis[v] = 1;
  152. q.push(v);
  153. }
  154. }
  155. }
  156. }
  157. }
  158.  
  159. int main(){
  160. while(scanf("%d%d", &n, &m) != EOF){
  161. initedge();
  162. getmap();
  163. find();
  164. change();
  165. initMap();
  166. suodian();
  167. SPFA(0);
  168. sort(dist, dist + scc_clock + 1);
  169. printf("%d\n", dist[scc_clock]);
  170. }
  171. return 0;
  172. }

POJ 3126 --Father Christmas flymouse【scc缩点构图 &amp;&amp; SPFA求最长路】的更多相关文章

  1. POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accep ...

  2. 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路

    题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...

  3. poj 3160 Father Christmas flymouse

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

  4. POJ——T3160 Father Christmas flymouse

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

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

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

  6. POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 53 ...

  7. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  8. poj 3592 Instantaneous Transference 【SCC +缩点 + SPFA】

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6204   Accep ...

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

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

随机推荐

  1. 关于ddx/ddy重建法线在edge边沿上的artifacts问题

    经验证,原来ddx/ddy这两个操作,在forward rendering与deferred rendering中存在着微妙的应用区别. 在forward rendering中,GPU shader会 ...

  2. (转)Making 1 million requests with python-aiohttp

    转自:https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html Making 1 millio ...

  3. 【Qt】qt库结构及示例

    QT库结构 Qt图形库是一个组织严谨的C++类库,其结构如图所示 细说Qt库 Qt类库中包含了上百个类,结构十分复杂,上图展示了Qt_3.2类库的基本结构. Qt类库中的类可以分成两种类型: 一种是直 ...

  4. vim复制内容到系统剪贴板

    vim提供了y键盘操作用于复制文本,但是复制之后的文本位于当前窗口的缓冲区中,不在系统剪贴板中,这给跨程序文本拷贝代码很来很多麻烦.搜索发现,可以使用]y指令快速将选定的文本复制到系统剪贴板中. 顺便 ...

  5. C# 裁剪图片

    /// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath"& ...

  6. 大数据 -- Spark

    Spark体系架构 zhuangzai Spark体系架构包括如下三个主要组件: 数据存储 API 管理框架 接下来让我们详细了解一下这些组件. 数据存储: Spark用HDFS文件系统存储数据.它可 ...

  7. c语言二分法

    #include <stdio.h> #include <stdlib.h> int Search(int *a,int key) { ,mid; ; while(low< ...

  8. WebAPI Action的几种返回值类型

    void 返回204状态码 HttpResponseMessage Convert directly to an HTTP response message. IHttpActionResult Ca ...

  9. Creating a Physical Standby Database 11g

    1.Environment Item Primary database standby database Platform Redhat 5.4 Redhat 5.4 Hostname gc1 gc2 ...

  10. 数据库like匹配的实现猜测

    insert into test_fulltext values("王正科技全文") select * from test_fulltext where data like &qu ...