POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 3241 | Accepted: 1099 |
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
题意:多了每个节点分配一个权值,走一条路权值最大
还是求SCC缩点DP
//16ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define C(x) memset(x,0,sizeof(x))
using namespace std;
const int N=,M=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,u,v,w[N];
struct edge{
int v,ne;
}e[M];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
int dfn[N],low[N],belong[N],dfc,scc,val[N];
int st[N],top;
void dfs(int u){
dfn[u]=low[u]=++dfc;
st[++top]=u;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!dfn[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}else if(!belong[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
scc++;
while(true){
int x=st[top--];
belong[x]=scc;
if(w[x]>) val[scc]+=w[x];
if(x==u) break;
}
}
}
void SCC(){
dfc=scc=;
C(dfn);C(low);C(belong);C(val);
top=;
for(int i=;i<=n;i++) if(!dfn[i]) dfs(i);
} edge es[N];
int hs[N],cs=;
inline void inss(int u,int v){
cs++;
es[cs].v=v;es[cs].ne=hs[u];hs[u]=cs;
}
void buildGraph(){
cs=;
C(hs);
for(int u=;u<=n;u++){
int a=belong[u];
for(int i=h[u];i;i=e[i].ne){
int b=belong[e[i].v];
if(a!=b)inss(a,b);
}
}
}
int f[N];
int dp(int u){
if(f[u]!=-) return f[u];
f[u]=val[u];
int mx=;
for(int i=hs[u];i;i=es[i].ne){
int v=es[i].v;//printf("dp %d v %d\n",u,v);
mx=max(mx,dp(v));
}
return f[u]+=mx;
}
int main(int argc, const char * argv[]) {
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++) w[i]=read();
cnt=;memset(h,,sizeof(h));
for(int i=;i<=m;i++){u=read()+;v=read()+;ins(u,v);}
SCC();
buildGraph(); memset(f,-,sizeof(f));
int ans=;
for(int i=;i<=scc;i++){
if(f[i]==-) dp(i);
ans=max(ans,f[i]);
}
printf("%d\n",ans);
} return ;
}
POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]的更多相关文章
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- BZOJ 1179 Atm(强连通分量缩点+DP)
题目说可以通过一条边多次,且点权是非负的,所以如果走到图中的一个强连通分量,那么一定可以拿完这个强连通分量上的money. 所以缩点已经很明显了.缩完点之后图就是一个DAG,对于DAG可以用DP来求出 ...
- UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP
题目链接:https://vjudge.net/problem/UVA-11324 题解: 题意:给出一张有向图,求一个结点数最大的结点集,使得任意两个结点u.v,要么u能到达v, 要么v能到达u(u ...
- UVA11324 The Largest Clique[强连通分量 缩点 DP]
UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...
- Father Christmas flymouse
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3479 Accep ...
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
- POJ1236Network of Schools(强连通分量 + 缩点)
题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...
- HD2767Proving Equivalences(有向图强连通分量+缩点)
题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...
- UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)
题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...
随机推荐
- Xamarin android 之Activity详解
序言: 上篇大概的讲解了新建一个android的流程.今天为大家带来的是Activity详解,因为自己在开发过程中就遇到 好几次坑,尴尬. 生命周期 和Java里头一样一样的,如图 图片来源于网上哈, ...
- 数据见50条常用sql
问题及描述: --1.学生表 Student(Sid,Sname,Sage,Ssex) --Sid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Course ...
- MongoDB安全和认证
1.每个MongoDB实例中的数据库都可以有许多用户.如果开启了安全性检查,则只有数据库认证用户才能执行读或者写操作. 在认证的上下文中,MongoDB会将普通的数据作为admin数据库处理.admi ...
- 从头开始搭建一个dubbo+zookeeper平台
本篇主要是来分享从头开始搭建一个dubbo+zookeeper平台的过程,其中会简要介绍下dubbo服务的作用. 首先,看下一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多之后 ...
- WindowsAPI调用和OCR图片识别
傻了吧唧的装双系统.成功的干崩了原本的系统.现在重装VS.闲的没事胡扯几句. WindowsAPI在每一台Windows系统上开放标准API供开发人员调用.功能齐全.在这里只介绍三个部分. 1.利用A ...
- 有问题得就分享(此实现不是 Windows 平台 FIPS 验证的加密算法的一部分)
今天呢,我写完了MD5加密算法的代码,可是我正高兴的想运行时,按了F5.突然了个这样的窗体 我当时就纳闷了,怎么回事? 代码又没有错,这是乍回事! 于是呢上网查了看看:可以这样解决的 在window中 ...
- JS操作未跨域iframe里的DOM
这里简单说明两个方法,都是未跨域情况下在index.html内操作b.html内的 DOM. 如:index.html内引入iframe,在index内如何用JS操作iframe内的DOM元素? 先贴 ...
- 【blade的UI设计】理解前端MVC与分层思想
前言 最近校招要来了,很多大三的同学一定按捺不住心中的焦躁,其中有期待也有彷徨,或许更多的是些许担忧,最近在开始疯狂的复习了吧 这里小钗有几点建议给各位: ① 不要看得太重,关心则乱,太紧张反而表现不 ...
- Linux入侵检测常用命令
find / -mtime 0 #0代表目前时间,表示从现在开始到24小时以前,有改动过内容的文件全都会被列出来.如果是3天前24小时内,则使用find / -mtime 3 find /etc -n ...
- reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector.
album = responseObject[@"album"]; 是我将一个字典直接赋值给了对象 改为如下即可 [album setValuesForKeysWithDicti ...