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

2 2
14
21
0 1
1 0

Sample Output

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值。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#define maxn 30000 + 3000
#define maxm 150000 + 15000
#define INF 0x3f3f3f3f
using namespace std;
int n, m; struct node {
int u, v, next;
}; node edge[maxn];
int head[maxn], cnt;
int low[maxn], dfn[maxn];
int dfs_clock;
int Stack[maxn], top;
bool Instack[maxn];
int Belong[maxn];
int scc_clock;
int num[maxn];//记录每一个点的舒适度
vector<int>scc[maxn]; void initedge(){
cnt = 0;
memset(head, -1, sizeof(head));
} void addedge(int u, int v){
edge[cnt] = {u, v, head[u]};
head[u] = cnt++;
} void getmap(){
for(int i = 1; i <= n; ++i)
scanf("%d", &num[i]);
while(m--){
int a, b;
scanf("%d%d", &a, &b);
a++, b++;
addedge(a, b);
}
} void Tarjan(int u, int per){
int v;
low[u] = dfn[u] = ++dfs_clock;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(!dfn[v]){
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(dfn[u] == low[u]){
scc_clock++;
scc[scc_clock].clear();
do{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc_clock;
scc[scc_clock].push_back(v);
}
while( v != u);
}
} void find(){
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
memset(Belong, 0, sizeof(Belong));
memset(Stack, 0, sizeof(Stack));
memset(Instack, false, sizeof(false));
dfs_clock = scc_clock = top = 0;
for(int i = 1; i <= n ; ++i){
if(!dfn[i])
Tarjan(i, i);
}
} struct NODE {
int u, v, w, next;
}; NODE Map[maxn];
int head1[maxn], cnt1;
int vis[maxn], dist[maxn];
int sumnum[maxn];//记录每一个缩点能得到的最大舒适度 void initMap(){
cnt1 = 0;
memset(head1, -1, sizeof(head1));
} void addMap(int u, int v, int w){
Map[cnt1] = {u, v, w, head1[u]};
head1[u] = cnt1++;
} //求出每一个缩点能得到的最大舒适度
//作为到达这个缩点的权值
void change(){
memset(sumnum, 0, sizeof(sumnum));
for(int i = 1; i <= scc_clock; ++i){
for(int j = 0; j < scc[i].size(); ++j){
if(num[scc[i][j]] > 0)
sumnum[i] += num[scc[i][j]];
}
}
} void suodian(){//缩点 && 新建图
for(int i = 0; i < cnt; ++i){
int u = Belong[edge[i].u];
int v = Belong[edge[i].v];
if(u != v){
addMap(u, v, sumnum[v]);
}
}
for(int i = 1; i <= scc_clock; ++i)
addMap(0, i, sumnum[i]); //建立虚拟源点
} //SPFA跑最长路。求出到达每一个缩点的所能得到的最大舒适度
void SPFA(int x){
queue<int>q;
for(int i = 0; i <= scc_clock; ++i){
dist[i] = -INF;
vis[i] = 0;
}
vis[x] = 1;
dist[x] = 0;
q.push(x);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i = head1[u]; i != -1; i = Map[i].next){
int v = Map[i].v;
int w = Map[i].w;
if(dist[v] < dist[u] + w){
dist[v] = dist[u] + w;
if(!vis[v]){
vis[v] = 1;
q.push(v);
}
}
}
}
} int main(){
while(scanf("%d%d", &n, &m) != EOF){
initedge();
getmap();
find();
change();
initMap();
suodian();
SPFA(0);
sort(dist, dist + scc_clock + 1);
printf("%d\n", dist[scc_clock]);
}
return 0;
}

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. python(47):range和xrange的区别和联系

    range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列. 比如: >>> range(5)[0, 1, 2, ...

  2. 【Java】IO Stream详细解读

    成鹏致远 | 2013年12月31日 什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流 ...

  3. Hive中小表与大表关联(join)的性能分析【转】

    Hive中小表与大表关联(join)的性能分析 [转自:http://blog.sina.com.cn/s/blog_6ff05a2c01016j7n.html] 经常看到一些Hive优化的建议中说当 ...

  4. 【机器学习】主成分分析PCA(Principal components analysis)

    1. 问题 真实的训练数据总是存在各种各样的问题: 1. 比如拿到一个汽车的样本,里面既有以“千米/每小时”度量的最大速度特征,也有“英里/小时”的最大速度特征,显然这两个特征有一个多余. 2. 拿到 ...

  5. iOS开发:代码通用性以及其规范 第二篇(猜想iOS中实现TableView内部设计思路(附代码),以类似的思想实现一个通用的进度条)

    在iOS开发中,经常是要用到UITableView的,我曾经思考过这样一个问题,为什么任何种类的model放到TableView和所需的cell里面,都可以正常显示?而我自己写的很多view却只是能放 ...

  6. 【Unity】使用JSONObject解析Json

    为何要用JSONObject 之前已经用过JsonUtility和Newton.Json来解析Json了,为什么现在又要用一个新的JSONObject来解析Json? 使用JsonUtility:ht ...

  7. C#绘制三角形并填充,使用winform实现qq聊天气泡

    首先是需求,需要制作一个聊天气泡, 但是winform中有没有类似Android的.9图,只有自己设计图形拼接气泡. 第一种是绘制空心三角形,第二种是绘制三角形区域,可以指定RGB颜色. privat ...

  8. Java编程的逻辑 (54) - 剖析Collections - 设计模式

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  9. kafka中的消费组

    一直以来都想写一点关于kafka consumer的东西,特别是关于新版consumer的中文资料很少.最近Kafka社区邮件组已经在讨论是否应该正式使用新版本consumer替换老版本,笔者也觉得时 ...

  10. ssh登陆过程图示