Caocao's Bridges

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

Total Submission(s): 1231    Accepted Submission(s): 478

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
 
Source

图的连通性问题~之求桥模板

#include "stdio.h"  //本人觉得两点之间可能有多条边,这样的话,邻接矩阵就没法存了,转邻接表~
#include "string.h" #define N 1005
#define INF 0x3fffffff struct node
{
int x,y;
int weight;
int next;
}edge[4*N*N];
int idx,head[N]; int set[N]; //并查集用 int root; bool mark[N],visit[N];
int low[N],dfn[N];
int stackk[4*N*N],num; int MIN(int a,int b){ return a<b?a:b; }
void Init(){ idx=0; memset(head,-1,sizeof(head)); }
int find(int x){ return set[x]==x?set[x]:set[x]=find(set[x]); } void Add(int x,int y,int weight)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = weight;
edge[idx].next = head[x];
head[x] = idx++;
} void Union(int x,int y)
{
int fa = find(x);
int fb = find(y);
if(fa!=fb)
set[fa] = fb;
} void DFS(int x,int times,int edge_father)
{
int i,y;
int child=0;
visit[x] = true;
low[x] = dfn[x] = times;
for(i=head[x]; i!=-1; i=edge[i].next)
{
y=edge[i].y;
if(!visit[y])
{
child++;
DFS(y,times+1,i);
low[x] = MIN(low[x],low[y]);
//if(x==root && child==2) mark[root] = true; //记录顶点是否为割顶
//if(x!=root && low[y]>=dfn[x]) mark[x] = true; //记录顶点是否为割顶
if(low[y]>dfn[x]) stackk[num++] = i; //若边i为桥,存入stackk[];
}
else if(edge_father!=-1 && i!=(edge_father^1)) //若当前边不为原来他father到他的边,更新low[x];
low[x] = MIN(low[x],dfn[y]);
}
} int solve(int n)
{
int i,ans = INF;
num = 0;
memset(visit,false,sizeof(visit)); //标记点是否已访问
//memset(mark,false,sizeof(mark));
root = 1;
int times = 1;
DFS(root,times,-1);
for(i=0; i<num; ++i)
ans = MIN(ans,edge[stackk[i]].weight);
if(ans==INF) ans=-1; //不存在桥,ans=-1;
if(ans==0) ans++; //如果桥权值是零,则最少要派一人去炸桥
return ans;
} int main()
{
int i;
int n,m;
int x,y,k;
while(scanf("%d %d",&n,&m),n&&m)
{
Init();
for(i=1; i<=n; ++i) set[i] = i; //并查集用
while(m--)
{
scanf("%d %d %d",&x,&y,&k);
if(x==y) continue;
Add(x,y,k);
Add(y,x,k);
Union(x,y); //合并两点
}
bool flag = true;
for(i=1; i<=n; ++i) //有一个点不连通,则flag为false;
{
if(find(i) != find(1))
flag = false;
}
if(!flag){ printf("0\n"); continue; }
printf("%d\n",solve(n));
}
return 0;
}

//后加上的代码~~

#include "stdio.h"
#include "string.h" #define N 1010
#define INF 0x3fffffff struct node
{
int x,y;
bool visit;
int weight;
int next;
}edge[2*N*N];
int idx,head[N]; int n,m;
int time;
int low[N],dfn[N];
bool mark[N];
int st[2*N*N],num; //存割边的编号 int MIN(int x,int y){ return x<y?x:y; }
void Init(){idx=0; memset(head,-1,sizeof(head)); }
void Add(int x,int y,int k)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].visit = false; //该条边未被访问
edge[idx].weight = k;
edge[idx].next = head[x];
head[x] = idx++;
} int set[N];
int find(int x);
void Union(int x,int y); void DFS(int x)
{
int i,y;
low[x] = dfn[x] = ++time;
for(i=head[x]; i!=-1; i=edge[i].next)
{
y = edge[i].y;
if(edge[i].visit) continue; //该边已经访问过,continue;
edge[i].visit = edge[i^1].visit = true;
if(!dfn[y])
{
DFS(y);
low[x] = MIN(low[x],low[y]);
if(low[y]>dfn[x])
st[num++] = i;
}
else
low[x] = MIN(low[x],dfn[y]);
}
} int Solve()
{
int ans = INF;
int i,j;
time = 0;
num = 0;
memset(dfn,0,sizeof(dfn));
DFS(1);
for(i=0; i<num; ++i)
{
if(ans>edge[st[i]].weight)
ans = edge[st[i]].weight;
}
if(ans==INF) ans=-1;
if(ans==0) ans=1;
return ans;
} int main()
{
bool flag;
int i,j;
int x,y,k;
while(scanf("%d %d",&n,&m),n||m)
{
Init();
for(i=1; i<=n; ++i) set[i] = i;
for(i=1; i<=m; ++i)
{
scanf("%d %d %d",&x,&y,&k);
Add(x,y,k);
Add(y,x,k);
Union(x,y);
}
flag = true;
for(i=1; i<=n; ++i)
{
if(find(i)!=find(1))
flag = false;
}
if(!flag) { printf("0\n"); continue; } //图不连通,派0个人去
printf("%d\n",Solve());
}
return 0;
} int find(int x)
{
return set[x]==x?set[x]:set[x]=find(set[x]);
}
void Union(int x,int y)
{
int fa = find(x);
int fb = find(y);
if(fa!=fb)
set[fa] = fb;
}

hdu 4738 Caocao's Bridges 图--桥的判断模板的更多相关文章

  1. hdu 4738 Caocao's Bridges(桥的最小权值+去重)

    http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有一些岛屿被桥连接,每座都有士兵把守,周瑜想把这些岛屿分成两部分,但他只能炸毁一条桥,问最少 ...

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

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

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

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

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

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

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

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

  6. HDU——4738 Caocao's Bridges

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

  7. HDU 4738 Caocao's Bridges

    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. 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)

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

随机推荐

  1. ASP.NET MVC使用input标签上传文件

    有些时间学习了,温习一下ASP.NET MVC了.上传文档是在开发过程中,必须撑握的一个功能.以前上传均是使用第三方控件uploadify来实现,今天使使用VS标准标签input 的type=&quo ...

  2. jQuery的事件change

    人生还在继续,只有不断补充以前所不懂的知识,今天练习一个jQuery的事件change.这个事件是在对象失去focus并且原本值有所变化时就产生此事件.如select时,用户所选择的选项有变时,或是t ...

  3. iOS7 UI兼容 导航栏按钮边框 UINavigationItem left and right padding

    iOS7之前的UI为: 而在iOS7中,由于设计方面的原因,使得UI变为: 修改的方法重写UINavigationItem的setLeftBarButtonItem和setRightBarButton ...

  4. WebApi传参总动员(二)

    上篇,从最简单的string入手.本篇演示了从请求的输入流中获取实体.api: public class ValuesController : ApiController { [HttpPost] p ...

  5. WinForm给控件加入hint文字

    本文代码主要是参考别人的,仅为个人记录,方面后续使用~ 效果图: 主要代码在一个Win32Utility类中,代码如下: public static class Win32Utility { [Dll ...

  6. C语言范例学习03-下

    树与图 3.5 二叉树及其应用 PS:二叉树是最经典的树形结构,适合计算机处理,具有存储方便和操作灵活等特点,而且任何树都可以转换成二叉树. 实例101 二叉树的递归创建 实例102 二叉树的遍历 问 ...

  7. nginx跨域处理

    http://www.nginx.cn/nginx-download nginx.conf配置 if ($request_method = ‘OPTIONS’) {         add_heade ...

  8. SQL Server SQL语句执行顺序

    执行顺序: 1.FROM:对FROM子句中前两个表执行笛卡尔积生成虚拟表vt1 2.ON:对vt1表应用ON筛选器只有满足 为真的行才被插入vt2 3.OUTER(join):如果指定了 OUTER ...

  9. 每一个成功的程序员的身后都有一个--------Parse

    相信好多同行都用过Parse,而正是因为Parse给我们的开发带来的极大的便利,才有了项目从零开始,到正式上线仅仅用上不到两周的时间,现在Swift还在迅速的发展,很快就会占有大量的市场,现在就就结合 ...

  10. 基于UML项目的分析与设计

    1,概述 项目中需求和设计的文档是必然的,UML工具可以帮助指导我们从不同的角度去看待一个新的系统,并把这个系统分解剖析出来.本篇文章主要讲述的是如何将UML应用到项目的开发工作中,关于如何学习UML ...