1003 Emergency (25分) 最短路径-迪杰斯特拉算法 dijkstra算法

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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

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

 大意:救援队用一个城市出发救援另一个城市,问有几条最短路径,且每个城市都有一个或多个救援队,问在这几条最短路径中,最多能集合起来的救援队一共有多少队。

 一刷:

柳:

#include <iostream>
#include <vector>

using namespace std;

//定义在外面的区别:全局数组初始化为0,局部数组初始化为随机数
bool visit[502];//代表那个已经确定最短路径的点所在的集合S
int num[502];//每个点最短路径的条数


int main()
{
	//freopen("in.txt", "r", stdin);
	const int inf = 999999;//代表无穷大  
	int n, m, c1, c2,e[502][502],dist[502],gatherTeams[502];
	cin >> n >> m >> c1 >> c2;
	vector<int> teams(n);
	
	for (int i = 0; i < n; i++)
	{
		cin >> teams[i];
	}
	fill(e[0], e[0] + 502 * 502, inf);
	fill(dist, dist + 502, inf);
	int a, b, c;
	for (int i = 0; i < m; i++)
	{
		cin >> a >> b >> c;
		e[a][b] =e[b][a]= c;
	}
	dist[c1] = 0;
	gatherTeams[c1] = teams[c1];
	num[c1] = 1;
	for (int i = 0; i < n; i++)
	{
		int u = -1,minn=inf;
		for (int j = 0; j < n; j++)
		{
			if (visit[j]==false&&dist[j]<minn)
			{
				u = j;
				minn = dist[j];
			}
		}
		if (u==-1)
		{
			break;//都进入集合S了
		}
		visit[u] = true;
		for (int j = 0; j < n; j++)
		{
			if (visit[j]==false&&e[u][j]!=inf)//e[u][j]==inf说明u和j之间没有路,不通
			{
				if (dist[j]>dist[u]+e[u][j])
				{
					dist[j] = dist[u] + e[u][j];
					num[j] = num[u];
					gatherTeams[j] = gatherTeams[u] + teams[j];
				}
				else if (dist[j]==dist[u]+e[u][j])
				{
					num[j] = num[j] + num[u];
					if (gatherTeams[u] + teams[j]>gatherTeams[j])
					{
						gatherTeams[j] = gatherTeams[u] + teams[j];
					}
					
				}
			}
		}
		
	}
	cout << num[c2] << " " << gatherTeams[c2] << endl;
		return 0;
}

二刷:两个点不过,没找到原因,已解决!(这是第二种用dfs的做法,且dfs部分有两种dfs方式)

吾:

#include <iostream>
#include <vector>

using namespace std;

const int maxn = 506, inf = 999999;
int n, m, scity, ecity, cnt = 0, maxhands = 0, temphands = 0, 
		e[maxn][maxn],weight[maxn],dis[maxn];
bool visit[maxn];
vector<int> pre[maxn];

//吾的dfs思路:过了
void dfs(int v, int temphands)
{
	//if (pre[v].size()>1)
	//{
	//	cnt *= pre[v].size();//计算路径数目  这种计算思路不对 这个错误找了好久 
	//}
	temphands += weight[v];
	if (temphands>maxhands)
	{
		maxhands = temphands;
	}
	if (v==scity)
	{
		cnt++;//改成这样就好了,回溯到起点的时候,路径+1
		return;
	}
	for (int i = 0; i < pre[v].size(); i++)
	{
		dfs(pre[v][i], temphands);
	}
}

//柳婼的dfs思路:过了
//vector<int> temppath;
//void dfs(int v)
//{
//	
//	temppath.push_back(v);
//	if (v==scity)
//	{
//		temphands = 0;
//		for (int i = 0; i < temppath.size(); i++)
//		{
//			temphands += weight[temppath[i]];
//		}
//		if (temphands> maxhands)
//		{
//			maxhands = temphands;
//		}
//		temppath.pop_back();
//		cnt++;
//		return;
//	}
//	for (int i = 0; i < pre[v].size(); i++)
//	{
//		dfs(pre[v][i]);
//	}
//	temppath.pop_back();
//}

int main()
{
	//freopen("in.txt", "r", stdin);
	cin >> n >> m >> scity >> ecity;
	fill(dis, dis + maxn, inf);
	fill(e[0], e[0] + maxn * maxn, inf);
	for (int i = 0; i < n; i++)
	{
		cin >> weight[i];
	}
	for (int i = 0; i < m; i++)
	{
		int a, b, len;
		cin >> a >> b >> len;
		e[a][b] = e[b][a] = len;
	}
	dis[scity] = 0;
	for (int i = 0; i < n; i++)
	{
		int minn = inf, u = -1;
		for (int j = 0; j < n; j++)
		{
			if (visit[j]==false&&dis[j]<minn)
			{
				minn = dis[j];
				u = j;
			}
		}
		if (u==-1)
		{
			break;
		}
		visit[u] = true;
		for (int v = 0; v < n; v++)
		{
			if (visit[v]==false&&e[u][v]!=inf)
			{
				if (dis[v]>dis[u]+e[u][v])
				{
					dis[v] = dis[u] + e[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if (dis[v] == dis[u] + e[u][v])
				{
					pre[v].push_back(u);
				}
			}
		}
	}
	dfs(ecity, temphands);


	//dfs(ecity);
	cout << cnt << " " << maxhands << endl;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值