博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU——4738 Caocao's Bridges
阅读量:6341 次
发布时间:2019-06-22

本文共 3806 字,大约阅读时间需要 12 分钟。

            Caocao's Bridges

              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

                    Total Submission(s): 5050    Accepted Submission(s): 1584

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
3 3 1 2 7 2 3 4 3 1 4 3 2 1 2 7 2 3 4 0 0
 
Sample Output
-1 4
 
题目:
周宇要炸调曹操的岛屿网络,是他们不相连,但是他只有一枚炸弹,所以他只能摧毁一座桥。周宇必须派一个人带炸弹来摧毁这座桥。桥上可能有守卫。轰炸队的士兵数量不能少于桥梁的守卫人数,否则任务将失败。请找出至少有多少士兵周宇要送到岛分离任务完成。
思路:
求出割边,求出边权最小的割边。如果没有割边,则无法完成任务,输出-1,如果割边不为1,我们求最小的割边的边权
有特殊情况要判断
1.这个图不一定是连通的,这样我们就不用去炸了
2.这个桥上可能没有人,但是我们还是要派一个人去炸桥
3.这个图没有割点,那就无法完成任务,输出-1
代码:
#include
#include
#include
#include
#include
#define N 1000010using namespace std;int n,m,x,y,z,s,tim,tot,ans=N;int dfn[N],low[N],vis[N],head[N],cut_edge[N],cut_point[N];struct Edge{ int from,to,next,dis;}edge[N];int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){
if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f;}int add(int x,int y,int z){ tot++; edge[tot].to=y; edge[tot].dis=z; edge[tot].next=head[x]; head[x]=tot;}int tarjan(int now,int pre){ int sum=0; bool boo=false; vis[now]=true; dfn[now]=low[now]=++tim; for(int i=head[now];i;i=edge[i].next) { int t=edge[i].to; if((pre^1)==i) continue; if(!dfn[t]) { sum++;tarjan(t,i); low[now]=min(low[now],low[t]); if(low[t]>dfn[now]) ans=min(ans,edge[i].dis); } else low[now]=min(low[now],dfn[t]); } s++;}void clean(){ ans=N,tim=0,tot=1,s=0; memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(vis,0,sizeof(vis)); memset(head,0,sizeof(head)); memset(cut_edge,0,sizeof(cut_edge));}int main(){ while(1) { n=read(),m=read(); clean(); if(n==0&&m==0) break; for(int i=1;i<=m;i++) x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z); tarjan(1,0); if(s

 

转载于:https://www.cnblogs.com/z360/p/7384013.html

你可能感兴趣的文章
.NET中使用Redis
查看>>
PHP 页面跳转的三种方式
查看>>
Juniper总结
查看>>
屏蔽scrollview的滚动
查看>>
面试题目3:智能指针
查看>>
取消凭证分解 (取消公司下的多个利润中心)
查看>>
flask ORM: Flask-SQLAlchemy【单表】增删改查
查看>>
vim 常用指令
查看>>
nodejs 获取自己的ip
查看>>
Nest.js 处理错误
查看>>
MD5的安全性
查看>>
你好,C++(16)用表达式表达我们的设计意图——4.1 用操作符对数据进行运算...
查看>>
[转] Mac下 快速写博客的软件 MarsEdit
查看>>
Unity的赛车游戏实现思路
查看>>
[Android UI] Shape详解 (GradientDrawable)
查看>>
边学边体验django--HttpRequest 对象
查看>>
18.3 redis 的安装
查看>>
jdbc 简单连接
查看>>
多态初步认识
查看>>
数组处理:118
查看>>