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.


解题心得:

  1. 题意很简单,就是给你一个有向图,要你选择任意一个起点开始走,每一个点有一个权值(有正有负),你在每一个点可以选择是否加该点的权值,每个点可以多次走过但是权值只能加一次,问你走这个图得到的最大值是多少。
  2. 每个点枚举跑DFS就不想了,处理负权直接当0来处理就行了,因为可以选择不加上去。这个题可以选择缩点之后再跑DFS,将一个联通块缩成一个点,这个点的的权值就是连通图里面权值的总和。

#include<stdio.h>
#include<cstring>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 3e4+200;
vector<int> ve[maxn],shrink[maxn],maps[maxn];
int pre[maxn],w[maxn],n,m,dfn[maxn],low[maxn],num,tot,w1[maxn],Max;
bool vis[maxn];
stack<int> st; void init()
{
while(!st.empty())
st.pop();
for(int i=0; i<=n; i++)
{
ve[i].clear();
maps[i].clear();
shrink[i].clear();
}
num = tot = 0;
memset(w1,0,sizeof(w1));
memset(vis,0,sizeof(vis));
memset(pre,0,sizeof(pre));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
for(int i=0; i<n; i++)
{
scanf("%d",&w[i]);
w[i] = w[i]<0?0:w[i];
}
for(int i=0; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
ve[a].push_back(b);
}
} void tarjan(int x)
{
dfn[x] = low[x] = ++tot;
vis[x] = true;
st.push(x);
for(int i=0; i<ve[x].size(); i++)
{
int v = ve[x][i];
if(!dfn[v])
{
tarjan(v);
low[x] = min(low[x],low[v]);
}
else if(vis[v])
{
low[x] = min(low[x],dfn[v]);
}
}
if(low[x] == dfn[x])
{
while(1)
{
int now = st.top();
st.pop();
shrink[num].push_back(now);
pre[now] = num;
w1[num] += w[now];
vis[now] = false;
if(now == x)
break;
}
num++;
}
} void get_new_maps()//缩点之后建立新图
{
memset(vis,0,sizeof(vis));
for(int i=0; i<num; i++)
{
for(int j=0; j<shrink[i].size(); j++)
{
for(int k=0; k<ve[shrink[i][j]].size(); k++)
{
int v = ve[shrink[i][j]][k];
if(pre[v] != i)//两点不在同一个联通块内
maps[i].push_back(pre[v]);
}
}
}
} void dfs(int pos,int sum_w)
{
if(sum_w > Max)
Max = sum_w;
for(int i=0;i<maps[pos].size();i++)
{
int v = maps[pos][i];
dfs(v,sum_w+w1[v]);
}
} int get_ans()
{
Max = 0;
for(int i=0;i<num;i++)
{
dfs(i,w1[i]);
}
} int main()
{
while(cin>>n>>m)
{
init();
for(int i=0; i<n; i++)
{
if(!dfn[i])
tarjan(i);
}
get_new_maps();
get_ans();
printf("%d\n",Max);
}
return 0;
}

POJ:3160-Father Christmas flymouse的更多相关文章

  1. poj 3160 Father Christmas flymouse

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

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

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

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

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

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

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

  5. Father Christmas flymouse

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

  6. L - Father Christmas flymouse

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

  7. POJ——T3160 Father Christmas flymouse

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

  8. Father Christmas flymouse--POJ3160Tarjan

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

  9. poj:4091:The Closest M Points

    poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...

随机推荐

  1. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  2. Windows7&IIS7.5部署Discuz全攻略

    组长说在内网部署一个论坛,这可难不倒我,装个Discuz嘛.部署环境就一台普通的PC,四核i3,Windows7.这就开搞了. 准备工作 系统是Windows 7 专业版,自带IIS7.5(家庭版不带 ...

  3. Hadoop实战项目:小文件合并

    项目背景 在实际项目中,输入数据往往是由许多小文件组成,这里的小文件是指小于HDFS系统Block大小的文件(默认128M),早期的版本所定义的小文件是64M,这里的hadoop-2.2.0所定义的小 ...

  4. 4. 把一幅彩色图像的R、G、B分量单独显示。

    #include <cv.h> #include <highgui.h> int main(void) { IplImage* oo = cvLoadImage(); IplI ...

  5. jsp使用中$的符号使用失效

    解决方法 添加一段话  <%@ page isELIgnored="false"%> 原因:因为jsp servlet版本问题,2.3及2.3之前的版本isELIgno ...

  6. 小技巧:在向导式页面设计中使用hidden型输入可以避免session的使用

    在向导式页面设计中使用hidden型输入可以避免session的使用,从而减小内存开支. 在表单中使用隐藏输入类型<input type="hidden" name=&quo ...

  7. 浏览器兼容之条件注释,cssHack

    对于形形色色的浏览器,随之而来的就是一些兼容问题,大多应该都是IE下的兼容问题,因为任何浏览器下出现渲染不一致都极有可能是我们自己的结构或样式不符合W3C的某些要求,或者说违背了浏览器的某些规则而先造 ...

  8. 洛谷 P2733 家的范围 Home on the Range

    题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因为一些原因,他的奶牛只在正方形的牧场上吃草.)遗憾的是,他的奶牛已经毁坏一些土地.( 一 ...

  9. connect() to 192.168.30.71:8082 failed (99: Cannot assign requested address) while connecting to upstream, client: 114.80.182.136, server: localhost, request: "GET /home/senior HTTP/1.1", upstream: "

    connect() to 192.168.30.71:8082 failed (99: Cannot assign requested address) while connecting to ups ...

  10. UVA 714 Copying Books 抄书 (二分)

    题意:把一个包含m个正整数的序列划分成k个非空的连续子序列.使得所有连续子序列的序列和Si的最大值尽量小. 二分,每次判断一下当前的值是否满足条件,然后修改区间.注意初始区间的范围,L应该为所有正整数 ...