PTA 1003 Emergency

只用dfs算法实现
题目:
1003 Emergency (25 分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c​2and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C
​1 to C2.

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C​1and C2 , and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

代码如下

#include<stdio.h>
int lengthCity[600]={0};//现城市与起点城市的距离 
int numFiremen[600]={0};//每个城市的消防员个数 
int lengthLink[600][600]={0};//两个城市之间的距离 
int book[600]={0};//用于标记城市是否已遍历 
int distance,min=99999999,targetcity,originalcity,numCity,currentNum=0,road=1,Num;
void deepsearch(int ,int );
int main(void)
{
	int numLink;
	scanf("%d%d%d%d",&numCity,&numLink,&originalcity,&targetcity);
	int i,j;
	for(i=0;i<numCity;i++) //读入每个城市消防员的数量 
	{
		scanf("%d",&numFiremen[i]);
	}
	int target,origin;
	for (i=0;i<numCity;i++)//初始化矩阵 
	{
		for(j=0;j<numCity;j++)
		{
			if(i==j) lengthLink[i][j]=0;
			else lengthLink[i][j]= -1;
		}
	}
	for(i=0;i<numLink;i++)  //读入城市之间的距离 
	{
		scanf("%d%d",&origin,&target);
		scanf("%d",&lengthLink[origin][target]);
		lengthLink[target][origin]=lengthLink[origin][target];   //建立无向图 
	}
	currentNum=numFiremen[originalcity];
	book[originalcity]=1;
	if(originalcity==targetcity)
	printf("1 %d",numFiremen[targetcity]);//起点和终点相同的情况 
	else{
	deepsearch(originalcity,0);
	printf("%d %d",road,Num);//输出结果 
	}
}
void deepsearch(int current,int distance)
{
	int j;
	if(distance>min) return; //未到终点但距离比现最小值大 
	if(current==targetcity)//到达终点 
	{
		if(distance<min) //发现更短路程,更新数据 
		{
			min=distance;
			Num=currentNum;
			road=1;
		}
		else if (distance==min)//路程长度相等的情况 
		{
			road++;
			if(Num<currentNum)
			Num=currentNum;
		}
		return;
	 } 
	 
	 for(j=0;j<numCity;j++)
	 {
	 	if(lengthLink[current][j]!=-1&&book[j]==0)
	 	{
	 		book[j]=1;   //标记城市已在路中 
	 		currentNum=currentNum+numFiremen[j];   //现在城市消防员数量 
	 		deepsearch(j,distance+lengthLink[current][j]);    //探索从下一个城市出发到达终点的路 
	 		if(currentNum!=numFiremen[originalcity])
	 		currentNum=currentNum-numFiremen[j];
	 		book[j]=0;//取消标记城市 
		 }
	 }
	 return;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值