Father Christmas flymouse
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 3479   Accepted: 1185

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

【思路】

最大点权值路径 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的更多相关文章

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

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

  2. POJ 3126 --Father Christmas flymouse【scc缩点构图 &amp;&amp; SPFA求最长路】

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

  3. L - Father Christmas flymouse

    来源poj3160 After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ...

  4. poj 3160 Father Christmas flymouse

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

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

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

  6. POJ——T3160 Father Christmas flymouse

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

  7. Father Christmas flymouse--POJ3160Tarjan

    Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...

  8. POJ:3160-Father Christmas flymouse

    Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...

  9. 【转】Tarjan&LCA题集

    转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...

随机推荐

  1. Django之邮件发送

    settings.py #settings 添加如下配置进行邮件发送 #邮件服务器 EMAIL_HOST = "smtp.qq.com" #邮件发送的端口 EMAIL_PORT = ...

  2. YARN/MRv2 中基本术语介绍

    YARN/MRv2是下一代MapReduce框架(见Hadoop-0.23.0),该框架完全不同于当前的MapReduce框架,它在扩展性,容错性和通用性等方面更出色,据统计,Yarn有超过15000 ...

  3. android studio 更新Gradle版本号方法

    在导入其它项目时,常常因为gradle版本号不一致而导致不能编译 解决方法: 第一步: 按提示点击让它下载.事实上目的并非要它下载.因为这样速度会非常慢.这样做仅仅是为了让它在本地创建相应的文件夹结构 ...

  4. 继承ViewGroup类

    Android中,布局都是直接或间接的继承自ViewGroup类,其中,ViewGroup的直接子类目前有: AbsoluteLayout, AdapterView<T extends Adap ...

  5. 自我总结- CGAffineTransform

    在应用中我们经常需要做一些仿射变换 可以用于 平移.旋转.缩放变换路径: View有一个属性transform 可以指定一个 CGAffineTransform 即可完成仿射变换 1.平移变换 // ...

  6. 微信小程序页面之间的跳转

    一.使用标签跳转             index.wxml:             在index.wxml页面添加一个<navigator>元素,在元素里面使用属性url就可以 二. ...

  7. 【百度之星复赛】T5 Valley Numer

    Valley Numer Problem Description 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Number,像山谷一样的数字. 当一个数字,从左到右依次看过 ...

  8. 九度OJ 1020:最小长方形 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6019 解决:2849 题目描述:     给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在 ...

  9. c++动态绑定的技术实现

    1 什么是动态绑定 有一个基类,两个派生类,基类有一个virtual函数,两个派生类都覆盖了这个虚函数.现在有一个基类的指针或者引用,当该基类指针或者引用指向不同的派生类对象时,调用该虚函数,那么最终 ...

  10. Django框架ORM单表删除表记录_模型层

    此方法依赖的表是之前创建的过的一张表 参考链接:https://www.cnblogs.com/apollo1616/p/9840354.html 1.删除方法就是delete(),它运行时立即删除对 ...