浙大pat Advance 1003

来源:互联网 发布:双色球出号绝密算法69 编辑:程序博客网 时间:2024/04/29 16:40

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

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

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.

基本的dijsktra算法,加上改进。基本上都是在更新路径的时候,去更新路的条数和人员数。

#include <iostream>

#include <vector>
#define MAXLEN 500
using namespace std;
typedef struct Node{
int next; //相邻节点的几点编号
int len;
}Node;
int main()
{
vector<Node> v[MAXLEN];
int n, m, c1, c2;
while(cin >> n >> m >> c1 >> c2){
int pCount[MAXLEN];
int i;
int dist[MAXLEN];
bool flag[MAXLEN];
int pMaxCount[MAXLEN];
int roadNum[MAXLEN];
for(i = 0; i < n; i ++){
cin >> pCount[i];
v[i].clear();
dist[i] = -1;
flag[i] = false;
}
for(i = 0; i < m; i ++){
int a, b, c;
cin >> a >> b >> c;
Node p;
p.next = a;
p.len = c;
v[b].push_back(p);
p.next = b;
v[a].push_back(p);
}
dist[c1] = 0;
flag[c1] = true;
int curN = c1;
pMaxCount[curN] = pCount[curN];
roadNum[curN] = 1;
for(i = 0; i < n - 1; i ++){
int j;
for(j = 0; j < v[curN].size(); j ++){
int t = v[curN][j].next;
if(flag[t] == true){
continue;
}
int c = v[curN][j].len;
if(dist[t] == -1){
dist[t] = dist[curN] + c;
pMaxCount[t] = pMaxCount[curN] + pCount[t];
roadNum[t] = roadNum[curN];
}
else if(dist[t] == dist[curN] + c){
roadNum[t] = roadNum[curN] + roadNum[t];
if(pMaxCount[curN] + pCount[t] > pMaxCount[t]){
pMaxCount[t] = pMaxCount[curN] + pCount[t];
}
}
else if(dist[t] > dist[curN] + c){
dist[t] = dist[curN] + c;
roadNum[t] = roadNum[curN];
pMaxCount[t] = pMaxCount[curN] + pCount[t];
}
}
int min = 99999999;
for(j = 0; j < n; j ++){
if(flag[j] == true || dist[j] == -1){
continue;
}
if(dist[j] < min){
min = dist[j];
curN = j;
}
}
flag[curN] = true;
}
cout << roadNum[c2] << " " << pMaxCount[c2] << endl;
}
return 0;
}
0 0
原创粉丝点击