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, 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(dis[j]<minDis) {
u=j;
minDis=dis[j];
}
}
if(u==-1) return;//没有与s邻接的点
vis[u]=true;
for(int v=0; v<N; v++) { //从u出发能到达的所有顶点
//v未被访问&& u能到达v
if(vis[v]==false&&Adj[u][v].dis!=INF) {
if(dis[u]+Adj[u][v].dis<dis[v]) {//以u为中介dis[v]更优
dis[v]= dis[u]+Adj[u][v].dis;//优化dis[v]
w[v]=w[u]+weight[v];//覆盖w[v]
num[v]=num[u];//覆盖 num[v]
} else if(dis[u]+Adj[u][v].dis==dis[v]) {//找到一条相同长度的路径
if(w[u]+weight[v]>w[v]) {//以u为中介时w[v]更大
w[v]=w[u]+weight[v];//w[v]继承w[u]
}
num[v]+=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}); //声明结构体变量{v2,t}
}
Dijkstra(C1);
printf("%d %d",num[C2],w[C2]);
return 0;
}
注意
- fill与memset的区别:
fill 按照单元赋值,将一个区间的元素都赋同一个值
memset 按照字节填充某字符,一般只用于填充char型数据。
参考:
https://blog.csdn.net/liuchuo/article/details/52296646
memset()
BUG
- 出现这个报错可能是:全局变量和局部变量命名有相同的,使用时误用了另一个变量,改名即可。
全局变量和局部变量同名时,采用的是局部变量。
局部:
全局:
单词
- subscript :下标
原文地址:https://www.cnblogs.com/musecho/p/12252135.html