【PAT】1003. Emergency (25)

来源:互联网 发布:如怎么成为淘宝达人 编辑:程序博客网 时间:2024/06/05 11:57

1003. Emergency (25)

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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4
分析:Dijkstra算法处理,用 path[] 数组储存多条路径

细节:已注释


#include <iostream>#include <cstdio>#include <cmath>#include <queue>#include <vector>#include <functional>using namespace std;const int MAX_INT=1000000;const int Max=500;struct Edge{int nextVer;int weight;};struct Ver{int info;int t;vector<Edge> edge; //用 vector 存储链接表 };struct node // Dijkstra算法进行边排序,使用优先队列时的节点结构 {int v,w;int dist;friend bool operator < (const node &a, const node &b){return a.dist>b.dist; //转换定义,使队列按小到大排(偷懒做法) }};class graph{public:graph();void insertVerTeam(int v, int t);void insertEdge(int v, int w,int weight);void relaxEdge(int v);void Dijkstra(int begin, int end);void print(int begin, int end);private:Ver ver[Max];int numver;int numedge;int dist[Max];int path[Max];int team[Max];priority_queue<node> q; // Dijkstra算法排序使用优先队列 };graph::graph(){for (int i=0;i<Max;++i){ver[i].info=-1;ver[i].t=0;path[i]=team[i]=-1;dist[i]=MAX_INT;}numver=numedge=0;}void graph::insertVerTeam(int v, int t){ver[v].info=v;ver[v].t=t;}void graph::insertEdge(int v, int w, int weight){Edge temp;temp.weight=weight;temp.nextVer=w; //无向边,要添加两次 ver[v].edge.push_back(temp);temp.nextVer=v; //ver[w].edge.push_back(temp);}void graph::relaxEdge(int v) // 松弛边 {for (int i=0;i<ver[v].edge.size();++i){if (path[ver[v].edge[i].nextVer]==-1){node temp;temp.v=v;temp.w=ver[v].edge[i].nextVer;temp.dist=ver[v].edge[i].weight+dist[v];q.push(temp);}}}void graph::Dijkstra(int begin, int end){dist[begin]=0;path[begin]=1;team[begin]=ver[begin].t;relaxEdge(begin);while (!q.empty()){node p=q.top();if (dist[p.w]<p.dist){q.pop();continue;}else if (dist[p.w]==p.dist){ // 记录路径相等的路径数量 path[p.w]+=path[p.v];if (team[p.w]<team[p.v]+ver[p.w].t) team[p.w]=team[p.v]+ver[p.w].t;q.pop();continue; }path[p.w]=path[p.v];dist[p.w]=p.dist;if (team[p.w]<team[p.v]+ver[p.w].t) team[p.w]=team[p.v]+ver[p.w].t;q.pop();relaxEdge(p.w);}}void graph::print(int begin, int end){Dijkstra(begin, end);cout<<path[end]<<' '<<team[end]<<endl;}int main(){//freopen("test.txt","r",stdin);graph g;int n,m,begin,end;cin>>n>>m>>begin>>end;for (int i=0;i<n;++i){int t;cin>>t;g.insertVerTeam(i,t);}for (int i=0;i<m;++i){int v,w,weight;cin>>v>>w>>weight;g.insertEdge(v,w,weight);}g.print(begin,end);return 0;}



0 0
原创粉丝点击