HDU 4738 Caocao's Bridges 桥+并查集

本文深入探讨了AI音视频处理领域中的关键技术和应用,特别是视频分割与语义识别技术。从理论基础到实际案例,为开发者提供了全面的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Caocao's Bridges

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


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 <= N 2 )

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



/** Author: ☆·aosaki(*’(OO)’*)  niconiconi★ **/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#define ALL(v) (v).begin(),(v).end()
#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)
#define SIZE(v) ((int)(v).size())
#define mem(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lp(k,a) for(int k=1;k<=a;k++)
#define lp0(k,a) for(int k=0;k<a;k++)
#define lpn(k,n,a) for(int k=n;k<=a;k++)
#define lpd(k,n,a) for(int k=n;k>=a;k--)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define lowbit(x) (x&(-x))
#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define PI acos(-1.0)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define TT cout<<"*****"<<endl;
#define TTT cout<<"********"<<endl;
inline int gcd(int a,int b)
{
    return a==0?b:gcd(b%a,a);
}

#define INF 1e8
#define eps 1e-8
#define mod 10007
#define maxn 1011
#define maxm 2000010

using namespace std;

int col[maxn];
int dfn[maxn],low[maxn],stack[maxn];
int pre[maxn];
int n,m,tot=0,cot,tm=0,index=0,top=0;
int pa[maxn];
bool instack[maxn];
bool cut[maxn];
int add_block[maxn];
int bridge;

void ms()
{
    lp(i,n) pa[i]=i;
}

struct Side
{
    int to,next;
    int w;
    bool cut;
}e[maxm];

int fin(int x)
{
    return (x==pa[x])?x:pa[x]=fin(pa[x]);
}

void uni(int x,int y)
{
    x=fin(x); y=fin(y);
    if(x==y) return;
    pa[y]=x;
}

void add(int u,int v,int w)
{
    e[tot].to=v;
    e[tot].next=pre[u];
    e[tot].w=w;
    e[tot].cut=0;
    pre[u]=tot++;
}

void tarjan(int u,int fa)
{
    int v;
    low[u]=dfn[u]=++index;
    stack[top++]=u;
    instack[u]=true;
    int son=0;
    int pre_num=0;
    for(int i=pre[u];i!=-1;i=e[i].next)
    {
        v=e[i].to;
        if(v==fa && pre_num==0)
        {
            pre_num++;
            continue;
        }
        if(!dfn[v])
        {
            son++;
            tarjan(v,u);
            if(low[u]>low[v]) low[u]=low[v];
            if(low[v]>dfn[u])
            {
                bridge++;
                e[i].cut=true;
                e[i^1].cut=true;
            }
            if(u!=fa && low[v]>=dfn[u])
            {
                cut[u]=true;
                add_block[u]++;
            }
        }
        else if(low[u]>dfn[v])
              low[u]=dfn[v];
    }
    if(u==fa && son>1) cut[u]=true;
    if(u==fa) add_block[u]=son-1;
    instack[u]=false;
    top--;
}

void init()
{
    mem(col);
    mem1(pre);
    mem(dfn);
    mem(low);
    mem(stack);
    mem(instack);
    mem(cut);
    mem(add_block);
    tot=0;
    top=0;
    tm=0;
    index=0;
    cot=0;
    bridge=0;
}

int main()
{
     //freopen("in.txt","r",stdin);
     int uu,vv,ww;
     while(~sc2(n,m) && n!=0)
     {
        ms();
        init();
        lp(i,m)
          {
             sc2(uu,vv);
             sc(ww);
            add(uu,vv,ww);
            add(vv,uu,ww);
            uni(uu,vv);

         }
        int f=0;
        lp(i,n)
        {
            if(fin(i)!=fin(1))
            {
                f=1;
                break;
            }
        }
        if(f)
        {
            printf("0\n");
            continue;
        }
       lp(i,n)
          if(!dfn[i])
          {
              tarjan(i,i);
          }
        int re=INF;
        for(int u=1;u<=n;u++)
            for(int i=pre[u];i!=-1;i=e[i].next)
                if(e[i].cut)
                    re=min(re,e[i].w);
        if(re==0)
        {
            printf("1\n");
            continue;
        }
        if(re==INF)
        {
            printf("-1\n");
            continue;
        }
        printf("%d\n",re);

     }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值