记录编号 479007 评测结果 AAAAAAAAAA
题目名称 最优布线问题 最终得分 100
用户昵称 GravatarAPWTMECRD 是否通过 通过
代码语言 C++ 运行时间 1.211 s
提交时间 2017-12-15 19:53:38 内存使用 8.73 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. bool flags;char ch;
  5. int read(int &x)
  6. {flags=false;
  7. while(ch=getchar(),ch<47 || 58<ch)if(ch=='-')flags=true;
  8. x=ch^'0';
  9. while(ch=getchar(),48<=ch && ch<=57)x=x*10+(ch^'0');
  10. if(flags)return -x;
  11. else return x;
  12. }
  13. int n,p[1501],ans,cnt;
  14. class node
  15. {public:
  16. int a,b,key;
  17. }s[1500001];
  18. int find(int u)
  19. {
  20. return p[u]==u ? u : p[u]=find(p[u]);
  21. }
  22. int sb()
  23. {
  24. for(int i=1;i<=n;i++)p[i]=i;
  25. for(int i=1;i<=cnt;i++)
  26. {
  27. int x=find(s[i].a);
  28. int y=find(s[i].b);
  29. if(p[x]!=p[y])
  30. {
  31. p[x]=p[y];
  32. ans+=s[i].key;
  33. }
  34. }
  35. }
  36. int cmp(node x,node y)
  37. {
  38. return x.key<y.key;
  39. }
  40. int LL()
  41. {
  42. freopen ("wire.in","r",stdin);
  43. freopen ("wire.out","w",stdout);
  44. read(n);
  45. int op;
  46. for(int i=1;i<=n;i++)
  47. {
  48. for(int j=1;j<=n;j++)
  49. {
  50. read(op);
  51. if(i>j && op)
  52. {
  53. s[++cnt].a=i;
  54. s[cnt].b=j;
  55. s[cnt].key=op;
  56. }
  57. }
  58. }
  59. sort(s+1,s+cnt+1,cmp);
  60. sb();
  61. printf("%d",ans);
  62. return 0;
  63. }
  64. int work=LL();
  65. int main(){;}