P3905 道路重建
我一开始想错了,我的是类似kruskal,把毁坏的边从小到大加,并且判断联通性。但是这有一个问题,你可能会多加,就是这条边没用,但是它比较小,你也加上了。
居然还有10分,数据也是水水的。。。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<ctime>
  7. #include<cstring>
  8. #define inf 2147483647
  9. #define For(i,a,b) for(register int i=a;i<=b;i++)
  10. #define p(a) putchar(a)
  11. #define g() getchar()
  12. //by war
  13. //2017.10.10
  14. using namespace std;
  15. int n,m,dd,A,B,x,y,v,t;
  16. int a[][];
  17. bool b[][];
  18. int d[];
  19. int ans;
  20.  
  21. struct node
  22. {
  23. int l,r,v;
  24. bool operator<(const node&aa)const
  25. {
  26. return v<aa.v;
  27. }
  28. }e[];
  29.  
  30. void in(int &x)
  31. {
  32. char c=g();x=;
  33. while(c<''||c>'')c=g();
  34. while(c<=''&&c>='')x=x*+c-'',c=g();
  35. }
  36. void o(int x)
  37. {
  38. if(x>)o(x/);
  39. p(x%+'');
  40. }
  41.  
  42. int find(int x)
  43. {
  44. if(d[x]==x)return x;
  45. d[x]=find(d[x]);
  46. return d[x];
  47. }
  48.  
  49. int main()
  50. {
  51. in(n),in(m);
  52. For(i,,n)
  53. d[i]=i;
  54. For(i,,m)
  55. {
  56. in(x),in(y),in(v);
  57. a[x][y]=v;
  58. a[y][x]=v;
  59. }
  60. in(dd);
  61. For(i,,dd)
  62. {
  63. in(x),in(y);
  64. b[x][y]=true;
  65. b[y][x]=true;
  66. }
  67. in(A),in(B);
  68. For(i,,n)
  69. For(j,,n)
  70. {
  71. if(!b[i][j]&&a[i][j]>)
  72. d[find(i)]=find(j);
  73. if(b[i][j])
  74. {
  75. e[++t].l=i;
  76. e[t].r=j;
  77. e[t].v=a[i][j];
  78. }
  79. }
  80. sort(e+,e+t+);
  81. For(i,,t)
  82. {
  83. if(find(A)!=find(B))
  84. {
  85. if(find(e[i].l)!=find(e[i].r))
  86. {
  87. d[find(e[i].l)]=find(e[i].r);
  88. ans+=e[i].v;
  89. }
  90. }
  91. else
  92. {
  93. o(ans);
  94. break;
  95. }
  96. }
  97. return ;
  98. }

正解是把未坏的边的权值设成0,坏的边的值不变,跑spfa即可。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<ctime>
  7. #include<cstring>
  8. #define inf 2147483647
  9. #define len 10010
  10. #define For(i,a,b) for(register int i=a;i<=b;i++)
  11. #define p(a) putchar(a)
  12. #define g() getchar()
  13.  
  14. using namespace std;
  15. int n,m,x,y,v,dd,A,B;
  16. queue<int>q;
  17. int d[];
  18. bool vis[],b[][];
  19.  
  20. struct node
  21. {
  22. int n,v;
  23. node *next;
  24. }*e[len];
  25.  
  26. void push(int x,int y,int v)
  27. {
  28. node *p;
  29. p=new node();
  30. p->n=y;
  31. p->v=v;
  32. if(e[x]==NULL)
  33. e[x]=p;
  34. else
  35. {
  36. p->next=e[x]->next;
  37. e[x]->next=p;
  38. }
  39. }
  40.  
  41. void in(int &x)
  42. {
  43. int y=;
  44. char c=g();x=;
  45. while(c<''||c>'')
  46. {
  47. if(c=='-')
  48. y=-;
  49. c=g();
  50. }
  51. while(c<=''&&c>='')x=x*+c-'',c=g();
  52. x*=y;
  53. }
  54.  
  55. void o(int x)
  56. {
  57. if(x<)
  58. {
  59. p('-');
  60. x=-x;
  61. }
  62. if(x>)o(x/);
  63. p(x%+'');
  64. }
  65.  
  66. void spfa(int x)
  67. {
  68. For(i,,n)
  69. d[i]=inf;
  70. d[x]=;
  71. q.push(x);
  72. node *p;
  73. int t;
  74. while(q.size()>)
  75. {
  76. t=q.front();
  77. p=e[t];
  78. vis[t]=true;
  79. while(p!=NULL)
  80. {
  81. if(!b[t][p->n])
  82. {
  83. if(d[t]<d[p->n])
  84. {
  85. d[p->n]=d[t];
  86. if(!vis[p->n])
  87. q.push(p->n);
  88. }
  89. }
  90. else
  91. {
  92. if(d[t]+p->v<d[p->n])
  93. {
  94. d[p->n]=d[t]+p->v;
  95. if(!vis[p->n])
  96. q.push(p->n);
  97. }
  98. }
  99. p=p->next;
  100. }
  101. vis[t]=false;
  102. q.pop();
  103. }
  104. }
  105.  
  106. int main()
  107. {
  108. in(n),in(m);
  109. For(i,,m)
  110. {
  111. in(x),in(y),in(v);
  112. push(x,y,v);
  113. push(y,x,v);
  114. }
  115. in(dd);
  116. For(i,,dd)
  117. {
  118. in(x),in(y);
  119. b[x][y]=true;
  120. b[y][x]=true;
  121. }
  122. in(A),in(B);
  123. spfa(A);
  124. o(d[B]),p(' ');
  125. return ;
  126. }

P3905 道路重建的更多相关文章

  1. 洛谷——P3905 道路重建

    P3905 道路重建 题目描述 从前,在一个王国中,在n个城市间有m条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有d条道路被破坏了.国王想要修复国家的道路系统,现 ...

  2. 洛谷 P3905 道路重建 题解

    P3905 道路重建 题目描述 从前,在一个王国中,在\(n\)个城市间有\(m\)条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有\(d\)条道路被破坏了.国王想 ...

  3. P1359 租用游艇 && P3905 道路重建 ------Floyd算法

    P1359 租用游艇   原题链接https://www.luogu.org/problemnew/show/P1359 P3905 道路重建   原题链接https://www.luogu.org/ ...

  4. 洛谷 P3905 道路重建

    题目描述 从前,在一个王国中,在n个城市间有m条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有d条道路被破坏了.国王想要修复国家的道路系统,现在有两个重要城市A和B ...

  5. 洛谷P3905 道路重建

    题目:https://www.luogu.org/problemnew/show/P3905 分析: 此题是显然的最短路算法,只是看到一起删掉的一堆边感到十分棘手,而且还要求出的是最短添加边的总长度 ...

  6. 【最短路】道路重建 @upcexam5797

    时间限制: 1 Sec 内存限制: 128 MB 题目描述 小L的家乡最近遭遇了一场洪水,城市变得面目全非,道路也都被冲毁了.生活还要继续,于是市政府决定重建城市中的道路. 在洪水到来前,城市中共有n ...

  7. [JZOJ 5465] [NOIP2017提高A组冲刺11.9] 道路重建 解题报告 (e-dcc+树的直径)

    题目链接: http://172.16.0.132/senior/#main/show/5465 题目: 小X所居住的X国共有n个城市,有m条无向道路将其连接.作为一个统一的国家,X 城的任意两个城市 ...

  8. 【洛谷P1272】道路重建

    题目大意:给定一个 N 个节点的树,求至少剪掉多少条边才能使得从树中分离出一个大小为 M 的子树. 题解:考虑树形 dp,定义 \(dp[u][i][t]\) 为以 u 为根节点与前 i 个子节点构成 ...

  9. [JZOJ5465]道路重建--边双缩点+树的直径

    题目链接 lueluelue 分析 这鬼题卡了我10发提交,之前做过一道类似的题目:https://rye-catcher.github.io/2018/07/09/luogu%E9%A2%98%E8 ...

随机推荐

  1. docker maven 出错:Failed to execute goal com.spotify:docker-maven-plugin:...: Request error: POST https://192.168.99.100:2376/build?t=

    Spring Boot项目构建docker镜像,出错Failed to execute goal com.spotify:docker-maven-plugin:0.4.13:build (defau ...

  2. Windows完成端口与Linux epoll技术简介

    收藏自:http://www.cnblogs.com/cr0-3/archive/2011/09/09/2172280.html WINDOWS完成端口编程1.基本概念2.WINDOWS完成端口的特点 ...

  3. saltstack主机管理项目【day23】:主机管理项目需求分析-设计

    本节内容 一. 主机管理项目需求分析 二 .主机管理项目架构设计 三.主机管理项目初始构建 四. 主机管理项目编主机分发器 一. 主机管理项目需求分析 场景:我现在又一台裸机要实现一下人物 配置管理: ...

  4. Dubbo学习笔记4:服务消费端泛化调用与异步调用

    本文借用dubbo.learn的Dubbo API方式来解释原理. 服务消费端泛化调用 前面我们讲解到,基于Spring和基于Dubbo API方式搭建简单的分布式系统时,服务消费端引入了一个SDK二 ...

  5. Java Web之路(二)Servlet之HttpServletResponse和HttpServletRequest

    HttpServletResponse 1.告诉服务器应用使用UTF-8解析文本的两种方式,告诉客户端要使用什么编码 response.setHeader("content-type&quo ...

  6. nodejs图片总结

    今天终于把朴灵老师写的<深入浅出Node.js>给学习完了, 这本书不是一本简单的Node入门书籍,它没有停留在Node介绍或者框架.库的使用层面上,而是从不同的视角来揭示Node自己内在 ...

  7. 【转】把Git Repository建到U盘上去

    CHENYILONG Blog 把Git Repository建到U盘上去 转 把Git Repository建到U盘上去 Git很火.原因有三: 它是大神Linus Torvalds的作品,天然地具 ...

  8. 游程编码(Run Length Code)

    一.什么是游程编码 游程编码是一种比较简单的压缩算法,其基本思想是将重复且连续出现多次的字符使用(连续出现次数,某个字符)来描述. 比如一个字符串: AAAAABBBBCCC 使用游程编码可以将其描述 ...

  9. Mysql备份文件

  10. python 获取二进制文件

    import requests response = requests.get('https://www.baidu.com/aladdin/img/tools/ip.png')with open(' ...