【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse
来源:互联网 发布:在北大读书的体验知乎 编辑:程序博客网 时间:2024/06/05 13:27
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
题意: 给出一张有向图,图上各点有权值,权值可能为负,选择图上一个顶点出发,每个点的权值可以选择加或者不加1,求可能得到的最大权值和。
思路: 首先对于一个强连通分量,从任意一个点进入都可以到达分量中所有的点,因此先用Tarjan算法进行缩点。由于负权点根本不用加上权值,因此负权点权值设为02。
然后尝试对每个入度为0的点进行dfs,求出最大值。
代码如下:
/* * ID: j.sure.1 * PROG: * LANG: C++ */#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define PB push_back#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;const double eps = 1e-8;/****************************************/const int N = 3e4 + 5, M = 15e4 + 5;struct Edge { int v, next; Edge(){} Edge(int _v, int _next): v(_v), next(_next){}}e[M];int tot, deep, scc_cnt, n, m;int head[N], dfn[N], scc_id[N], w[N];int line[M][2], in[N], sum[N];stack <int> s;vector <int> ee[N];void init(){ tot = deep = scc_cnt = 0; memset(head, -1, sizeof(head)); memset(dfn, 0, sizeof(dfn)); memset(scc_id, 0, sizeof(scc_id)); memset(in, 0, sizeof(in)); memset(sum, 0, sizeof(sum));}void add(int u, int v){ e[tot] = Edge(v, head[u]); head[u] = tot++;}int dfs(int u){ int lowu = dfn[u] = ++deep; s.push(u); for(int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; if(!dfn[v]) { int lowv = dfs(v); lowu = min(lowu, lowv); } else if(!scc_id[v]) { lowu = min(lowu, dfn[v]); } } if(lowu == dfn[u]) { scc_cnt++; while(1) { int x = s.top(); s.pop(); scc_id[x] = scc_cnt; sum[scc_cnt] += w[x]; if(x == u) break; } } return lowu;}void tarjan(){ for(int i = 0; i < n; i++) { if(!dfn[i]) dfs(i); }}int dfs2(int u, int tmp){ int sz = ee[u].size(); int maxi = tmp; for(int i = 0; i < sz; i++) { int v = ee[u][i]; maxi = max(maxi, dfs2(v, tmp + sum[v])); } return maxi;}int solve(){ int ans = 0; for(int i = 1; i <= scc_cnt; i++) ee[i].clear(); for(int i = 0; i < m; i++) { int u = scc_id[line[i][0]], v = scc_id[line[i][1]]; if(u != v) { in[v]++; ee[u].PB(v); } } for(int i = 1; i <= scc_cnt; i++) { if(!in[i]) { ans = max(ans, dfs2(i, sum[i])); } } return ans;}int main(){#ifdef J_Sure freopen("000.in", "r", stdin); //freopen("999.out", "w", stdout);#endif while(~scanf("%d%d", &n, &m)) { init(); for(int i = 0; i < n; i++) { scanf("%d", &w[i]); if(w[i] < 0) w[i] = 0; } int u, v; for(int i = 0; i < m; i++) { scanf("%d%d", &u, &v); add(u, v); line[i][0] = u; line[i][1] = v; } tarjan(); int ans = solve(); printf("%d\n", ans); } return 0;}
- 题意描述不清楚,假如整张图都是负权,答案是0。 ↩
- 不得不怀疑出题人是否脑残。 ↩
- 【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse
- poj 3160 Father Christmas flymouse (强连通分量+记忆化搜素)
- poj 3160 Father Christmas flymouse 强连通分量
- poj 3160-Father Christmas flymouse-强连通
- POJ 3160 Father Christmas flymouse 强连通分量+缩点+DP
- POJ - 3160 Father Christmas flymouse(最长路+强连通分量)
- POJ 3160 Father Christmas flymouse(强连通分量+spfa最长路)
- poj 3160 Father Christmas flymouse(强连通+dp)
- 【POJ】3160 Father Christmas flymouse 强连通+最长路
- POJ 3160 Father Christmas flymouse(强连通+DP)
- poj 3160 Father Christmas flymouse 强连通+dp
- POJ 3160 — Father Christmas flymouse 强连通+spfa
- POJ 3160 Father Christmas flymouse 强连通+最长路
- POJ 3160 Father Christmas flymouse 强连通+Spfa
- Poj 3160 Father Christmas flymouse【强连通Tarjan+SPFA+超级源点】
- POJ3160 Father Christmas flymouse 强连通tarjan算法+dfs+简单dp
- POJ 3160 Father Christmas flymouse 强联通
- POJ 3160 Father Christmas flymouse
- TCP/IP三次握手与四次挥手
- java 静态方法与实例方法的区别
- 机器学习知识导入
- Vijos-谁拿了最多的奖学金
- 斯皮尔曼等级相关
- 【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse
- Git学习笔记
- C++ primer中的一些概念和编程时应注意的问题
- html5 进度条效果<progress>
- 安卓第二记
- Python中任意函数参数问题
- java 抓取网页图片并保存
- Ubuntu14.4 及 对应 VBox 和 扩展包 下载
- Gson简要使用笔记