CCF_201612-4_交通规划
http://115.28.138.223/view.page?gpid=T44
好像也没想象中的那么难,没办法,当初连个优先队列dij都不会写= =
在优先队列dij算法上加上相等的时候的处理就可以了。
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> #include<queue> using namespace std; struct node { int pos,cost; node(int a,int b):pos(a),cost(b){}; friend bool operator <(node x,node y) { return x.cost > y.cost; } }; struct edge { int to,cost; edge(int a,int b):to(a),cost(b){}; }; vector<edge> v[10005]; bool vis[10005] = {0}; int dis[10005],cost[10005],n,m; void dij() { memset(dis,0x3f,sizeof(dis)); memset(cost,0x3f,sizeof(cost)); dis[0] = 0; cost[0] = 0; priority_queue<node> q; q.push(node(1,0)); while(!q.empty()) { int now = q.top().pos; if(vis[now]) continue; vis[now] = 1; for(int i = 0;i < v[now].size();i++) { int to = v[now][i].to,c = v[now][i].cost; if(vis[to]) continue; if(dis[to] > dis[now]+c) { dis[to] = dis[now]+c; cost[to] = c; q.push(node(to,dis[to])); } else if(dis[to] == dis[now]+c) cost[to] = min(cost[to],c); } } } int main() { scanf("%d%d",&n,&m); for(int i = 1;i <= m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); v[a].push_back(edge(b,c)); v[b].push_back(edge(a,c)); } dij(); int ans = 0; for(int i = 2;i <= n;i++) ans += cost[i]; printf("%d\n",ans); return 0; }