20130709 【南华大学 ACM】 新生赛第二场 【B. Dating With Girls】


Problem B: Dating With Girls

Time Limit:2 Sec  Memory Limit:128 MB

Description

If you have a date with a pretty girl in a hurry, you can ignore what I will say next.

Hellis is a little bad guy in Rural Small Technical College. And the most important fact is that he is especially fond of glaring at pretty girls! And as we all know, there are some girls he favors in the same school. So there comes the trouble. On one terrible day, it should rains. The girls all are being different places. They all phone him and ask the boy to deliver them umbrellas at the same time. However, the cute boy faces the embarrassing condition. You can have a simple Understanding that each girl has a special relationship with our sunny boy. If they know the boy sends the umbrella to anyone of them, the others may go mad and that is not the boy expects. If that happens, Of course you will never see that he is completing again. But the fact is some girls are so beautiful and he would like to give them help. The trouble is this guy wants to meet more beautiful girls before he arrives at anyone who phones him for help. It is just a disaster, he thinks. Eventually, he makes the choice. He will just send an umbrella to only one girl who is most important to him, and he can not be seen by other girls who phones him for help. If that happens, I can only say hehe. He must pass these girls. In the process, he can send beautiful girls their umbrellas! In that case, he can create a chance to communicate with them and just win their favorable impression. It is such a good idea!

There arendifferent places in our problem. There aremdifferent undirectional edges. And there is no loop. The bad guy starts at 1 node, and other 2 tonnode stand different girls. Each girl is standing at different nodes, too. The i-th girl haswi. When Hellis meets a girl, he can get|wi|point, and he wants to get max sum of point he gets.(wi<0 means the i-th girl has phoned him for help). 

Input

Multiple test cases. For each test case:
First line, two integers,
n ,m(1< n <100, 1< m <5000)

2th to (m+1)th per line,ai,bi(0<ain,0<bin)means one road from aito bi.

(m+2)th to(n+m)th per line,wi(0<|wi|≤100,2in

Output

If the guy can meet the girl they chose, output line print a single integer ans)the max sum of point he gets.

Else print /What is a fucking day!0(without the quotes). 

Sample Input

3 3
1 2
1 3
2 3
-30
10

Sample Output

30


----------------------------------------------------------------------------------------------------------

解题报告:


思路就是:

【注:由于输入时一时大意,导致代码中人的位置为输入时-1,即r[i-1][j-1]】

【所以 np 个人(包括男主角,0号),有np-1个女孩(从1号开始),最末端女孩的位置号为np-1】

  1. 二维数组(r[i][j]),记录起点(i)到终点(j)是否有路(r[i][j]为1“有路”,其余情况为“无路”);
  2. 评分(s[j]),记录j号人(女孩)的分数。
  3. 从 自己本身(起点i=0)开始搜索路径终点( j 从0开始++,使r[i][j]==1,就找到路)。
  4. ----------
  5. 记录点从0开始(h=0),记录起点(head[h]=i)与此前的总分(hs[h]=sum);
  6. 把 j 号女孩的分数加入sum中(sum+=j),更新最小值(min)。
  7. ----------
  8. 如果 搜索到 最后一个女孩 都没有通路(此时j==np);
  9. 返回上一记录点(h--);
  10. 终点从 此时 起点 i+1 开始;
  11. 起点回溯到上一记录点(i=head[h]);
  12. 当前总分也回溯到上一记录点(sum=hs[h])。
  13. ----------
  14. 当起点回溯到 男主角 (i=0) 时,搜索到最后的女孩 (j==np)时;
  15. 记录点将回溯到再上一个 起点,此时记录 h = -1,此时跳出搜索。
  16. ----------
  17. 输出 最后得总分(min<0,输出最小值的相反数;否则,输出“What is fucking day!”)。 

----------------------------------------------------------------------------------------------------------

代码:

#include<stdio.h>
int main()
{
	int r[105][105]={0},s[105],nr,np,i,j,h,min,sum,head[105],hs[105];
//路[起点][终点] 评分[女生位置] 人数 路数 i,j,h 最小值 当前值 起点[记录点] 总分[记录点]
	scanf("%d%d",&np,&nr);
	for(i=0;i<nr;i++)
	{
		scanf("%d%d",&i,&j);		// i is 起点 -> j is 终点 
		r[i-1][j-1]=1;			// 有路 
	}
	for(s[0]=0,i=1;i<np;i++)		// 自己 不是 妹子 
		scanf("%d",&s[i]);		// 评分 
	min=100;				// 用最大值 初始化 最小值 
	for(sum=h=i=j=0; ;)
	{	
		for(  ; j<np ;j++)						
		{	
			if( 1==r[i][j] )					 
			{	
				head[h]=i;	// 记录 当前 起点 
				hs[h]=sum;	// 记录 上个 总和 
				h++;		// 记录点 增加
				sum += s[ j ];	// 当前总分 
				i=j;		// 终点	转为 起点
				if( min > sum )	min=sum;	// 更新 最小分 
				break;			
			}	
		}
		if( j==np )			// 搜索到了 最后的节点,回溯 
		{
			j=i+1;			// 在前一节点上 紧接 上个起点 搜索 路径   
			h--; 			// 返回 上一 记录点
			i=head[h];		// 回溯 到 上个点 作为 起点	
			sum=hs[h];		// 返回 上个点 时 总和 
			if( h<0 )	break;	// 回到 不曾存在 的 过去 		
		}
		else	j=0; 			// 未到达 末端时 从头开始 搜索路径 
	}
	if( min > sum )		min=sum;	// 更新 最小值 
	if( min < 0 )		printf("%d\n",-min);
	else			printf("What is a fucking day!\n"); 
	return 0; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值