HDU 4738 Caocao's Bridges (割边)

坑点:

  1. 一开始就是不连通的时候,不需要去炸桥,所以是0.
  2. 如果是联通,并且最小的桥的权值为0,那么至少也得派一个人去炸。。。
  3. 注意判断边的次数。
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1100;

struct edge{
    int to,v,next;
}ed[maxn*maxn*2];int cnte;
int head[maxn];
int cnt[maxn][maxn];
void ae(int x,int y,int v){
    ed[++cnte].to = y;
    ed[cnte].v = v;
    ed[cnte].next = head[x];
    head[x] = cnte;
}
int ans;
int dfn[maxn],low[maxn],vis[maxn],stak[maxn],cntc,cnts,index;
void dfs(int u,int f){
    dfn[u]=low[u]=++index;
    stak[cnts++] = u;
    vis[u] = 1;
    for(int i = head[u];i!=-1;i = ed[i].next){
        int to = ed[i].to;
        if(f == to) continue;
        if(!dfn[to]){
            dfs(to,u);
            low[u] = min(low[u],low[to]);
            if(dfn[u] < low[to] && cnt[u][to] == 1) {
                ans = min(ans,ed[i].v);
            }
        }
        else if(vis[to]){
            low[u] = min(low[u],dfn[to]);
        }
    }
    if(dfn[u]==low[u]){
        cntc++;int v;
        do{
            v = stak[--cnts];
            vis[v]=0;
        }while(v!=u);
    }
}

int n,m;
int tarjan(){
    int cnt2 = 0;
    for(int i = 1; i <= n;i++){
        if(!dfn[i]){
            dfs(i,-1);
            cnt2++;
        }
    }
    return cnt2;
}
void ini(){
    memset(head,-1,sizeof(head));memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));memset(vis,0,sizeof(vis));memset(cnt,0,sizeof(cnt));
    index = cnts = cntc = cnte = 0;
}
int main(){
    int a,b,val;
    while(~scanf("%d%d",&n,&m),n||m){
        ini();
        ans = 50000;
        for(int i = 1;i <= m;i++){
            scanf("%d%d%d",&a,&b,&val);
            ae(a,b,val);ae(b,a,val);
            cnt[a][b]++;cnt[b][a]++;
        }
        int cnt2 = tarjan();
        if(cnt2 != 1){
            printf("0\n");continue;
        }
        if(ans == 0) ans++;
        if(ans == 50000)
            puts("-1");
        else
            printf("%d\n",ans);
    }
}

你可能感兴趣的:(====,图论,=====,强连通/割边/割点)