hdu 5253
2015-05-31 20:42
260 查看
没想到百度之星跪这么惨...
本以为很简单的最小生成树 结果爆内存 超时 re 各种错 连一个wa都没有 归根究底 居然只是以为一个Find 函数写错了 因为抄的以前的代码 所以一直t到死 都不知道怎么回事
本以为很简单的最小生成树 结果爆内存 超时 re 各种错 连一个wa都没有 归根究底 居然只是以为一个Find 函数写错了 因为抄的以前的代码 所以一直t到死 都不知道怎么回事
#include <iostream> #include <algorithm> #include <queue> #include <cstring> #include <cstdio> #include <vector> #include <string> #include <iterator> #include <cmath> #include <deque> #include <stack> #include <cctype> #include <iomanip> using namespace std; typedef long long ll; typedef long double ld; const int N = 1100; const int INF = 0xfffffff; const double EPS = 1e-8; const ll MOD = 1e9 + 7; const ld PI = acos (-1.0); #define INFL 0x7fffffffffffffffLL #define met(a, b) memset(a, b, sizeof(a)) #define put(a) cout << setiosflags(ios::fixed) << setprecision(a) struct node { int x, y, len; bool operator < (const node &a) const { return len < a.len; } }; int Find (int x); int Union (int x, int y); int kruskal (); int x , m, n, k; int father[N*N]; node ve[N*N*4]; int main () { int na, ca = 1; scanf ("%d", &na); while (na--) { k = 0; scanf ("%d%d", &m, &n); for (int i=0; i<m; i++) for (int j=0; j<n; j++) { node q; father[i*n+j] = i * n + j; scanf ("%d", &x[i][j]); if (j > 0) { q.x = i*n+j, q.y = i*n+j-1; q.len = abs (x[i][j] - x[i][j-1]); ve[k++] = q; } if (i > 0) { q.x = i*n+j, q.y = i*n+j-n; q.len = abs (x[i][j] - x[i-1][j]); ve[k++] = q; } } sort (ve, ve + k); printf ("Case #%d:\n", ca++); int xx = kruskal(); printf ("%d\n", xx); } return 0; } int Union (int x, int y) { int a = Find (x), b = Find (y); if (a != b) { father[a] = b; return 1; } return 0; } int kruskal () { node s; int i; int sum = 0; for (int i=0; i<k; i++) { if (Union (ve[i].x, ve[i].y)) sum += ve[i].len; } return sum; } int Find(int x) { return x == father[x] ? x : father[x] = Find (father[x]); }
int Find(int x) // 原先的写法 不知道以前是怎么过的 t到死 { return x == father[x] ? father[x] : Find (father[x]); }
相关文章推荐
- Android文字转语音
- 案例_用户通过配置文件运行指定的类和方法
- 公交查询系统测试计划
- 习惯七:不断更新---平衡的自我更新的原则
- 内存管理
- 第十二周上机项目2 摩托车继承自行车和机动车
- 简单的实现计算机功能代码如下
- 2015年大一下第12周项目4-链表类(1)
- Hilbert 空间填充曲线的产生式规则
- SMP
- AOP之代理模式(三)
- ECharts初识与Echarts-java类库应用
- 3.srs文档
- Windows Server 2012 之文件服务器(FTP)
- SHELL脚本if语句
- 浅析_tmain()与main()的区别
- http怎么加入超时和代理
- HDU 3966 Aragorn's Story
- 第五次作业
- Android-Activity状态保存