【PAT甲级】1003 Emergency (25分)

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

分析(参考《算法笔记》p367-p381)

要求:最短路径,最大点权

求:最短路径的条数,最短路径的基础上的最大点权


顶点:城市

边:路

边权:路的长度

点权:每个城市的救援队个数


  • 无向权图,再插入边权时,一条边要插入两次

  • dis[u] 为起点s到点u的最短距离

dis[]数组的初始不需要另外写,第一次循环找到的最小dis[u]一定是dis[s](=0),而后优化所有从s出发能到达的顶点的dis[],即对整个dis[]进行了初始化。


代码

/*邻接表实现Dijkstra算法*/

#include<iostream>
#include<vector>

using namespace std;

const int maxN=500;             //最多顶点个数
const int INF=1000000000;//10^9 dis[u]的最大距离
struct Node {
	int v,dis;//v顶点编号 ,dis 点Adj[u]到点j的边权
};
vector<Node> Adj[maxN];//图G使用邻接表实现,存放从点u出发可以到达的所有顶点
int dis[maxN];//dis[u] 起点s到点u的最短距离
int num[maxN];//num[u] 起点s到u的最短路径条数
int w[maxN];//w[u] 起点s到点u的最大点权
int  weight[maxN];//weight[u] 点u的点权
bool vis[maxN]= {false}; //点的访问数组
int N,M,C1,C2;//城市个数,路的条数,起始城市,目的城市

void Dijkstra(int s) {
	/*初始化*/
	fill(dis,dis+N,INF);//赋值dis数组全部元素为INF
	fill(w,w+N,0);//赋值w数组全部元素为0
	fill(num,num+N,0);// 赋值num数组全部元素为0

	dis[s]=0;//s到自身的距离为 0
	w[s]=weight[s]; //s到自身的点权为 weight[s]
	num[s]=1;//s到自身的最短路径为1条

	/**/
	for(int i=0; i<N; i++) { //循环n次
		//找到 未访问过且最小dis[u]的顶点
		int u=-1;
		int minDis=INF;
		for(int j=0; j<N; j++) {
			if(vis[j]==false&&dis[j]<minDis) {
				u=j;
				minDis=dis[j];
			}
		}

		if(u==-1) return;//没有与s邻接的点,则结束

		vis[u]=true;
		for(int vv=0; vv<Adj[u].size(); vv++) { //遍历从u出发能到达的所有顶点
			int index=Adj[u][vv].v;
			//index未被访问
			if(vis[index]==false) {
				if(dis[u]+Adj[u][vv].dis<dis[index]) {//以u为中介dis[index]更优
					dis[index]= dis[u]+Adj[u][vv].dis;//优化dis[index]
					w[index]=w[u]+weight[index];//覆盖w[index]
					num[index]=num[u];//覆盖 num[index]
				} else if(dis[u]+Adj[u][vv].dis==dis[index]) {//找到一条相同长度的路径
					if(w[u]+weight[index]>w[index]) {//以u为中介时w[index]更大
						w[index]=w[u]+weight[index];//w[index]继承w[u]
					}
					num[index]+=num[u];
				}
			}
		}

	}
}


int main() {
	scanf("%d%d%d%d",&N,&M,&C1,&C2);
	for(int i=0; i<N; i++) {   //点权
		scanf("%d",&weight[i]);
	}
	//插入无向权图的边权
	int u,v,t;
	for(int i=0; i<M; i++) {
		scanf("%d%d%d",&u,&v,&t);
		Adj[u].push_back({v,t}); //声明结构体变量{v,t}
		Adj[v].push_back({u,t}); //声明结构体变量{u,t}
	}

	Dijkstra(C1);
	printf("%d %d",num[C2],w[C2]);

	return 0;
}

注意


BUG

  • 出现这个报错可能是:全局变量和局部变量命名有相同的,使用时误用了另一个变量,改名即可。

    全局变量和局部变量同名时,采用的是局部变量。

局部:

全局:


单词

  • subscript :下标
原文地址:https://www.cnblogs.com/musecho/p/12252135.html