HDU-4738-Caocao's Bridges(割边)
来源:互联网 发布:ic卡扇区数据分析转换 编辑:程序博客网 时间:2024/05/17 07:54
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738
大致题意:无向边,带权值,让求权值最小的一个割边
注意以下几点:
1,多图:ans=0
2,割边最小权值=0 :ans=1(至少派一个士兵去炸桥)
3,无割边: ans=-1
4,重边的处理
//#include <bits/stdc++.h>#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int Scan(){ int res=0,ch,flag=0; if((ch=getchar())=='-')flag=1; else if(ch>='0'&&ch<='9')res=ch-'0'; while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0'; return flag?-res:res;}void Out(int a){ if(a>9)Out(a/10); putchar(a%10+'0');}#define INF 0x3f3f3f3f#define bug cout<<"bug"<<endlconst int MAXN = 1e5+7;struct node{ int v,next,cost; int vis;} edge[MAXN<<3];int head[MAXN],index;void add_edge(int u, int v, int cost){ edge[index].vis=0; edge[index].v=v; edge[index].cost=cost; edge[index].next=head[u]; head[u]=index++; edge[index].vis=0; edge[index].v=u; edge[index].cost=cost; edge[index].next=head[v]; head[v]=index++;}int belong[MAXN],DFN[MAXN],low[MAXN],stack_[MAXN];int cir,deep,top,temp;bool in_stack[MAXN],use[MAXN];void Tarjan(int u){ int p; DFN[u]=low[u]=++deep; in_stack[u]=1; stack_[top++]=u; temp++; for(int i=head[u]; i+1; i=edge[i].next) { int v=edge[i].v; if(edge[i].vis)continue; edge[i].vis=edge[i^1].vis=1; if(!DFN[v]) { Tarjan(v); low[u]=min(low[u],low[v]); } else if(in_stack[v]) low[u]=min(low[u],DFN[v]); } if(DFN[u]==low[u]) { cir++; do{ p=stack_[--top]; in_stack[p]=0; belong[p]=cir; } while(p!=u); }}int main(){ int n,m,k,a,b,c; int cas=1; while(scanf("%d%d",&n,&m)!=EOF) { if(!n)break; temp=cir=index=deep=top=0; memset(DFN,0,sizeof(DFN)); memset(head,-1,sizeof(head)); for(int i=0; i<m; ++i) { scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); } Tarjan(1); if(temp<n){printf("0\n");continue;} int ans=INF,flag=1; for(int i=1; i<=n; ++i) for(int j=head[i]; j+1; j=edge[j].next) { int v=edge[j].v; if(belong[i]!=belong[v] && edge[j].cost<ans) { flag=0; ans=edge[j].cost; } } if(ans==0)ans=1; if(flag)printf("-1\n"); else printf("%d\n",ans); } return 0;}/*4 31 2 11 2 33 4 44 41 2 11 2 33 4 42 4 6*/
0 0
- hdu - 4738 Caocao's Bridges 割边
- hdu 4738 Caocao's Bridges 割边无敌(trick)
- HDU-4738-Caocao's Bridges(割边)
- HDU 4738 Caocao's Bridges (割边)
- hdu 4738 Caocao's Bridges (割边/桥)
- hdu 4738 Caocao's Bridges 图的割边
- hdu 4738 Caocao's Bridges 【求最小代价的割边(桥)】
- HDU 4738Caocao's Bridges 邻接表tarjan 割桥
- HDU 4738 Caocao's Bridges(找割边)
- hdu 4738 Caocao's Bridges
- hdu 4738 Caocao's Bridges
- hdu 4738 Caocao's Bridges
- HDU:4738 Caocao's Bridges
- HDU 4738 Caocao's Bridges
- HDU 4738 Caocao's Bridges
- hdu 4738 - Caocao's Bridges
- hdu 4738 Caocao's Bridges
- HDU-4738Caocao's Bridges
- 图解用MySQL创建进销存数据库
- sql sever安装完成但失败后的正确姿势
- [从头学数学] 第216节 带着计算机去高考(八)
- 数据存储知识(3)--mysql主从服务器的搭建
- Spark读取Hbase中的数据
- HDU-4738-Caocao's Bridges(割边)
- Android day3:Java浮点类型、转义字符、布尔类型
- sql 相关
- 数据库设计
- Unity陀螺仪应用
- C++上机实验6--数组分离
- 关于margin叠加问题
- 第十三周項目-阅读项目-4
- 《重温最美古诗词》