标签:int html 包含 规模 算法 distance 链接 ack als
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址
#include <cstdio> #include <climits> #include <set> #include <vector> using namespace std; struct Edge { int x; // from int y; // to int v; // length int d; // distance from root Edge(int x_, int y_, int v_, int d_) : x(x_), y(y_), v(v_), d(d_) {} bool operator<(const Edge &b) const { if(d < b.d) return true; else if(d == b.d && v < b.v) return true; return false; } }; int main() { int N, M; scanf("%d%d", &N, &M); vector<vector<Edge> > graph(N+1, vector<Edge>()); for(int m=0; m<M; m++) { int x, y, v; scanf("%d%d%d", &x, &y, &v); graph[x].push_back(Edge(x,y,v,0)); graph[y].push_back(Edge(y,x,v,0)); } vector<int> dist(N+1, INT_MAX); vector<bool> visited(N+1); multiset<Edge> s; s.insert(Edge(1,1,0,0)); dist[1] = 0; int result = 0; while(!s.empty()) { Edge e = *s.begin(); //printf("E: %d %d %d %d\n", e.x, e.y, e.v, e.d); int from = e.y; s.erase(s.begin()); if(visited[from]) continue; visited[from] = true; result += e.v; for(int i=0; i<graph[from].size(); i++) { int to = graph[from][i].y; int v = graph[from][i].v; if(dist[from] + v <= dist[to]) { dist[to] = dist[from] + v; s.insert(Edge(from, to, v, dist[to])); } } } printf("%d", result); }
标签:int html 包含 规模 算法 distance 链接 ack als
原文地址:http://www.cnblogs.com/meelo/p/7638558.html