Time Limit: 1000MS Memory Limit: 131072Kmarkdown
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.ide
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.oop
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.ui
For each test case, output one line with only the maximized sum of accumulated comfort indices.spa
2 2
14
21
0 1
1 0code
35ip
32-bit signed integer type is capable of doing all arithmetic.ci
POJ Monthly–2006.12.31, Semprrem
題意:Flymouse從武漢大學ACM集訓隊退役後,作起了志願者,在聖誕節來臨時,Flymouse要打扮成聖誕老人給集訓隊員發放禮物。集訓隊員住在校園宿舍的不一樣寢室,爲了節省體力,Flymouse決定從某一個寢室出發,沿着有向路一個接一個的訪問寢室並順便發放禮物,直至能到達的全部寢室走遍爲止。對於每個寢室他能夠通過無數次可是隻能進入一次,進入房間會獲得一個數值(數值可正可負),他想知道他能得到最大的數值和。input
思路:對於一個有向圖,圖中的強連通必定能夠相互抵達,因此Flymouse能夠訪問強連通份量中的任意元素,對於集合中的負值不要,只要正值就能夠保證獲得的值最大,因此咱們將強連通縮點後造成一個DAG圖,搜索一下就能夠獲得最大值。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;
const int Max = 30010;
vector<int>Map[Max];
vector<int>G[Max];
vector<int>P[Max];
int va[Max]; //節點價值
int dfn[Max],low[Max],vis[Max],dep;//遍歷的順序,回溯,標記,遍歷的順序。
int pre[Max],num,a[Max];// 集合,數目,集合價值
stack<int>S;
int n,m;
void init() //初始化
{
for(int i=0;i<=n;i++)
{
Map[i].clear();
P[i].clear();
G[i].clear();
}
memset(vis,0,sizeof(vis));
memset(a,0,sizeof(a));
dep = 0 ; num = 0;
}
void Tarjan(int u)
{
dfn[u] = low[u] =dep++;
vis[u]=1;
S.push(u);
for(int i=0;i<Map[u].size();i++)
{
if(vis[Map[u][i]]==1)
{
low[u] = min(low[u],dfn[Map[u][i]]);
}
else if(vis[Map[u][i]]==0)
{
Tarjan(Map[u][i]);
low[u] = min(low[u],low[Map[u][i]]);
}
}
if(dfn[u]==low[u])
{
while(!S.empty()) //縮點
{
int v = S.top();
S.pop();
pre[v] = num;
vis[v] = 2;
a[num]+=va[v];
G[num].push_back(v);//記錄集合的點
if(u==v)
{
break;
}
}
num++;
}
}
int dfs(int u)
{
if(!vis[u])
{
int ans = 0;
vis[u]=1;
for(int i=0;i<P[u].size();i++)
{
ans = max(ans,dfs(P[u][i]));
}
a[u] += ans ;
}
return a[u];
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=0;i<n;i++) //先輸入價值
{
scanf("%d",&va[i]);
va[i]=va[i]<0?0:va[i];//小於零的歸零,爲不訪問
}
int u,v;
for(int i=0;i<m;i++) //建圖
{
scanf("%d %d",&u,&v);
Map[u].push_back(v);
}
for(int i=0;i<n;i++)//強連通縮點
{
if(vis[i]==0)//從未被遍歷的點搜索
{
Tarjan(i);
}
}
for(int i=0;i<num;i++) //從新建圖
{
memset(vis,0,sizeof(vis));
for(int j=0;j<G[i].size();j++)
{
int u=G[i][j];//集合中的點
for(int k=0;k<Map[u].size();k++)
{
if(pre[Map[u][k]] != i && !vis[pre[Map[u][k]]])
{
P[i].push_back(pre[Map[u][k]]);
vis[pre[Map[u][k]]] = 1;
}
}
}
}
int ans= 0 ;
memset(vis,0,sizeof(vis));
for(int i=0;i<num;i++)//搜索最大的值
{
ans = max(ans,dfs(i));
}
printf("%d\n",ans);
}
return 0;
}