浙大pat Advance 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

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.

基本的dijsktra算法,加上改进。基本上都是在更新路径的时候,去更新路的条数和人员数。

#include <iostream>

#include <vector>
#define MAXLEN 500
using namespace std;
typedef struct Node{
int next; //相邻节点的几点编号
int len;
}Node;
int main()
{
vector<Node> v[MAXLEN];
int n, m, c1, c2;
while(cin >> n >> m >> c1 >> c2){
int pCount[MAXLEN];
int i;
int dist[MAXLEN];
bool flag[MAXLEN];
int pMaxCount[MAXLEN];
int roadNum[MAXLEN];
for(i = 0; i < n; i ++){
cin >> pCount[i];
v[i].clear();
dist[i] = -1;
flag[i] = false;
}
for(i = 0; i < m; i ++){
int a, b, c;
cin >> a >> b >> c;
Node p;
p.next = a;
p.len = c;
v[b].push_back(p);
p.next = b;
v[a].push_back(p);
}
dist[c1] = 0;
flag[c1] = true;
int curN = c1;
pMaxCount[curN] = pCount[curN];
roadNum[curN] = 1;
for(i = 0; i < n - 1; i ++){
int j;
for(j = 0; j < v[curN].size(); j ++){
int t = v[curN][j].next;
if(flag[t] == true){
continue;
}
int c = v[curN][j].len;
if(dist[t] == -1){
dist[t] = dist[curN] + c;
pMaxCount[t] = pMaxCount[curN] + pCount[t];
roadNum[t] = roadNum[curN];
}
else if(dist[t] == dist[curN] + c){
roadNum[t] = roadNum[curN] + roadNum[t];
if(pMaxCount[curN] + pCount[t] > pMaxCount[t]){
pMaxCount[t] = pMaxCount[curN] + pCount[t];
}
}
else if(dist[t] > dist[curN] + c){
dist[t] = dist[curN] + c;
roadNum[t] = roadNum[curN];
pMaxCount[t] = pMaxCount[curN] + pCount[t];
}
}
int min = 99999999;
for(j = 0; j < n; j ++){
if(flag[j] == true || dist[j] == -1){
continue;
}
if(dist[j] < min){
min = dist[j];
curN = j;
}
}
flag[curN] = true;
}
cout << roadNum[c2] << " " << pMaxCount[c2] << endl;
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
浙江大学(Zhejiang University)是一所位于中国浙江省杭州市的综合性高等学府,拥有深厚的学术底蕴和雄厚的师资力量。在浙大的C语言英文课件中,学生将学习并掌握C编程语言的基本语法和相关概念,以及如何用C语言进行程序设计与开发。 C语言是一种古老但仍然广泛使用的编程语言,因其简洁高效的特点,在计算机科学、软件工程和嵌入式系统等领域具有重要地位。浙大的C语言英文课件将从基础知识开始,逐步引导学生了解变量、数据类型、运算符、循环结构、数组和函数等基本概念。学生将学习如何使用这些概念以及相关语法规则来编写简单的C程序。 在课程的进阶部分,学生将深入学习C语言的高级特性,包括指针、结构和文件操作等。指针是C语言的重要特点之一,掌握指针的使用可以提高程序的灵活性和效率。结构(structure)则允许学生组合不同类型的数据,形成更加复杂的数据类型,并通过结构体(struct)对其进行操作。文件操作是学生在实际项目中常常需要处理的任务,包括读写文件和管理文件系统等。 通过浙大的C语言英文课件,学生不仅将理解C语言的基本概念和语法,还将通过实践项目来提高编程能力。课件中可能包括一些实例和练习题,帮助学生巩固所学知识,并培养解决问题和团队合作的能力。学生还可以通过课件学习相关的编程工具和调试技巧,为将来的软件开发和项目管理打下坚实的基础。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值