ZJU PAT 1003 Emergency

最短路径,DFS 、路径非负的时候还能用dijkstra算法。

#include<iostream>
using namespace std;
inline int max_(int &a, int &b)
{
	return a>b?a:b;
}
const int src = 505;
const int des = 505;
int road[src][des];//-1表不通
bool vis[src];//1表未遍历过
int hper_[src];
int len,hper,shortest,res_hper,road_num;
void dfs(int const &c1, int const &c2,int const &city_num_);
int main()
{
	int city_num,city_src,city_des,i,j;
	scanf("%d%d%d%d",&city_num,&road_num,&city_src,&city_des);
	for(i = 0 ; i < city_num ; i++)
	{
		vis[i] = 1;
		cin >> hper_[i];
		for(j = 0 ; j < city_num; j++)road[i][j] = -1;
	}
	for(i = 0 ; i < road_num ; i++)
	{
		int tmp1,tmp2,tmp3;
		cin>>tmp1>>tmp2>>tmp3;
		road[tmp1][tmp2] = road[tmp2][tmp1] = tmp3;
	}

	shortest = -1;
	len = hper = res_hper = road_num = 0;
	dfs(city_src,city_des,city_num);
	cout<<road_num<<" "<<res_hper<<endl;
	return 0;
}
void dfs(int const &c1,int const &c2 ,int const &city_num_)
{
	hper += hper_[c1];
	vis[c1] = 0;
	if(c1 == c2)
	{
		if(len <= shortest || shortest == -1)
		{
			if(shortest == len)
			{
				res_hper = max_(hper,res_hper);
				road_num++;
			}
			else
			{
				shortest = len; 
				res_hper = hper;
				road_num = 1;
			}
		}
		return;
	}
	for(int c = 0 ; c < city_num_ ; c++)
	{
		if(vis[c]&&road[c1][c]!=-1)
		{
			len += road[c1][c];
			dfs(c,c2,city_num_);
			len -= road[c1][c];
			hper -= hper_[c];
			vis[c] = 1;
		}
	}
	return;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值