题意:有N个点,M条边,每个点有权值,问从起点到终点最短路的个数以及权值最大的最短路的权值。
分析:修改Dijstra模板。
#include<bits/stdc++.h> using namespace std; const int INT_INF = 0x3f3f3f3f; const int MAXN = 500 + 10; int weight[MAXN]; typedef long long LL; struct Edge{ int from, to; LL dist; Edge(int f, int t, LL d):from(f), to(t), dist(d){} }; struct HeapNode{ LL d; int u; HeapNode(LL dd, int uu):d(dd), u(uu){} bool operator < (const HeapNode& rhs)const{ return d > rhs.d; } }; struct Dijkstra{ int n, m; vector<Edge> edges; vector<int> G[MAXN]; LL d[MAXN]; int num[MAXN]; int value[MAXN]; bool done[MAXN]; void init(int n){ this -> n = n; for(int i = 0; i <= n; ++i) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, LL dist){ edges.push_back(Edge(from, to, dist)); m = edges.size(); G[from].push_back(m - 1); } void dijkstra(int s){ priority_queue<HeapNode> Q; for(int i = 0; i <= n; ++i){ d[i] = 0x3f3f3f3f3f3f3f3f; } memset(done, false, sizeof done); d[s] = 0; num[s] = 1; value[s] = weight[s]; Q.push(HeapNode(0, s)); while(!Q.empty()){ HeapNode x = Q.top(); Q.pop(); int u = x.u; if(done[u]) continue; done[u] = true; for(int i = 0; i < G[u].size(); ++i){ Edge &e = edges[G[u][i]]; if(d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; num[e.to] = num[u]; value[e.to] = value[u] + weight[e.to]; Q.push(HeapNode(d[e.to], e.to)); } else if(d[e.to] == d[u] + e.dist){ num[e.to] += num[u]; if(value[u] + weight[e.to] > value[e.to]){ value[e.to] = value[u] + weight[e.to]; } } } } } }dij; int main(){ int n, m, st, et; scanf("%d%d%d%d", &n, &m, &st, &et); for(int i = 0; i < n; ++i){ scanf("%d", &weight[i]); } dij.init(n); int x, y; LL l; for(int i = 0; i < m; ++i){ scanf("%d%d%lld", &x, &y, &l); dij.AddEdge(x, y, l); dij.AddEdge(y, x, l); } dij.dijkstra(st); printf("%d %d\n", dij.num[et], dij.value[et]); return 0; }