0
点赞
收藏
分享

微信扫一扫

1003 Emergency (25分)——Dijkstra(邻接表、邻接矩阵)、Bellman_Ford、SPFA


1 题目

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, C​1​ 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​​ , c​2
​​ 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 C​1​​ and C​2​​ , 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

2 解析

2.1 题意

给N个城市,M条无向边,每个城市都有一定数目的救援数目,所有边的边权都已知。给出起点和终点,求起点到终点的最短路径条数和最短路径上救援小组数目之和。

2.2 思路

求图的最短路径,同时求最短路径条数和最短路径上的最大点权之和。

  • 注意点:
  • 1 顶点下标范围为0~N-1,且边为无向边
  • 2 输出的第一个数目是最短路径条数
  • 3 最短路径条数的依据仅与第一标尺有关,与点权无关。

3 参考代码

3.1 Dijkstra

3.1.1 邻接表

用时:
1003 Emergency (25分)——Dijkstra(邻接表、邻接矩阵)、Bellman_Ford、SPFA_#include

#include 
#include
#include
#include

using std::fill;
using std::vector;

struct node
{
int v;
int dis;
};

int N, M, start, end;
const int MAXV = 600;
vector<node> G[MAXV];
bool vis[MAXV] = {false};//是否已访问
int d[MAXV];//最短距离
int INF =0x3fffffff;
int weight[MAXV];//点权
int w[MAXV];//记录最大点权之和
int num[MAXV];//记录最短路径条数

void Dijkstra(int s){
fill(d, d + MAXV, INF);
memset(w, 0, sizeof(w));
memset(num, 0, sizeof(num));
d[s] = 0;
w[s] = weight[s];
num[s] = 1;

for (int i = 0; i < N; ++i) {
int u = -1, min = INF;
for (int j = 0; j < N; ++j) {
if(vis[j] == false && d[j] < min){
u = j;
min = d[j];
}
}

if(u == -1) return;
vis[u] = true;
for (int j = 0; j < G[u].size(); ++j) {
int v = G[u][j].v;
if(vis[v] == false){
if(d[u] + G[u][j].dis < d[v]){//以u为中介点能令d[v]更小
d[v] = d[u] + G[u][j].dis;
w[v] = w[u] + weight[v];
num[v] = num[u];
}else if(d[u] + G[u][j].dis == d[v]){
if(w[u] + weight[v] > w[v]){//以u为中介点时点权更大
w[v] = w[u] + weight[v];
}
num[v] += num[u];
}
}
}
}
}

int main(int argc, char const *argv[])
{
scanf("%d%d%d%d", &N, &M, &start, &end);
for (int i = 0; i < N; ++i)
{
scanf("%d", &weight[i]);
}

int e1, e2, tempW;
node temp;
for (int i = 0; i < M; ++i)
{
scanf("%d%d%d",&e1, &e2, &tempW);
temp.v = e2;
temp.dis = tempW;
G[e1].push_back(temp);
temp.v = e1;
G[e2].push_back(temp);
}

Dijkstra(start);
printf("%d %d\n", num[end], w[end]);

return 0;
}

3.1.2 邻接矩阵

用时:
1003 Emergency (25分)——Dijkstra(邻接表、邻接矩阵)、Bellman_Ford、SPFA_#include_02

#include 
#include
#include
#include

using std::fill;
using std::vector;

struct node
{
int v;
int dis;
};

int N, M, start, end;
const int MAXV = 600;
int G[MAXV][MAXV];
bool vis[MAXV] = {false};//是否已访问
int d[MAXV];//最短距离
int INF =0x3fffffff;
int weight[MAXV];//点权
int w[MAXV];//记录最大点权之和
int num[MAXV];//记录最短路径条数

void Dijkstra(int s){
fill(d, d + MAXV, INF);
memset(w, 0, sizeof(w));
memset(num, 0, sizeof(num));
d[s] = 0;
w[s] = weight[s];
num[s] = 1;

for (int i = 0; i < N; ++i) {
int u = -1, min = INF;
for (int j = 0; j < N; ++j) {
if(vis[j] == false && d[j] < min){
u = j;
min = d[j];
}
}

if(u == -1) return;
vis[u] = true;
for (int v = 0; v < N; ++v) {
if(vis[v] == false && G[u][v] != INF){
if(d[u] + G[u][v] < d[v]){
d[v] = d[u] + G[u][v];
w[v] = w[u] + weight[v];
num[v] = num[u];
}else if(d[u] + G[u][v] == d[v]){
if(w[u] + weight[v] > w[v]){
w[v] = w[u] + weight[v];
}
num[v] += num[u];
}
}
}
}
}

int main(int argc, char const *argv[])
{
scanf("%d%d%d%d", &N, &M, &start, &end);
for (int i = 0; i < N; ++i)
{
scanf("%d", &weight[i]);
}

fill(G[0], G[0] + MAXV * MAXV, INF);
int e1, e2;
for (int i = 0; i < M; ++i)
{
scanf("%d%d",&e1, &e2);
scanf("%d", &G[e1][e2]);
G[e2][e1] = G[e1][e2];
}

Dijkstra(start);
printf("%d %d\n", num[end], w[end]);

return 0;
}

3.2 Bellman_Ford

用时:
1003 Emergency (25分)——Dijkstra(邻接表、邻接矩阵)、Bellman_Ford、SPFA_#include_03

#include 
#include
#include
#include
#include

using std::vector;
using std::fill;
using std::set;

struct node
{
int v;
int dis;
node(int _v, int _dis) : v(_v), dis(_dis){}//构造函数
};

const int INF = 0x3fffffff;
const int MAXV = 510;
vector<node> G[MAXV];
int N;
int d[MAXV];//最短距离
int w[MAXV];//最大点权之和
int num[MAXV];//最短路径条数
int weight[MAXV];//点权
int start, end;
set<int> pre[MAXV];//前驱

void Bellman(int s){
fill(d, d + MAXV, INF);
memset(w, 0, sizeof(w));
memset(num, 0 , sizeof(num));

d[s] = 0;
w[s] = weight[s];
num[s] = 1;

for (int i = 0; i < N - 1; ++i)//执行n-1轮操作
{
for (int u = 0; u < N; ++u)//每轮遍历所有边
{
for (int j = 0; j < G[u].size(); ++j)
{
int v = G[u][j].v;//邻接边的顶点
int dis = G[u][j].dis;//邻接边的边权
if(d[u] + dis < d[v]){//以u为中介点时令d[v]更小
d[v] = dis + d[u];//覆盖d[u]
w[v] = w[u] + weight[v];
num[v] = num[u];
pre[v].clear();
pre[v].insert(u);
}else if(d[u] + dis == d[v]){//找到一条长度相同的路径
if(w[u] + weight[v] > w[v]){//以u为中介点时点权之和更大
w[v] = w[u] + weight[v];
}
pre[v].insert(u);//将u加入pre[v]
num[v] = 0;//重新统计num[v]
for (set<int>::const_iterator it = pre[v].begin(); it != pre[v].end() ; ++it)
{
num[v] += num[*it];
}
}
}
}
}
}

int main(int argc, char const *argv[])
{
int m;
scanf("%d%d%d%d", &N, &m, &start, &end);

for (int i = 0; i < N; ++i)
{
scanf("%d", &weight[i]);
}

int e1, e2, distance;
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d", &e1, &e2, &distance);
G[e1].push_back(node(e2, distance));
G[e2].push_back(node(e1, distance));
}

Bellman(start);
printf("%d %d\n", num[end], w[end]);

return 0;
}

3.3 SPFA

用时:
1003 Emergency (25分)——Dijkstra(邻接表、邻接矩阵)、Bellman_Ford、SPFA_最短路径_04

#include 
#include
#include
#include
#include
#include

using std::set;
using std::vector;
using std::queue;
using std::fill;

struct node
{
int v;
int dis;
node(int _v, int _dis): v(_v), dis(_dis){}
};

const int MAXV = 510;
const int INF = 0x3ffffff;
vector<node> G[MAXV];
set<int> pre[MAXV];
int N;
bool inq[MAXV];
int d[MAXV];
//int num[MAXV];
int start, end;
int weight[MAXV];
int w[MAXV];
int Count[MAXV];

void SPFA(int s){
memset(inq, false, sizeof(inq));
// memset(num, 0, sizeof(num));
fill(d, d + MAXV, INF);

queue<int> q;
q.push(s);
inq[s] = true;
d[s] = 0;
w[s] = weight[s];
// num[s]++;
Count[s] = 1;

while(!q.empty()){
int u = q.front();
q.pop();
inq[u] = false;

for (int j = 0; j < G[u].size(); ++j)
{
int v = G[u][j].v;
int dis = G[u][j].dis;
if(d[v] > d[u] + dis){
d[v] = d[u] + dis;

w[v] = w[u] + weight[v];

pre[v].clear();
pre[v].insert(u);

Count[v] = Count[u];

if(!inq[v]){
q.push(v);
inq[v] = true;
// num[v]++;
// if(num[v] >= N){
// return;
// }
}
}else if(d[v] == d[u] + dis){
if(w[u] + weight[v] > w[v]){
w[v] = w[u] + weight[v];
}

pre[v].insert(u);

Count[v] = 0;
for (set<int>::const_iterator it = pre[v].begin(); it != pre[v].end() ; ++it)
{
Count[v] += Count[*it];
}

if(!inq[v]){
q.push(v);
inq[v] = true;
// num[v]++;
// if(num[v] >= N){
// return;
// }
}
}
}
}
}

int main(int argc, char const *argv[])
{
int m, u, v, distance;
scanf("%d%d%d%d", &N, &m, &start, &end);

for (int i = 0; i < N; ++i)
{
scanf("%d", &weight[i]);
}

for (int i = 0; i < m; ++i)
{
scanf("%d%d%d", &u, &v, &distance);
G[u].push_back(node(v,distance));
G[v].push_back(node(u,distance));
}

SPFA(start);

printf("%d %d\n", Count[end], w[end]);
return 0;
}

举报

相关推荐

0 条评论