首页 > 学院 > 开发设计 > 正文

PAT (Advanced Level) Practise 1003. Emergency (25)

2019-11-06 09:24:21
字体:
来源:转载
供稿:网友

1003. Emergency (25) 时间限制: 400 ms内存限制: 65536 kB代码长度限制: 16000 B判题程序:Standard
  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. Examples

Input
5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Output
2 4

  Notes   【acm】 作者   CHEN, Yue

  在Dijkstra算法的基础上,增加队伍数量和路径数的确定,当到达某点的多条路长度相同时,考虑队伍数量最大化。   

#include <iostream>#include <algorithm>#include <map>#include <vector>#include <functional>#include <string>#include <cstring>#include <queue>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <sstream>#include <iomanip>using namespace std;#define IOS ios_base::sync_with_stdio(false)#define TIE std::cin.tie(0)#define MIN2(a,b) (a<b?a:b)#define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))#define MAX2(a,b) (a>b?a:b)#define MAX3(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))typedef long long LL;typedef unsigned long long ULL;const int INF = 0x3f3f3f3f;const double PI = 4.0*atan(1.0);const double eps = 0.000001;const int maxn = 505;int n, m, c1, c2, L;int st, en;int teams[maxn];struct Edge{ int form, to, dist; //Edge(int u, int v, int d) :form(u), to(v), dist(d){}};struct HeapNode{ int d, u; bool Operator < (const HeapNode &a)const { return d > a.d; }};struct Dijkstra{ vector<Edge> edge; vector<int> G[maxn]; int d[maxn], p[maxn], t[maxn]; bool done[maxn]; void init() { for (int i = 0; i < n; i++) G[i].clear(); edge.clear(); } void addEdge(const Edge &e) { edge.push_back(e); G[e.form].push_back(edge.size() - 1); } void dijkstra(int s) { for (int i = 0; i < n; i++) d[i] = INF; for (int i = 0; i < n; i++) t[i] = 0; for (int i = 0; i < n; i++) p[i] = 0; memset(done, 0, sizeof(done)); d[s] = 0; p[s] = 1; t[s] = teams[s]; priority_queue<HeapNode> que; que.push(HeapNode{0,s}); while (que.size()){ HeapNode x = que.top(); que.pop(); if (done[x.u]) continue; done[x.u] = true; for (int i = 0; i < G[x.u].size(); i++){ Edge &e = edge[G[x.u][i]]; if (d[e.to] > d[x.u] + e.dist){ d[e.to] = d[x.u] + e.dist; p[e.to] = p[x.u]; t[e.to] = t[x.u] + teams[e.to]; } else if (d[e.to] == d[x.u] + e.dist){ if (t[e.to] < t[x.u] + teams[e.to]) t[e.to] = t[x.u] + teams[e.to]; p[e.to] += p[x.u]; } que.push(HeapNode{ d[e.to], e.to }); } } }};int main(){ scanf("%d%d%d%d", &n, &m, &st, &en); Dijkstra dj; dj.init(); for (int i = 0; i < n; i++){ scanf("%d", &teams[i]); } for (int i = 0; i < m; i++){ scanf("%d%d%d", &c1, &c2, &L); dj.addEdge(Edge{ c1, c2, L }); dj.addEdge(Edge{ c2, c1, L }); } dj.dijkstra(st); printf("%d %d/n", dj.p[en], dj.t[en]); //system("pause");}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表