PTA 甲级 1003 Emergency (25 分)

1003 Emergency (25 分)
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, C1and 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, c​2and 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​1and 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
这里要求单源点的最短路径,所以采用了dijkstra算法,在求完最短路径后,使用了dfs来求出路径数和最多救援队数量。

#include<stdio.h>
int e[500][500],numFirmen=0;//数组e用来保存两个城市之间路的长度 
int INF = 99999999;//定义INF为一个很大的值 
int minRoad = 0;//路径数 
int f[501];//存储消防员数目的数组 
int end, numCity;//终点和城市数 
int book2[601] = { 0 };//dfs中的标记数组,标记该点是否走过 
void dfs(int distance,int firemen,int current_position) {//深度优先搜索消防员人数和路径数 
	int v;//下一步走的位置 
	if(distance<0) return;
	if (distance == 0&&current_position==end) {
		minRoad++;
		if (numFirmen < firemen)
			numFirmen = firemen;
		return;
	}
	if (distance != 0&&current_position==end) return;
	if(distance==0)return;
	if(distance>0){
		for (v = 0; v < numCity; v++) {
			if (e[current_position][v]!=INF&&book2[v]==0) {
					book2[v] = 1;
					dfs( distance-e[current_position][v], firemen+f[v], v);
					book2[v] = 0;
			}
		}
	}
	return;
}
int main(void) {
	int  dis[600], book[600],m,t1,t2,t3,c1,i,j;
	int min,u=0,v;
	scanf("%d %d %d %d", &numCity, &m, &c1, &end);
	for (i = 0; i < numCity; i++)
		for (j = 0; j < numCity; j++)
			if (i == j)e[i][j] = 0;
			else e[i][j] = INF;
	for (i = 0; i < numCity; i++) {
		scanf("%d", &f[i]);
	}
	for (i = 0; i < m; i++) {
		scanf("%d %d %d", &t1, &t2, &t3);
		e[t1][t2] =e[t2][t1]= t3;
	}
	for (i = 0; i < numCity; i++) {
		dis[i] = e[c1][i];
	}
	for (i = 0; i < numCity; i++)
		book[i] = 0;
	book[c1] = 1;
	//Dijkstra算法核心语句
	for (i = 0; i < numCity - 1; i++) {
		min = INF;
		for (j = 0; j < numCity; j++) {//找到距离从c1最近的顶点 
			if (book[j] == 0 && dis[j] < min) {
				min = dis[j];
				u = j;
			}
		}
		book[u] = 1;
		for (v = 0; v < numCity; v++) {
			if (e[u][v] != INF) {
				if (dis[v] > dis[u] + e[u][v])
					dis[v] = dis[u] + e[u][v];
			}
		}
	}
	book2[c1]=1;
	dfs(dis[end], f[c1], c1);//输入剩余路程数,现有消防员数量,当前位置 
	printf("%d %d", minRoad, numFirmen);
}

我在完成代码时因为错把j打成i导致后三个测试点一直过不去,所以建议在多重循环中不要使用i,j这种相似的变量。
还应注意因为是无向图,所以将路径存入时e[t1][t2] =e[t2][t1]= t3;不要忘记在对称的位置存入。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值