POJ 3160 Father Christmas flymouse

探讨了在一张带有权值的有向图中,通过缩点转化为DAG图的方法来寻找一条路径,使得经过某些特定节点可以获得最大的累积正向权值。此问题涉及到图论中的缩点和DAG最优化等高级算法。

Father Christmas flymouse
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 3804 Accepted: 1256

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.

题意:有n个点m条边,给出每个点的权值(权值可能为负),对于一些点可以选择走或不走,求能获得的最大权值

先对其进行缩点,得到一个DAG图,对于每个强连通分量,他都能到这个分量里的任意一点,遇到权值为负数的点,就不管它

#include<iostream>
#include<cstring>
#include<stack>
#include<vector>
using namespace std;
const int maxn=3e4+7;
int index,scc_cnt;
int sum[maxn];
int val[maxn];
int dfn[maxn];
int low[maxn];
int num[maxn];
int in[maxn];
int numa[maxn];
int numb[maxn];
bool vis[maxn];
stack<int> sta;
vector<int> vec[maxn];
void init()
{
	index=scc_cnt=0;
	memset(num,0,sizeof(num));
	memset(in,0,sizeof(in));
	memset(dfn,0,sizeof(dfn));
	memset(sum,0,sizeof(sum));
	memset(low,0,sizeof(low));
	memset(vis,false,sizeof(vis));
	while(!sta.empty()) sta.pop();
	for(int i=0;i<maxn;i++) vec[i].clear();
}
void tarjan(int node)
{
	dfn[node]=low[node]=++index;
	sta.push(node);
	vis[node]=true;
	int len=vec[node].size();
	for(int i=0;i<len;i++)
	{
		int v=vec[node][i];
		if(!dfn[v])
		{
			tarjan(v);
			low[node]=min(low[node],low[v]);
		}
		else if(vis[v])
		{
			low[node]=min(low[node],dfn[v]);
		}
	}
	if(dfn[node]==low[node])
	{
		++scc_cnt;
		int v=-1;
		while(node!=v)
		{
			v=sta.top();
			sta.pop();
			vis[v]=false;
			num[v]=scc_cnt;
			if(val[v]<0) continue;
			sum[scc_cnt]+=val[v];
		}
	}
	return;
}
int dfs(int node,int ldj)
{
	int ans=ldj;
	int len=vec[node].size();
	for(int i=0;i<len;i++)
	{
		int v=vec[node][i];
		ans=max(ans,dfs(v,ldj+sum[v]));
	}
	return ans;
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		init();
		for(int i=0;i<n;i++)
		{
			cin>>val[i];
		}
		for(int i=0;i<m;i++)
		{
			int a,b;
			cin>>a>>b;
			numa[i]=a;
			numb[i]=b;
			vec[a].push_back(b);
		}
		for(int i=0;i<n;i++)
		{
			if(!dfn[i]) 
			{
				tarjan(i);
			}
		}
		for(int i=0;i<maxn;i++) vec[i].clear();
		for(int i=0;i<m;i++)
		{
			int a=num[numa[i]];
			int b=num[numb[i]];
			if(a!=b)
			{
				in[b]++;
				vec[a].push_back(b);
			}
		}
		int ans=0;
		for(int i=1;i<=scc_cnt;i++)
		{
			if(!in[i])
			{
				ans=max(ans,dfs(i,sum[i]));
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值