UESTC 1146 秋实大哥与连锁快餐店
2016-03-16 21:10
281 查看
秋实大哥与连锁快餐店
Time Limit: 9000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Submit
Status
成大事者,不惟有超世之才,亦有坚忍不拔之志。
秋实大哥开了一家快餐店之后,由于人赢光环的影响,很快就赚得了大量的资金。为了继续实现心中的远大的理想,他打算在全国各地开设分店赚大
钱。假设现在有nn家快餐店(其中有至少有一家是旗舰店)分布在二维平面上,第i家快餐店的坐标为(xixi, yiyi)。为了方便交通,他打算在一些
快餐店之间修建道路使得任意一家快餐店都能够通过道路到达某一家旗舰店。
但是秋实大哥忙于赚钱和过节,没有时间来设计道路,你能帮助秋实大哥算出最少一共需要修建多长的道路吗?
Input
第一行一个整数n,表示快餐店的个数。(n≤6666)接下来n行,每行两个整数xi,yi,zi(−1000000≤xi,yi≤1000000
−1000000≤xi,yi≤1000000)。表示第i家快餐店的位置(xi,yi),如果zi=0表示该店是普通的分店,如果 zi=1表示该店是旗舰店。
保证至少有一家旗舰店
Output
输出最少一共需要修建的道路长度,保留小数点后两位。
Sample input and output
Sample Input | Sample Output |
---|---|
3 1 -1 0 1 1 0 0 0 1 | 2.83 |
Source
2015 UESTC Training for Graph TheoryThe question is from here.
My Solution
最小生成树 Prim算法 O(n^2);旗舰店与旗舰店距离为0;
TLE(3000ms) 了好多次,实验了各种改进,最后把直接求欧几里得距离的平方改成用 pow() 平方就 614 ms Accepted了……
这里算距离用至少N^2/2次,所以sqrt()也要这么多次,所以直接平方改用pow 平方就快出了好多(555555.............暂时只能这么解释了吧)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; const int maxn = 6669, INF = 0x3f3f3f3f; int n; // struct point { int x, y, condi; } po[maxn]; // double dis[maxn]; bool v[maxn]; //this problem is unique,so we don't use the vector<edge> g[maxn] inline double getdist(point a,point b) { return sqrt(pow(a.x-b.x,2) + pow(a.y-b.y,2)); } double prim() { memset(v, 0, sizeof v); for(int i = 1; i <= n; i++) dis[i] = INF; dis[1] = 0; double ans = 0; for(int i = 1; i <= n; i++){ int mark = -1; for(int j = 1; j <= n; j++) if(!v[j]){ if(mark == -1) mark = j; else if(dis[j] < dis[mark]) mark = j; } if(mark == -1) break; //all vertices are marked v[mark] = 1; ans += dis[mark]; for(int j = 1; j <= n; j++){ if(j == mark) continue; if(!v[j]){ dis[j] = min(dis[j], (po[mark].condi==1 && po[j].condi==1) ? 0 : getdist(po[mark],po[j]) ); } } } return ans; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d%d%d", &po[i].x, &po[i].y, &po[i].condi); } printf("%.2f", prim()); return 0; }
Thank you!
相关文章推荐
- Shape + Selector: Make a Shape as one item of the Selector
- windows客户端开发--duilib显示html
- windows客户端开发--duilib显示html
- CharSequence类
- Volley源码学习(三):线程处理request,类CacheDispatcher,NetworkDispatcher,ExecutorDelivery
- iOS UIView animation动画效果 (1)
- easyui 列表里下拉框联动
- UI进阶——SQL数据库
- android:finishOnTaskLaunch="true" //虽然这样设置了,但是Back键与Home键还是不同
- torque
- 给UIButton设置BackgroundColor:forState
- guice框架的入门使用
- (4.5.3.4)finished with non-zero exit value 2
- UI组件:PopupWindows的详细使用(一)
- U3D打包DLL插件 DLL Builder
- UITableView传值(自己使用)(属性,代理传值)
- iOS UITextField设置金额(钱数)输入框
- iOS--页面跳转(UITableView)
- NGUI中打字效果TypewriterEffect的一个BUG
- POJ 2229 Ultra-QuickSort 归并排序求逆序数