1003. Emergency (25)

1.求源城市s到目标城市t的最小耗费路径,如果路径不唯一,则输出具有最多救援队的路径。

2.采用dijkstra算法,算出最小耗费cost。

3.利用最小耗费cost最为约束条件,进行深度搜索DFS(循环->锁->dfs->解锁)。

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

AC代码:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
using namespace std;
#define maxDist 999999
struct vertexNode{
	vector<int> list;
	bool visited, sured;
	int pre;
	int cost;
};
struct cmp{
	bool operator()(const pair<int, int>&a, const pair<int, int>&b)
	{
		return a.second > b.second;
	}
};

void dfs(int now, int&target, vertexNode *v, int **w, int*rescue, vector<bool>&used, int &minCost, vector<int>&ans, int nowCost, int rescueTeam)
{
	if (now == target&&minCost == nowCost)
	{
		ans.push_back(rescueTeam);
	}
	else
	{
		for (int i = 0; i < v[now].list.size(); i++)
		{
			int q = v[now].list[i];
			if (!used[q] && nowCost + w[now][q] <= minCost)
			{//以最小耗费为约束条件
				used[q] = true;
				dfs(q, target, v, w, rescue, used, minCost, ans, nowCost + w[now][q], rescueTeam + rescue[q]);
				used[q] = false;
			}
		}
	}
}

int main(void) {

	int n, m, s, t;
	cin >> n >> m >> s >> t;

	int *rescue = new int[n];
	int *cost = new int[n];

	for (int i = 0; i < n; i++)
	{
		scanf("%d", &rescue[i]);//输出各城市的救援队数目
		cost[i] = 99999999;//初始化cost
	}

	int **w = new int*[n];

	vertexNode *v = new vertexNode[n];
	for (int i = 0; i < n; i++)
	{
		w[i] = new int[n];
		memset(w[i], 0, sizeof(w[i]));
		v[i].cost = 99999999;
		v[i].pre = -1;
		v[i].list = vector<int>(0);
		v[i].sured = false;
		v[i].visited = false;
	}

	for (int i = 0; i < m; i++)
	{
		int a, b, weight;
		scanf("%d %d %d", &a, &b, &weight);
		v[a].list.push_back(b);
		v[b].list.push_back(a);
		w[a][b] = weight;
		w[b][a] = weight;
	}

	v[s].visited = true;
	v[s].pre = -1;
	v[s].cost = 0;
	while (1)
	{//采用dijkstra算法计算最小耗费
		int p = -1;
		for (int i = 0; i < n; i++)
		{
			if (p == -1 && v[i].visited&&!v[i].sured)
				p = i;
			else if (p != -1 && v[i].visited&&!v[i].sured&&v[p].cost > v[i].cost)
				p = i;
		}
		if (p == -1) break;
		v[p].sured = true;
		for (int i = 0; i < v[p].list.size(); i++)
		{
			int q = v[p].list[i];
			if (!v[q].visited)
			{
				v[q].cost = v[p].cost + w[p][q];
				v[q].visited = true;
				v[q].pre = p;
			}
			else if (v[q].visited&&!v[q].sured&&v[q].cost > v[p].cost + w[p][q])
			{
				v[q].cost = v[p].cost + w[p][q];
				v[q].visited = true;
				v[q].pre = p;
			}
		}
	}
	int minCost = v[t].cost;
	vector<bool> used(n, false);
	vector<int> ans(0);
	used[s] = true;
	dfs(s, t, v, w, rescue, used, minCost, ans, 0, rescue[s]);

	sort(ans.begin(), ans.end());
	cout << ans.size() << " " << ans.back() << endl;

	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注