题目如下:
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
注意:
1.路是双向的
2.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.
意思是说你在保证最短路径的条件下 ,找到最多的救援帮手(不要忽略条件)
算法
可以用dijkstra 算法
每一个节点更新时分两种情况:
1.小于就更新最短路径数目,帮手人数,最短路径长度
2.等于就比较帮手人数,最短路径增加
所以说出发地的最短路径数目初始化为1
代码如下:
我用的是邻接表建图
#include <bits/stdc++.h>
using namespace std;
const int maxlen = 501;
class Arcnode
{
public:
int adjvex;
int Len;
Arcnode* next;
Arcnode() :adjvex(0), Len(0), next(nullptr) {}
};
class Vnode
{
public:
int data;
Arcnode* firstarc;
Vnode() :data(0), firstarc(nullptr) {}
};
class Map
{
public:
int Vnum;
int Anum;
int start;
int end;
int num[maxlen];
int minroad[maxlen];
int maxmun[maxlen];
int dist[maxlen];
bool visit[maxlen];
Vnode arr[maxlen];
Map(int v, int a, int s, int e)
{
this->Vnum = v;
this->Anum = a;
this->start = s;
this->end = e;
for (int i = 0; i < v; i++)
{
cin >> num[i];
}
for (int i = 0; i < a; i++)
{
int n, m, len;
cin >> n >> m >> len;
if (arr[n].firstarc == nullptr)
{
Arcnode* p = new Arcnode;
p->adjvex = m;
p->Len = len;
p->next = nullptr;
arr[n].firstarc = p;
}
else
{
Arcnode* p = new Arcnode;
p->adjvex = m;
p->Len = len;
p->next = arr[n].firstarc;
arr[n].firstarc = p;
}
if (arr[m].firstarc == nullptr)
{
Arcnode* p = new Arcnode;
p->adjvex = n;
p->Len = len;
p->next = nullptr;
arr[m].firstarc = p;
}
else
{
Arcnode* p = new Arcnode;
p->adjvex = n;
p->Len = len;
p->next = arr[m].firstarc;
arr[m].firstarc = p;
}
}
}
};
void dijkstra(Map& m)
{
memset(m.dist, 0x3f, sizeof(m.dist));
for (int i = 0; i < m.Vnum; i++)
{
m.visit[i] = false;
m.maxmun[i] = 0;
m.minroad[i] = 0;
}
m.minroad[m.start] = 1;
m.dist[m.start] = 0;
m.maxmun[m.start] = m.num[m.start];
for (int i = 0; i < m.Vnum; i++)
{
int t = -1;
for (int j = 0; j < m.Vnum; j++)
{
if (!m.visit[j] && (t == -1 || m.dist[j] < m.dist[t]))
{
t = j;
}
}
m.visit[t] = true;
if (m.arr[t].firstarc)
{
Arcnode* temp = m.arr[t].firstarc;
while (temp)
{
if (m.dist[temp->adjvex] > m.dist[t] + temp->Len)
{
m.dist[temp->adjvex] = m.dist[t] + temp->Len;
m.maxmun[temp->adjvex] = m.maxmun[t] + m.num[temp->adjvex];
m.minroad[temp->adjvex] = m.minroad[t];
}
else if (m.dist[temp->adjvex] == m.dist[t] + temp->Len)
{
m.minroad[temp->adjvex] += m.minroad[t];
m.maxmun[temp->adjvex] = max(m.num[temp->adjvex] + m.maxmun[t], m.maxmun[temp->adjvex]);
}
temp = temp->next;
}
}
}
}
int main()
{
int v, a, s, e;
cin >> v >> a >> s >> e;
Map m(v, a, s, e);
dijkstra(m);
cout << m.minroad[e] << " " << m.maxmun[e] << endl;
return 0;
}
新手 有问题欢迎指出