UVALive - 4872 最小生成树
2017-05-13 18:06
381 查看
这个题看着像是一个图,于是我就当最小生成树树去搞了一下,然后一脸懵逼的A了
#include <iostream> #include <cmath> using namespace std; long long INF = 1000000; double cost[1005][1005]; double mincost[1005]; bool used[1005]; int vv; double prim(){ for(int i = 0; i< vv; ++i){ mincost[i] = INF; used[i] = false; } mincost[0] = 0; double res = 0; while(true){ int v = -1; for(int u = 0; u < vv; u++){ if(!used[u] && (v== -1 || mincost[u] < mincost[v])) v = u; } if(v == -1) break; used[v] = true; res += mincost[v]; for(int u = 0; u < vv; u++){ mincost[u] = min(mincost[u], cost[v][u]); } } return res; } int main(){ int n; while(scanf("%d",&n)&&n){ int x[n+1], y[n+1]; vv=0; for(int i = 0; i < n; i++){ scanf("%d%d", &x[vv], &y[vv]); for(int j = 0; j < vv; j++){ cost[j][vv] = sqrt((double)((x[vv]-x[j])*(x[vv]-x[j])+(y[vv]-y[j])*(y[vv]-y[j]))); cost[vv][j] = cost[j][vv]; } vv++; } double d = prim(); printf("%.2lf\n",d); } }
相关文章推荐
- UVALive 4872 Underground Cables 最小生成树
- UVALive 4872 Underground Cables 最小生成树
- UVALive 3662 Another Minimum Spanning Tree 曼哈顿最小生成树
- UVALive 3662 Another Minimum Spanning Tree [离散化+线段树+最小生成树]
- UVALive 3887 边权极差最小生成树模板
- 曼哈顿最小距离生成树(poj 3241&& UVALive 3662)
- UVALive - 5713 最小生成树
- 最小生成树应用uvalive5713
- UVAlive 3662 Another Minimum Spanning Tree 莫队算法,曼哈顿最小生成树
- UVALive-7303- Aquarium【最小生成树】【连通块】
- UVA Live 6437 Power Plant 最小生成树
- 【UVALive】3887 Slim Span 枚举+最小生成树
- UVALive - 4848 Tour Belt(暴力+最小生成树)
- UVALive-7303 Aquarium (最小生成树)
- UVALive 6437 Power Plant 【最小生成树 + 思维】
- UVALive 7303 Aquarium 最小生成树
- B - Bus Problem UVALive - 7001 最小生成树
- UVA Live 6437 Power Plant 最小生成树
- UVALive 6837 (最小生成树)
- Another Minimum Spanning Tree - UVaLive 3662 曼哈顿最小生成树