PAT甲级1003
Posted wx630c98f24f6b8
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲级1003相关的知识,希望对你有一定的参考价值。
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<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 500;
const int INF = 1000000000;
struct Node
int v, dis;
node;
struct compare
bool operator()(Node n1, Node n2)
return n1.dis > n2.dis;
;//自定义比较器
vector<Node> Adj[maxn];//图的邻接表形式
int N, M, C1, C2, c1, c2, L;
int weight[maxn] = 0 ;//记录各顶点的权
bool vis[maxn] = false ;//标记顶点是否被访问
int d[maxn], w[maxn],//统计最短路径、最大点权
num[maxn];//统计最短路径数
void Dijkstra(int s)
fill(d, d + maxn, INF);
fill(w, w + maxn, 0);
fill(num, num + maxn, 0);
d[s] = 0;
w[s] = weight[s];//对增加点权的问题初始化
num[s] = 1;//对增加路径数的问题初始化
priority_queue<Node,vector<Node>,compare>Q;//找出最小的u,我这里没用穷举方式,用堆优化一下
node.v = s; node.dis = d[s];
Q.push(node);
int u;
for (int i = 0; i < N; i++)
if (!Q.empty())
u = Q.top().v;
vis[u] = true;
Q.pop();
else
return;//已经没有要处理的点了,可以返回了
for (int j = 0; j < Adj[u].size(); j++)
int v = Adj[u][j].v;
int dis = Adj[u][j].dis;//注意这里是边长
if (!vis[v])
if (d[u] + dis < d[v])
d[v] = d[u] + dis;
w[v] = weight[v] + w[u];
num[v] = num[u];//优化情形下直接继承
node.v = v; node.dis = d[v];//注意这里的dis是更新为起点到当前点的距离
Q.push(node);
else if (d[u] + dis == d[v])
num[v] += num[u];//相等则累加
if(w[v]<weight[v] + w[u])
w[v] = weight[v] + w[u];//总是更新为最大点权
int main()
scanf("%d%d%d%d", &N, &M, &C1, &C2);
for (int i = 0; i < N; i++)
scanf("%d", &weight[i]);
for (int i = 0; i < M; i++)
scanf("%d%d%d", &c1, &c2, &L);
node.v = c2;
node.dis = L;
Adj[c1].push_back(node);
node.v = c1;
Adj[c2].push_back(node);
Dijkstra(C1);
printf("%d %d\\n", num[C2], w[C2]);
return 0;
以上是关于PAT甲级1003的主要内容,如果未能解决你的问题,请参考以下文章