浙大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.
#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;
}
- 浙大pat Advance 1003
- 浙大PAT甲级-1003
- 浙大 pat Basic Level 1003
- pat-advance(1069-1072)
- 浙大PAT 1003题 1003. Emergency
- 浙大PAT advanced level 1003 emergency
- 浙大pat 1017
- 浙大PAT目录
- 浙大PAT 1049
- 浙大PAT 1051
- 浙大PAT 1058
- 浙大PAT 1063
- 浙大PAT 1102
- 浙大PAT 1099
- 浙大PAT 1033
- 浙大pat甲级 1023
- 浙大pat甲级 1024
- 浙大pat甲级 1025
- 快速排序
- Python文件夹与文件的操作
- [黑马程序员]多线程_懒汉模式
- POJ1182食物链
- 1 Longest Palindromic Substring
- 浙大pat Advance 1003
- Electric Car Rally--最短路SPFA
- MYSQL语句, 空的 order by , 空的 where
- Linux中Kill进程的N种方法
- 杭电 1418 抱歉 题解报告
- 高性能MySQL第1章知识点梳理
- Windows Mysql Server重启, log-bin路径配置
- 黑客语言Python
- eval 转换成json对象