L2-001 紧急救援 (25 分) 最短路

Dijkstra维护两个值。递归找起点。

Code:

#include <bits/stdc++.h>
const int inf = 1e9 + 7;
const int maxn = 505;
int a, b, c, n, m, ss, ee;
int dis[maxn], s[maxn][maxn], cnt[maxn], people[maxn], num[maxn], pre[maxn];
bool book[maxn];
using namespace std;
void print(int k)
{
	if (k == ss)
	{
		printf("%d", ss);
		return;
	}
	print(pre[k]);
	printf(" %d", k);
}
int main()
{
	scanf("%d%d%d%d", &n, &m, &ss, &ee);
	for (int i = 0; i < n; i++)
		scanf("%d", &people[i]);
	fill(dis, dis + maxn, inf);
	fill(s[0], s[0] + maxn * maxn, inf);
	fill(book, book + maxn, false);
	while (m--)
	{
		scanf("%d%d%d", &a, &b, &c);
		s[a][b] = c;
		s[b][a] = c;
	}
	dis[ss] = 0;
	cnt[ss] = 1;
	num[ss] = people[ss];
	for (int i = 0; i < n; i++)
	{
		int f = -1, minn = inf;
		for (int j = 0; j < n; j++)
		{
			if (book[j] == false && dis[j] < minn)
			{
				f = j;
				minn = dis[j];
			}
		}
		book[f] = true;
		if (f == -1)
			break;
		for (int j = 0; j < n; j++)
		{
			if (book[j] == false && dis[j] > dis[f] + s[f][j])
			{
				dis[j] = dis[f] + s[f][j];
				cnt[j] = cnt[f];
				num[j] = num[f] + people[j];
				pre[j] = f;
			}
			else if (book[j] == false && dis[j] == dis[f] + s[f][j])
			{
				cnt[j] += cnt[f];
				if (num[j] < num[f] + people[j])
				{
					num[j] = num[f] + people[j];
					pre[j] = f;
				}
			}
		}
	}
	printf("%d %d\n", cnt[ee], num[ee]);
	print(ee);
}

 

全部评论

相关推荐

仁者伍敌:实习生要工作经验,工作要实习经验
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 12:05
俺不中了,BOSS遇到了一个hr,我觉得我咨询的问题都很正常吧,然后直接就被拒绝了???
恶龙战士:你问的太多了,要不就整理成一段话直接问他,一个一个问不太好
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
07-10 12:17
已编辑
商丘师范学院 Java
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务