HUST - 1608 求带权有向图上的最长路

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 are n different places in our problem. There are m different undirectional edges. And there is no loop. The bad guy starts at 1 node, and other 2 to n node stand different girls. Each girl is standing at different nodes, too. The i-th girl has wi. When Hellis meets a girl, he can get |wi| point, and he wants to get max sum of point he gets.(wi<0 mean the i-th girl has phoned him for help 
Input
First line, two integers, n ,m (1<n<100, 1<m<5000) 
2th to (m+1)  th per line ,  ai, bi (0<ai<=n, 0<bi<=n) means one road from  ai to bi. 
(m+2)  th to (n+m)  th per line,  wi (0<|  wi|<=100, 2<=i<=n)  
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!”  
Sample Input
3 3
1 2
1 3
2 3
-30
10
Sample Output
30



最长路的求法类似最短路,只不过每个点向拓展的时候都要先更新一遍dp[i],拓展完最后要把该点的vis值改回零

#include<bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
#define mod 1000000007
using namespace std;
vector<int> v[105];
int w[105];
int vis[105];
int dp[105];
int main(){
	int n,m,a,b;
	cin>>n>>m;
	for(int i=0;i<m;++i){
		cin>>a>>b;
		v[a].push_back(b);
	}
	int minn=inf,id=-1;
	for(int i=2;i<=n;++i){
		cin>>w[i];
		if(w[i]<0){
			if(w[i]<minn){
				minn=w[i];
				id=i;
			}
		}
	}
	if(id==-1){
		cout<<"What is a fucking day!"<<endl;
		return 0;
	}
	queue<int> q;
	q.push(1);
	vis[1]=1;
	while(!q.empty()){
		int fr=q.front();
		q.pop();
		for(int i=0;i<v[fr].size();++i){
			a=v[fr][i];
			if(w[a]>=0||a==id)dp[a]=max(dp[a],dp[fr]+abs(w[a]));
			if(vis[a]==1||w[a]<0) continue;
			vis[a]=1;
			q.push(a);
		}
		vis[fr]=0; //????
	}
	if(dp[id]==0) cout<<"What is a fucking day!"<<endl;
	else cout<<dp[id]<<endl; 
}  


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值