本文共 1578 字,大约阅读时间需要 5 分钟。
题目链接:
#include#include #include #include #include #include using namespace std;#define INF 0x3f3f3f3f#define MAXN 10005 #define MAXM 100005typedef long long ll;struct Edge{ int to, w; Edge(){} Edge(int pto, int pw){ to = pto; w = pw; } bool operator < (const Edge o)const{ return w > o.w; }};int n,m;ll dis[MAXN];bool vis[MAXN];int pre[MAXN];//记录某点直接到达该点的最小长度vector adj[MAXN];priority_queue pq;void dijkstra(int s) { memset(vis,false,sizeof(vis)); memset(dis,INF,sizeof(dis)); memset(pre,INF,sizeof(pre)); dis[s] = 0; pre[s] = 0; pq.push(Edge(s,dis[s])); while(!pq.empty()){ Edge tmp = pq.top(); pq.pop(); int cur = tmp.to;// if(cur == n){ //不能有这个 // return ;// } if(vis[cur]) continue; vis[cur] = true; for(int i = 0; i < adj[cur].size(); ++i){ Edge e = adj[cur][i]; if(!vis[e.to]){ if(dis[e.to] > dis[cur] + e.w){ dis[e.to] = dis[cur] + e.w; pre[e.to] = e.w; pq.push(Edge(e.to,dis[e.to])); } else if(dis[e.to] == dis[cur] + e.w){//到某个点最短距离相同时候,取最小的前驱 pre[e.to] = min(e.w,pre[e.to]); } } } } }int main() { scanf("%d%d",&n,&m); int f,t,w; for(int i = 0; i < m; ++i){ scanf("%d%d%d",&f,&t,&w); adj[f].push_back(Edge(t,w)); adj[t].push_back(Edge(f,w)); } dijkstra(1); ll ans = 0; for(int i = 2; i <= n; ++i){ ans += pre[i]; } printf("%d",ans); return 0; }
转载地址:http://yqimi.baihongyu.com/