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

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, C1 and 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, c2 and 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 C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and 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.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4

图使用链式存储。算法采用回溯+穷举,没有使用DFS或者Dijkstra算法
 
#include <stdio.h>
#include <stdlib.h>
#define bool int
typedef struct ROAD{
	int c1;
	int c2;
	int L;
	struct ROAD *next;
}ROAD;

typedef struct HEAD_TABLE_NODE{
	ROAD* head;
	ROAD* cur;
}HEAD_TABLE_NODE;

void insertNode(HEAD_TABLE_NODE *headsTable,int c1,ROAD *r){
		if(headsTable[c1].head == NULL){
			headsTable[c1].head = r;
			headsTable[c1].cur = r;
		}
		else{
			headsTable[c1].cur->next = r;
			headsTable[c1].cur = r;
		}
}
int main(){
	int N,M,C1,C2;
	int c1,c2,l,start;
	int resSum = 0,maxSum = 0;
	int pathCnt = 0;
	int pathL = 0,minL = 1000000;
	int *teams;
	int des = 0;
	int stack[1000],indexOfStack;
	HEAD_TABLE_NODE *headsTable;
	int i;
	ROAD *r;
	ROAD *tmp;
	bool visited[1000] = {0};
	scanf("%d%d%d%d",&N,&M,&C1,&C2);
	teams = (int *)malloc(N*sizeof(int));
	headsTable = (HEAD_TABLE_NODE *)malloc(N*sizeof(HEAD_TABLE_NODE));
	for(i = 0;i < N;++i){
		scanf("%d",&teams[i]);
	}
	for(i = 0;i < N;++i){
		headsTable[i].head = NULL;
		headsTable[i].cur = NULL;
	}
	for(i = 0;i < M;++i)
	{
		scanf("%d%d%d",&c1,&c2,&l);
		r = (ROAD *)malloc(sizeof(ROAD));
		r->c1 = c1;
		r->c2 = c2;
		r->L = l;
		r->next = NULL;
		insertNode(headsTable,c1,r);

		r = (ROAD *)malloc(sizeof(ROAD));
		r->c1 = c2;
		r->c2 = c1;
		r->L = l;
		r->next = NULL;
		insertNode(headsTable,c2,r);
	}
	
	if(C1 != C2){
		for(i = 0;i < N;++i){
			headsTable[i].cur = headsTable[i].head;
		}
		start = C1;
		visited[start] = 1;
		resSum = teams[start];
		indexOfStack = 0;
		while(headsTable[C1].cur != NULL){
			while(headsTable[start].cur != NULL){
				des = headsTable[start].cur->c2;
				if(visited[des]){
					headsTable[start].cur = headsTable[start].cur->next;
					continue;
				}
				visited[des] = 1;
				resSum = resSum + teams[des];
				pathL = pathL + headsTable[start].cur->L;
				
				
				if(des == C2) {
					if(pathL < minL){
						minL = pathL;
						pathCnt = 1;
						maxSum = resSum;
					}else if(pathL == minL){
						pathCnt++;
						if(maxSum < resSum)
							maxSum = resSum;
					}
					resSum = resSum - teams[des];
					pathL = pathL - headsTable[start].cur->L;
					visited[des] = 0;
					headsTable[start].cur = headsTable[start].cur->next;
				}
				else{
					stack[indexOfStack++] = start;
					start = des;
				}
			}
			if(headsTable[start].cur == NULL && start != C1){
				headsTable[start].cur = headsTable[start].head;
				start = stack[--indexOfStack];
				des = headsTable[start].cur->c2;
				resSum = resSum - teams[des];
				pathL = pathL - headsTable[start].cur->L;
				headsTable[start].cur = headsTable[start].cur->next;
				for(i = 0;i < N;++i) visited[i] = 0;
				for(i = 0;i <= indexOfStack;++i) visited[stack[i]] = 1;
			}
		}
	}
	else{
		pathCnt = 1;
		maxSum = teams[C1];
	}
	printf("%d %d",pathCnt,maxSum);
	free(teams);
	for(i = 0;i < N;++i){
		headsTable[i].cur = headsTable[i].head;
		while(headsTable[i].cur != NULL){
			tmp = headsTable[i].cur;
			headsTable[i].cur = tmp->next;
			free(tmp);
		}
	}
	free(headsTable);
	return 0;
}


 

你可能感兴趣的:(算法,it,解题报告,pat,1003)