首页 > 其他分享 >1003 Emergency

1003 Emergency

时间:2024-08-01 14:53:28浏览次数:8  
标签:pq dist rescue Emergency int teams length 1003

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, 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 Specification:

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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

#include <iostream>
#include <vector>
#include <limits>
#include <queue>

using namespace std;

const int INF = numeric_limits<int>::max();//int max

struct Edge
{
	int to;
	int length;
	Edge(int _to,int _length):to(_to),length(_length){}
};
typedef pair<int, int>pii;//pair存储距离和城市
void dijkstra(vector<vector<Edge>>& graph, vector<int>& teams, int start, int end, int n)
{
	vector<int>dist(n, INF);//距离
	vector<int>num_paths(n, 0);
	vector<int>rescue_teams(n, 0);
	priority_queue<pii, vector<pii>, greater<pii>>pq;
	// 使用最小堆的优先队列,根据距离排序
	dist[start] = 0;
	num_paths[start] = 1;
	rescue_teams[start] = teams[start];
	pq.push({ 0,start });

	while (!pq.empty()) {
		int d = pq.top().first; 当前节点的最短距离 
		int u = pq.top().second;// 当前节点的索引 
		pq.pop();

		if (d > dist[u])continue;

		for (Edge& e : graph[u]) {
			int v = e.to;
			int len = e.length;

			if (dist[u] + len < dist[v]) {
				dist[v] = dist[u] + len;
				num_paths[v] = num_paths[u];
				rescue_teams[v] = rescue_teams[u] + teams[v];
				pq.push({ dist[v],v });
			}
			else if(dist[u]+len==dist[v]) {
				num_paths[v] += num_paths[u];
				if (rescue_teams[u] + teams[v] > rescue_teams[v]) {
					rescue_teams[v] = rescue_teams[u] + teams[v];
				}
			}
		}
	}
	cout << num_paths[end] << " " << rescue_teams[end] << endl;
}
int main()
{
	int N, M, C1, C2;
	cin >> N >> M >> C1 >> C2;

	vector<int>teams(N);//each city's rescue teams
	for (int i = 0; i < N; i++) {
		cin >> teams[i];
	}

	vector<vector<Edge>>graph(N);//建立无权图
	for (int i = 0; i < M; i++) {
		int c1, c2, l;
		cin >> c1 >> c2 >> l;
		graph[c1].emplace_back(c2, l);//添加边到图
		graph[c2].emplace_back(c1, l);//无权图,反向添加
	}
	dijkstra(graph, teams, C1, C2, N);//计算最短路径的方法
	
	return 0;
}

注意:

        

标签:pq,dist,rescue,Emergency,int,teams,length,1003
From: https://blog.csdn.net/2301_80161204/article/details/140767280

相关文章

  • 2024“钉耙编程”中国大学生算法设计超级联赛(2)1003
    绝对不模拟的简单魔方要相信题目的提示(直接模拟的代码长达300行),由于魔方的特性,不论如何转动脚上的色块颜色不会变动,只要枚举8个角块看看是否一致即可,枚举角块时需确定访问角块颜色的顺序,例如以3号为顶,后左上访问顺序为123即坐标为\((3,4)->(4,3)-(4,4)\),那么可以通过此角......
  • 2024杭电钉耙2-1003 HDOJ7447 绝对不模拟的简单魔方
    欢迎您来我的网站看这篇题解!Problem有一个魔方可能被拧了不超过三次,同时还弄丢了一个角块上的两个贴纸。现在把这两个贴纸贴回去,请问有没有贴错?只可能拧侧面,不会拧中间层,且每次只能拧\(90^\circ\)。魔方用一个9行12列的字符型矩阵表示:初始魔方的展开图如下图:\(1\leT......
  • 2024杭电1003 HDOJ7435 树
    本文同步发布于我的网站Problem给一棵根为1的有根树,点\(i\)具有一个权值\(A_i\)。定义一个点对的值\(f(u,v)=\max\left(A_u,A_v\right)\times\left|A_u-A_v\right|\)。你需要对于每个节点\(i\),计算\(ans_i=\sum_{u\in\operatorname{subtree}(i),v\in\op......
  • 1003:对齐输出 题解
    题目链接题目描述读入三个整数,按每个整数占\(8\)个字符的宽度,右对齐输出它们,按照格式要求依次输出三个整数,之间以一个空格分开。解题思路由于我们不知道这个数有多大,所以我们可以用printf自带的占位符%xd输出,其中x为位数。例:printf("%3d",a);就是占用3位。题目要求为\(8\)位......
  • HDU1000,HDU1001,HDU1002,HDU1003,HDU1004
    目录HDU1000——A+BProblem题目描述运行代码代码思路HDU1001——SumProblem题目描述运行代码代码思路HDU1002——A+BProblemII(高精度加法)题目描述运行代码代码思路高精度加法模板HDU1003——MaxSum题目描述运行代码代码思路HDU1004——LettheBall......
  • 《昇思25天学习打卡营第06天|qingyun201003》
    日期心得什么是函数式自动微分,在日常的模型训练中,涉及到复杂的数学公式如何转换为机械语言,通过本次的学习,使我了解到了如何去做梯度计算,通过梯度计算,设计损失函数,有一步步优化代码。昇思MindSpore基础入门学习函数式自动微分(AI代码解析)函数式自动微分神经网络的......
  • 1003:对齐输出
    ......
  • 【数学】100332. 包含所有 1 的最小矩形面积 II
    本文涉及知识点数学LeetCode100332.包含所有1的最小矩形面积II给你一个二维二进制数组grid。你需要找到3个不重叠、面积非零、边在水平方向和竖直方向上的矩形,并且满足grid中所有的1都在这些矩形的内部。返回这些矩形面积之和的最小可能值。注意,这些......
  • C++题解(1) 信息学奥赛一本通 1003:对齐输出 洛谷 B2004:对齐输出 土豆编程 T1003:对
    【题目描述】读入三个整数,按每个整数占8个字符的宽度,右对齐输出它们,按照格式要求依次输出三个整数,之间以一个空格分开。【输入】只有一行,包含三个整数,整数之间以一个空格分开。【输出】只有一行,按照格式要求依次输出三个整数,之间以一个空格分开。【输入样例】......
  • 100311. 无需开会的工作日
    题目描述给你一个正整数days,表示员工可工作的总天数(从第1天开始)。另给你一个二维数组meetings,长度为n,其中meetings[i]=[start_i,end_i]表示第i次会议的开始和结束天数(包含首尾)。返回员工可工作且没有安排会议的天数。注意:会议时间可能会有重叠。情况描述6月2日周......