Father Christmas flymouse
|
Father Christmas flymouse
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 Sample Output 35 Hint 32-bit signed integer type is capable of doing all arithmetic. Source POJ Monthly--2006.12.31, Semp
|
【思路】
最大点权值路径 tarjian缩点+spfa
缩点的权值只记录正的值。
【code】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define ME 150005
#define NM 500009
vector<int>vec[NM];
int n,m,val[NM],head[NM],dfn[NM],low[NM],u,v;
int col[NM],belong[NM],sumcol,ans,in[NM],out[NM];
int sumedge,stack[NM],instack[NM],dis[NM],inq[NM];
int tim,top;
struct Edge {
int x,y,nxt;
Edge(int x=,int y=,int nxt=):
x(x),y(y),nxt(nxt) {}
} edge[ME];
void add(int x,int y)
{
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
}
void fir()
{
memset(head,,sizeof(head));
memset(stack,,sizeof(stack));
memset(instack,,sizeof(instack));
memset(dis,,sizeof(dis));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(out,,sizeof(out));
memset(in,,sizeof(in));
memset(inq,,sizeof(inq));
for(int i=; i<n; i++)
vec[i].clear();
top=;
sumedge=;
sumcol=;
tim=;
}
void tarjian(int x)
{
dfn[x]=low[x]=++tim;
stack[top++]=x;
instack[x]=;
for(int i=head[x]; i; i=edge[i].nxt)
if(instack[edge[i].y])
low[x]=min(low[x],dfn[edge[i].y]);
else if(!dfn[edge[i].y]) {
tarjian(edge[i].y);
low[x]=min(low[x],low[edge[i].y]);
} else {
}
if(low[x]==dfn[x]) {
sumcol++;
while(stack[top-]!=x) {
col[stack[top-]]=sumcol;
instack[stack[top-]]=false;
top--;
}
stack[top]=sumcol;
top--;
}
}
void spfa()
{
queue<int>q;
q.push();
inq[]=;
while(!q.empty()) {
int now=q.front();
q.pop();
inq[now]=;
for(int i=vec[now].size()-; i>=; i--) {
int to=vec[now][i];
if(dis[to]<dis[now]+belong[to]) {
dis[to]=dis[now]+belong[to];
if(!inq[to]) {
inq[to]=;
q.push(to);
} }
}
}
}
int main()
{
while(scanf("%d %d",&n,&m)) {
fir();
for(int i=; i<n; i++)
{
scanf("%d",&val[i]);
val[i]=max(val[i],);
} for(int i=; i<=m; i++)
{
scanf("%d %d",&u,&v);
add(u,v);
}
for(int i=; i<n; i++)
if(!dfn[i])tarjian(i);
for(int i=;i<n;i++)cout<<col[i]<<endl;
for(int i=; i<n; i++)
belong[col[i]]+=val[i];
for(int i=; i<n; i++) {
for(int j=head[i]; j; j=edge[j].nxt) {
if(col[i]==col[edge[j].y])continue;
vec[col[i]].push_back(col[edge[i].y]);
out[col[i]]++;
in[col[edge[i].y]]++;
}
}
for(int i=;i<=sumcol;i++)
if(!in[i])vec[].push_back(i);
spfa();
for(int i=; i<=sumcol; i++) {
if(!out[i])ans=max(ans,dis[i]);
}
printf("%d\n",ans);
}
return ;
}
Father Christmas flymouse的更多相关文章
- POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3241 Accep ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- L - Father Christmas flymouse
来源poj3160 After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ...
- poj 3160 Father Christmas flymouse
// 题目描述:从武汉大学ACM集训队退役后,flymouse 做起了志愿者,帮助集训队做一些琐碎的事情,比如打扫集训用的机房等等.当圣诞节来临时,flymouse打扮成圣诞老人给集训队员发放礼物.集 ...
- poj 3160 Father Christmas flymouse【强连通 DAG spfa 】
和上一道题一样,可以用DAG上的动态规划来做,也可以建立一个源点,用spfa来做 #include<cstdio> #include<cstring> #include< ...
- POJ——T3160 Father Christmas flymouse
Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3496 Accepted: 1191 缩点,然后每个新点跑一边SPFA ...
- Father Christmas flymouse--POJ3160Tarjan
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...
- POJ:3160-Father Christmas flymouse
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...
- 【转】Tarjan&LCA题集
转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...
随机推荐
- Struts2学习九----------处理结果类型(input)
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2处理结果类型 - SUCCESS:Action正确的执行完成,返回相应的视图,success是name属性的默认值 - ERROR:表示 ...
- cocos2dx中使用iconv转码(win32,iOS,Android)
首先贴下环境:Win7 64, NDK r8e, libiconv-1.14, cygwin 一 Win32环境配置 Cocos2D-X自带有win32上的iconv库.仅仅须要配置一下就可以使用. ...
- 基于RedHat发行的Apache Tomcat本地提权漏洞
描述 Tomcat最近总想搞一些大新闻,一个月都没到,Tomcat又爆出漏洞.2016年10月11日,网上爆出Tomcat本地提权漏洞,漏洞编号为CVE-2016-5425.此次受到影响的主要是基于R ...
- servletResponse 实用的页面跳转技术和定时刷新技术
package response; import java.io.IOException;import java.util.Random; import javax.servlet.ServletEx ...
- python跳坑手记
解决python同目录报错:import util 改成 from . import util
- redis启动错误-- Creating Server TCP listening socket *:6379: listen: UnKnown error
前提:windows server 2008.redis 3.x 今天给服务器部署redis环境,文件配置.服务安装都很顺利,可就在启动服务的时候提示 百度老半天也没找到个说到点子上的. 这里记录下解 ...
- css jquery 实现轮播效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- jvm堆查看
jps查看jvm的进程号 jmap -histo:live [进程号] >log.txt dump jvm堆.
- WePY根据环境变量来改变运行时的参数
WePY根据环境变量来改变运行时的参数 · Tencent/wepy Wiki https://github.com/Tencent/wepy/wiki/WePY%E6%A0%B9%E6%8D%AE% ...
- Kubernetes TensorFlow 默认 特定 集群管理器
Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...