uva 10397 Connect the Campus(最小生成树)
2016-09-06 12:16
435 查看
题目意思很明确,最小生成树,只是有些城市之间已经提前连接好了,不需要再计算了,把城市之间的权值设置为0就可以了
#include <iostream> #include <algorithm> #include <utility> #include <cstring> #include <cmath> #include <cstdio> using namespace std; #define INF 9999999 typedef pair<double,double> P; P point[800]; double grap[800][800]; double dis[800]; bool used[800]; int N,M; double prim() { memset(used,false,sizeof(used)); for(int i = 0; i < N; ++i) dis[i] = INF; dis[0] = 0; double res = 0; while(true) { int v = -1; for(int u = 0; u < N; ++u) if(!used[u] && (v == -1 || dis[v] > dis[u])) v = u; if(v == -1) break; used[v] = true; res += dis[v]; for(int u = 0; u < N; ++u) dis[u] = min(dis[u],grap[v][u]); } return res; } int main() { while(cin >> N) { for(int i = 0; i < N; ++i) cin >> point[i].first >> point[i].second; cin >> M; memset(grap,-1,sizeof(grap)); int a,b; for(int i = 0; i < M; ++i) { cin >> a >> b; grap[a-1][b-1] = grap[b-1][a-1] = 0; } for(int i = 0; i < N; ++i) { grap[i][i] = 0; for(int j = i+1; j < N; ++j) { if(grap[i][j] != 0) { grap[i][j] = grap[j][i] = sqrt((point[i].first-point[j].first)*(point[i].first-point[j].first) + (point[i].second-point[j].second)*(point[i].second-point[j].second)); } } } printf("%.2f\n",prim()); } return 0; }
相关文章推荐
- UVa:10397 Connect the Campus(最小生成树)
- UVa 10397 Connect the Campus (Kruskal+补全最小生成树)
- uva 10397 Connect the Campus(最小生成树kruskal)
- UVA - 10397 Connect the Campus (最小生成树)
- UVA 10397 - Connect the Campus(最小生成树)
- uva 10397 - Connect the Campus(最小生成树&并查集)
- uvaoj 10397 - Connect the Campus【最小生成树】
- Uva 10397 - Connect the Campus
- UVA 10397 Connect the Campus
- uva 10397 Connect the Campus 把学校连起来
- UVA 10397 - Connect the Campus
- uva 10397 Connect the Campus kruskal 算法变形
- UVA 10397 - Connect the Campus
- uva 10397【Connect the Campus】
- UVA 10397 Connect the Campus
- uva 10397 Connect the Campus
- UVA 10397 - Connect the Campus 翻译
- UVa 10397 - Connect the Campus
- UVa 10397 - Connect the Campus
- UVA 10397 Connect the Campus