CCF-交通规划
2016-11-09 20:42
169 查看
#include<cstdio> #include<queue> #include<vector> #define maxn 100005 #define inf 0x7fffffff int n,m; using namespace std; struct Point { int u; int dist; Point(int uu,int d){ u=uu,dist=d; } friend bool operator < (Point a,Point b) { return a.dist > b.dist; } }; struct Edge { int v; int cost; Edge(int vv,int c){ v=vv,cost=c; } }; vector<Edge> G[maxn]; bool vis[maxn]; int disto[maxn]; int costo[maxn]; void Dijkstra(int s) { for(int i=1;i<=n;i++){ vis[i]=false; disto[i]=costo[i]=inf; } disto[s]=0; costo[s]=0; priority_queue<Point> queue; queue.push(Point(s,0)); while(!queue.empty()){ Point p=queue.top(); queue.pop(); int u=p.u; if(!vis[u]){ for(int i=0;i<G[u].size();i++){ int v=G[u][i].v; int co=G[u][i].cost; if(!vis[v]){ if(disto[v]>disto[u]+co){ disto[v]=disto[u]+co; queue.push(Point(v,disto[v])); costo[v]=co; } if(disto[v]==disto[u]+co){ costo[v]=min(costo[v],co); } } } } } } int main() { int u,v,c; scanf("%d%d",&n,&m); while(m--){ scanf("%d%d%d",&u,&v,&c); G[u].push_back(Edge(v,c)); G[v].push_back(Edge(u,c)); } Dijkstra(1); int ans=0; for(int i=2;i<=n;i++) ans+=costo[i]; printf("%d",ans); }
相关文章推荐
- CCF考试——201609-4交通规划
- 201609-4 交通规划 ccf
- CCF 交通规划(最短路加边权维护)
- 交通规划 ccf
- CCF交通规划--SPFA和dist数组
- CCF之交通规划
- CCF-CSP 交通规划 JAVA 2016-09-04 100分
- CCF-交通规划(最短路变形->dj算法)
- CCF CSP 201609-4 交通规划
- CCF 201609-4 交通规划
- ccf 之交通规划 java版
- CCF习题 201612-4 交通规划 (Dijkstra + 贪心)
- CCF 201609-4 交通规划(BFS)
- 交通规划-CCF往届题(系统判断WA)
- CCF 交通规划 求解答
- CCF CSP 2016年9月第4题 交通规划(Dijkstra算法)
- CCF 201612-4 交通规划(spfa)
- 【CCF 交通规划 立flag中】
- ccf 201609-4 交通规划
- CCF-交通规划-dijkstra+贪心