POJ 3160 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 inN 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 followM 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

POJ Monthly--2006.12.31, Sempr

题解

强连通+spfa。

首先,舒适值为负 的点一定不访问。然后如果单单求最长路,有可能有正环。鉴于“每个人只会给飞鼠一次舒适值”,就用强连通缩点即可。

最后,我用了spfa的SLF优化,可是和不用的速度一样。POJ上只用了32ms,是不是数据太水了?

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,zz,head[30002],v[30002],pt,hd[30002];
struct bian{int to,nx,v;} e[150002],E[150002];
int pre[30002],low[30002],sccnum[30002],scc,ct;
int stack[30002],top,c[30002],in[30002];
int dis[30002],pd[30002],q[30002];
void insert(int x,int y)
{zz++; e[zz].to=y; e[zz].nx=head[x]; head[x]=zz;}
void init()
{
	int i,x,y; zz=0;
	memset(head,0,sizeof(head));
	for(i=0;i<n;i++) scanf("%d",&v[i]);
	for(i=1;i<=m;i++)
	   {scanf("%d%d",&x,&y);
	    insert(x,y);
	   }
}
void dfs(int x)
{
	pre[x]=low[x]=++ct;
	stack[++top]=x;
	int i,p;
	for(i=head[x];i;i=e[i].nx)
	   {p=e[i].to;
	    if(!pre[p])
		   {dfs(p); low[x]=min(low[x],low[p]);}
	    else if(!sccnum[p]) low[x]=min(low[x],pre[p]);
	   }
	p=-1;
	if(pre[x]==low[x])
	   {scc++;
		while(p!=x)
	       {p=stack[top]; top--;
		    sccnum[p]=scc; 
		    if(v[p]>0) c[scc]=c[scc]+v[p];
		   }
	   }
}
void targan()
{
	int i;
	scc=ct=top=0;
	memset(sccnum,0,sizeof(sccnum));
	memset(pre,0,sizeof(pre));
	memset(low,0,sizeof(low));
	memset(c,0,sizeof(c));
	for(i=0;i<n;i++)
	   {if(!pre[i]) dfs(i);}
}
void rebuild()
{
	int i,j,p,x,y;
	pt=0;
	memset(hd,0,sizeof(hd));
	memset(in,0,sizeof(in));
	for(i=0;i<n;i++)
	for(j=head[i];j;j=e[j].nx)
	   {p=e[j].to;
	    if(sccnum[i]!=sccnum[p])
	       {x=sccnum[i],y=sccnum[p];
		    pt++; E[pt].to=y; E[pt].v=c[y]; E[pt].nx=hd[x]; hd[x]=pt;
		    in[y]++;
		   }
	   }
	for(i=1;i<=scc;i++)
	   {if(!in[i])
	       {pt++; E[pt].to=i; E[pt].v=c[i]; E[pt].nx=hd[0]; hd[0]=pt;}
	   }
}
void spfa()
{
	memset(dis,-127,sizeof(dis));
	dis[0]=0; q[0]=0; pd[0]=1;
	int t=0,w=1,x,p,i;
	while(t!=w)
	   {x=q[t]; t=(t+1)%n;
	    for(i=hd[x];i;i=E[i].nx)
	       {p=E[i].to;
		    if(dis[x]+E[i].v>dis[p])
		       {dis[p]=dis[x]+E[i].v;
			    if(!pd[p])
			       {if(dis[p]>dis[q[t]])
				      {t=(t+n-1)%n; q[t]=p; pd[p]=1;}
				    else
				       {pd[p]=1; q[w]=p; w=(w+1)%n;}
				   }
			   }
		   }
		pd[x]=0;
	   }
	int ans=0;
	for(i=1;i<=scc;i++)
	   ans=max(ans,dis[i]);
	printf("%d\n",ans);
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	   {init(); 
	    targan(); rebuild(); 
	    spfa();
	   }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值