pat 1003
2016-03-03 23:45
155 查看
1003. Emergency (25)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.
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
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 510
int roads
, dist
, visited
, persons
, path
;
int main() {
int n, m, c1, c2;
vector<int> resume;
scanf("%d %d %d %d", &n, &m, &c1, &c2);
for(int i = 0; i < n; i++) {
int num = 0;
scanf("%d", &num);
resume.push_back(num);
}
for(int i = 0; i < m; i++) {
int a, b, l;
scanf("%d %d %d", &a, &b, &l);
roads[a][b] = l;
roads[b][a] = l;
}
int index = c1, cnt = 1;
persons[index] = resume[index];
path[index] = 1;
for(int i = 0; i < n; i++) {
if(roads[index][i] == 0) continue;
dist[i] = roads[c1][i];
persons[i] = persons[index] + resume[i];
path[i] = 1;
}
visited[index] = 1;
for(int i = 0; i < n; i++) {
int minn = 0x7fffffff;
int tmp = -1;
for(int j = 0; j < n; j++) {
if(visited[j]==1 || dist[j] == 0)
continue;
if(dist[j]< minn) {
minn = dist[j];
index = j;
}
}
visited[index] = 1;
if(index == c2) break;
for(int j = 0; j < n; j++) {
if(visited[j] == 1 || roads[index][j]==0) continue;
if(dist[j] > 0 && dist[index]+roads[index][j] < dist[j]) {
dist[j] = dist[index]+roads[index][j];
persons[j] = persons[index] + resume[j];
path[j] = path[index];
} else if(dist[j] > 0 && dist[index]+roads[index][j] == dist[j]) {
//cnt+=1;
path[j] += path[index];
if(persons[index] + resume[j] > persons[j])
persons[j] = persons[index] + resume[j];
} else if(dist[j] == 0) {
dist[j] = dist[index]+roads[index][j];
persons[j] = persons[index] + resume[j];
path[j] = path[index];
}
}
}
if(c2 != c1)
printf("%d %d\n", path[c2], persons[c2]);
else
printf("1 %d\n", resume[c1]);
return 0;
}
相关文章推荐
- Git入门
- Android View(一)-View坐标以及方法说明
- Celery 和 Redis 入门
- 项目经常使用到的PCH文件设置全项目获取宏或类
- bzoj1251 序列终结者(splay)
- sql sql语句的执行原理
- android线程小结
- 自媒体的人格个性化如何培养?
- WCL入门
- Excel 导入 mysql
- ios即时通讯客户端开发之-mac上搭建openfire服务器
- 字符串处理:如何实现字符串的反转
- UILable“中文”加上“英文或者其他非中文字符”自动换行的问题
- C# 文件操作类集合(三) INI相关
- EntityFramework 开始小试
- 多级 级联 案例
- 开发工具整合
- C# 文件操作类集合(二) 文件处理
- 使用MAT查看Android内存泄露
- sharesdk 的使用