pat 1003. Emergency ——深搜

题目要求:找到到达目标的最短路径条数,并计算能到达目标点最多的人数!!

#include<iostream>
#include<stdio.h>
using namespace std;

const int maxN=505;
const int bignum=999999;
class node
{
public:
	int to,next,cost;
};
class st
{
public:
	int cost,man,way;
};
node edge[maxN*3];
st ans[maxN];
int head[maxN],cnt=0,team[maxN];
int N,M,C1,C2;

void Init()
{
	int i,a,b,c;
	scanf("%d%d%d%d",&N,&M,&C1,&C2);
	for(i=0;i<maxN;i++) head[i]=0;
	for(i=0;i<N;i++)
		scanf("%d",&team[i]);
	for(i=0;i<M;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		edge[++cnt].to =b;
		edge[cnt].next =head[a];
		edge[cnt].cost =c;
		head[a]=cnt;
		edge[++cnt].to =a;
		edge[cnt].next =head[b];
		edge[cnt].cost =c;
		head[b]=cnt;
	}
}
int vis[maxN];
void dfs(int u)
{
	int i;
	for(i=head[u];i;i=edge[i].next ){ // 遍历
		int v=edge[i].to,now_cost ;
		if(!vis[v]){
			now_cost=ans[u].cost +edge[i].cost ;
			if(now_cost<ans[v].cost ){ // 找到新的路径
				ans[v].cost =now_cost;
				ans[v].way =ans[u].way; // 
				ans[v].man =(ans[u].man +team[v]);
				
			}
			else if(now_cost==ans[v].cost ){ // 相等的时候  路径+1 
				ans[v].way +=1;
				ans[v].man =ans[v].man >(ans[u].man +team[v])?ans[v].man :(ans[u].man +team[v]) ;
			}
	    	else  if(now_cost>ans[v].cost)  // 路径超过之前的,不在往下 
				continue;
		   if(v==C2) continue ; //到达目标
		  vis[v]=1;
			dfs(v);// 继续往下深搜~
		  vis[v]=0;
		}
	}
}
void solve()
{
	int i,j;
	for(j=0;j<maxN;j++)  vis[j]=0;
	for(i=0;i<maxN;i++) {
		ans[i].cost =bignum; ans[i].way =0;
	}
	ans[C1].way=1;ans[C1].cost =0; ans[C1].man=team[C1];vis[C1]=1;
	dfs(C1);
	printf("%d %d\n",ans[C2].way,ans[C2].man);
}
int main()
{
	Init();
	solve();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值