Caocao's Bridges

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4254    Accepted Submission(s): 1337

Problem Description

Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and based on those islands, Caocao's army could easily attack Zhou Yu's troop. Caocao also built bridges connecting islands. If all islands were connected by bridges, Caocao's army could be deployed very conveniently among those islands. Zhou Yu couldn't stand with that, so he wanted to destroy some Caocao's bridges so one or more islands would be seperated from other islands. But Zhou Yu had only one bomb which was left by Zhuge Liang, so he could only destroy one bridge. Zhou Yu must send someone carrying the bomb to destroy the bridge. There might be guards on bridges. The soldier number of the bombing team couldn't be less than the guard number of a bridge, or the mission would fail. Please figure out as least how many soldiers Zhou Yu have to sent to complete the island seperating mission.

Input

There are no more than 12 test cases.
In each test case:
The first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 <= N <= 1000, 0 < M <= N2 )
Next M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 <= W <= 10,000 )
The input ends with N = 0 and M = 0.

Output

For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn't succeed any way, print -1 instead.

Sample Input

  1.  
  2. 3 3
  3. 1 2 7
  4. 2 3 4
  5. 3 1 4
  6. 3 2
  7. 1 2 7
  8. 2 3 4
  9. 0 0

Sample Output

  1.  
  2. -1
  3. 4

Source

2013 ACM/ICPC Asia Regional Hangzhou Online

Recommend

liuyiding

题意:

现在有个(可重边)无向图,无向图的每条边上都有一定数目的守卫,你现在想派人去炸掉这个图的一条边,是的该图不连通。但是你只能炸1条边且如果该边守卫为x人,那么你至少要派x个人过去。所以现在问你最少需要派多少人出发?

分析:

本题的本质还是无向图求桥,且求得是守卫数目最少的那个桥。但是本题有3个点要注意:

1.所给的图可能不连通,且不连通的时候不需要炸,输出0.

2.当所要去炸的桥上的守卫数=0时,我们需要派的人数是1不是0.

3.任意两个节点u与v之间可能存在多条边。

前两点都挺好解决的,无非是加判断语句

对于重边的问题,是这样处理的:

放弃一般的模版直接比较父节点的做法,去比较边与递归上一层的“父边”是否是同时添加进去的,即可能可在这个点返回其父节点(有重边时)

具体看代码:

  1. 1 #include<cstdio>
  2. 2 #include<cstring>
  3. 3 #include<algorithm>
  4. 4 using namespace std;
  5. 5 const int maxn=1000+10;
  6. 6 const int maxm=2*1000*1000+100;
  7. 7 int n,m;
  8. 8 int tot;
  9. 9 int head[maxn];
  10. 10 struct Edge
  11. 11 {
  12. 12 int to,next,w;
  13. 13 }edges[maxm];
  14. 14 void add_edge(int u,int v,int w)
  15. 15 {
  16. 16 edges[tot]=(Edge){v,head[u],w};
  17. 17 head[u]=tot++;
  18. 18 edges[tot]=(Edge){u,head[v],w};
  19. 19 head[v]=tot++;
  20. 20 }
  21. 21
  22. 22 int pre[maxn],low[maxn];
  23. 23 int dfs_clock,point_num;
  24. 24 int ans;
  25. 25 void tarjan(int u,int E)
  26. 26 {
  27. 27 low[u]=pre[u]=++dfs_clock;
  28. 28 for(int e=head[u];e!=-1;e=edges[e].next)
  29. 29 {
  30. 30 int v=edges[e].to;
  31. 31 if(e==(E^1)) continue;
  32. 32 if(!pre[v])
  33. 33 {
  34. 34 tarjan(v,e);
  35. 35 low[u]=min(low[u],low[v]);
  36. 36 if(low[v]>pre[u])
  37. 37 ans=min(ans,edges[e].w);
  38. 38 }
  39. 39 else low[u]=min(low[u],pre[v]);
  40. 40 }
  41. 41 point_num++;
  42. 42 }
  43. 43 int main()
  44. 44 {
  45. 45 while(scanf("%d%d",&n,&m)==2&&n)
  46. 46 {
  47. 47 ans=1000000;
  48. 48 dfs_clock=point_num=tot=0;
  49. 49 memset(pre,0,sizeof(pre));
  50. 50 memset(head,-1,sizeof(head));
  51. 51 for(int i=0;i<m;i++)
  52. 52 {
  53. 53 int u,v,w;
  54. 54 scanf("%d%d%d",&u,&v,&w);
  55. 55 add_edge(u,v,w);
  56. 56 }
  57. 57 tarjan(1,-1);
  58. 58 if(point_num<n) printf("0\n"); //图不连通,不用炸
  59. 59 else if(ans==1000000) printf("-1\n"); //图中无桥
  60. 60 else if(ans==0) printf("%d\n",1); //桥上兵为0
  61. 61 else printf("%d\n",ans);
  62. 62 }
  63. 63 return 0;
  64. 64 }

HDU 4738--Caocao's Bridges(重边无向图求桥)的更多相关文章

  1. HDU 4738 Caocao's Bridges(Tarjan求桥+重边判断)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥 ...

  3. HDU 4738 Caocao's Bridges taijan (求割边,神坑)

    神坑题.这题的坑点有1.判断连通,2.有重边,3.至少要有一个人背*** 因为有重边,tarjan的时候不能用子结点和父节点来判断是不是树边的二次访问,所以我的采用用前向星存边编号的奇偶性关系,用^1 ...

  4. 【HDU 4738 Caocao's Bridges】BCC 找桥

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一个n个节点m条边的无向图(可能不连通.有重边),每条边有一个权值.判断其连通性,若双 ...

  5. Hdu 4738 Caocao's Bridges (连通图+桥)

    题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...

  6. HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】

     Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 4738 Caocao's Bridges (tarjan求桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:给一些点,用一些边把这些点相连,每一条边上有一个权值.现在要你破坏任意一个边(要付出相 ...

  9. hdu 4738 Caocao's Bridges 图--桥的判断模板

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. C++学习 之 变量和常量的使用(笔记)

    一.变量 1.对变量含义的理解: 变量就像是经过工厂加工后有一定容量的容器.在变量定义时,系统充当了工厂的角色,按照类型为变量分配相应的空间.定义完成的变量可以存放相应类型的值,存放的值大于变量所能接 ...

  2. 树上选两点(使最短)树的直径+bfs

    题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...

  3. python网络编程——使用UDP、TCP协议收发信息

    UDP UDP是面向无连接的通讯协议,UDP数据包括目的端口号和源端口号信息,由于通讯不需要连接,所以可以实现广播发送. UDP传输数据时有大小限制,每个被传输的数据报必须限定在64KB之内. UDP ...

  4. Android开发build出现java.lang.NumberFormatException: For input string: "tle 0x7f0800aa"错误的解决方案

    查看异常栈没有发现项目代码的问题,因为问题是出现在layout文件中. 全局查找tle这个,发现在某个layout文件中title一词被变成ti tle了,结果Android就xjb报错了. 参考

  5. Spring 注入所得

    Spring在注入的时候 @Autowired @Qualifier(value = "inpatientInfoInInterService") private Inpatien ...

  6. Vue+axios 拦截,超时登录问题

    axios.interceptors.request.use(config => config, error => Promise.reject(error)); axios.interc ...

  7. [转自SA]浅谈nginx的工作原理和使用

    nginx apache 简单对比 nginx 相对 apache 的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而 apac ...

  8. 在Chrome中使用IE浏览器!

    学校的毕业论文系统,无法完整显示网页,特别是下部的"提交"按钮看不见. IE11无效. 发现"360极速浏览器 7.5.3.186"能正常显示,百度网盘下载太慢 ...

  9. opencart nginx静态化设置

    在niginx设置里添加下面代码,(lnmp的可能是 网址.conf文件添加) # SEO URL Settings # Nginx configuration of OC htaccess loca ...

  10. Woobuntu

    Wooyun + Ubuntu = Woobuntu Woobuntu是基于Ubuntu系统的一款安全研究环境配置工具,可以自动安装并配置众多的安全工具与依赖环境,此外还针对中国用户的习惯进行了一些优 ...