[题目链接]
https://www.spoj.com/problems/ADATRIP/
[算法]
直接使用dijkstra堆优化算法即可
[代码]
#include<bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; const int INF = 2e9; struct edge { int to,w,nxt; } e[MAXN << 1]; int i,j,tot,a,b,l,n,m,u,q; int head[MAXN]; pair<int,int> ans; inline void addedge(int u,int v,int w) { tot++; e[tot] = (edge){v,w,head[u]}; head[u] = tot; } inline pair<int,int> dijkstra(int s) { int i,l,r,cur,ans1,ans2,v,w; static int dist[MAXN]; static bool visited[MAXN]; priority_queue< pair<int,int> > q; for (i = 0; i < n; i++) { dist[i] = INF; visited[i] = false; } q.push(make_pair(0,s)); dist[s] = 0; while (!q.empty()) { cur = q.top().second; q.pop(); if (visited[cur]) continue; visited[cur] = true; for (i = head[cur]; i; i = e[i].nxt) { v = e[i].to; w = e[i].w; if (dist[cur] + w < dist[v]) { dist[v] = dist[cur] + w; q.push(make_pair(-dist[v],v)); } } } ans1 = 0; for (i = 0; i < n; i++) { if (dist[i] != INF) ans1 = max(ans1,dist[i]); } ans2 = 0; for (i = 0; i < n; i++) { if (dist[i] == ans1) ans2++; } return make_pair(ans1,ans2); } int main() { scanf("%d%d%d",&n,&m,&q); for (i = 1; i <= m; i++) { scanf("%d%d%d",&a,&b,&l); if (l == 0) continue; addedge(a,b,l); addedge(b,a,l); } while (q--) { scanf("%d",&u); ans = dijkstra(u); printf("%d %d ",ans.first,ans.second); } return 0; }