admin管理员组

文章数量:1577566

只用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;}

本文标签: PTAEmergency