Caocao's Bridges HDU - 4738
@[toc]

题目:

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

题意:

n个点,m个边,每个边有权值,我们现在要取消一个边,使得一个或者多个点与其他点分离,但是取消边必须要花费大于等于边权的能量值,问最少花费多少能量值?如果不存在这样的边,则输出-1

题解:

先tarjan求强连通分量,如果只有一个则直接输出-1
如果原图都不连通,那我们都不需要拆桥,直接输出0
然后进行缩点,缩点后记录每个点之前的边的权值,直接输出最小值

代码:

我自己写了个代码,错总是有个地方错误(在判断原图是否连通那里),无奈只好借鉴别人的ac代码
本人代码

//( 2 <= N <= 1000, 0 < M <= N 2 )
#include<iostream>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#define mem(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
const int MAXN=2004;
struct Node{
    int to,next,w,fool;
}edge[MAXN*MAXN];
int tot,head[MAXN];
int index,vis[MAXN],dfn[MAXN],low[MAXN];
int color[MAXN];
int block;
int n,m;
stack<int>q;
void add(int x,int y,int z)
{
    edge[tot].to=y;
    edge[tot].fool=0;
    edge[tot].next=head[x]; 
    edge[tot].w=z;
    head[x]=tot++;
}
void tarjan(int u,int pre)
{
    dfn[u]=low[u]=++index;
    q.push(u);
    vis[u]=1;
    for(int i=head[u];i;i=edge[i].next)
    {
        int v=edge[i].to;
        if((i^1)==pre)continue;
        if(!dfn[v])
        {
            tarjan(v,i);
            low[u]=min(low[u],low[v]);
            if(low[v]>low[u])
            {
                edge[i].fool=edge[i^1].fool=1;//记录我们要找的桥 
            }
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        block++;
        int v;
        do{
            v=q.top();
            vis[v]=0;
            q.pop();
            color[v]=block;
        }while(v!=u);
    }
}
void init()
{
    tot=0;
    mem(head);
    mem(dfn);
    mem(low);
    mem(color);
    mem(vis);
    block=0;
    index=0;
}
int main()
{

    while(cin>>n>>m&&(n||m))

    {
        init();
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            cin>>u>>v>>w;
            add(u,v,w);
            add(v,u,w);
        }
        int time=0;
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
            {
                tarjan(i,-1);
                time++;
            }
        }
        int ans=1e9+2;
        if(time>1)
        {
    ///        cout<<"0"<<endl;
    //        cout<<time<<endl; 
        }
        if(block==1)printf("-1\n");//原图为边双联通图,则不能实现
        else 
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=head[i];j;j=edge[j].next)
                {
                    if(edge[i].fool)
                    ans=min(ans,edge[i].w);
                 } 
            }
            if(ans==0)ans=1;
            cout<<ans<<endl;
        }
    }
    return 0;
} 
/*
3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0
*/

借鉴代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 1e3+10;

struct Edge
{
    int to, next, w;
    bool iscut;
}edge[MAXN*MAXN*2];
int tot, head[MAXN];

int index, low[MAXN], dfn[MAXN];
int top, Stack[MAXN], instack[MAXN];
int block, belong[MAXN];

void addedge(int u, int v, int w)
{
    edge[tot].iscut = false;
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].next = head[u];
    head[u] = tot++;
}

void Tarjan(int u, int pre)
{
    low[u] = dfn[u] = ++index;
    Stack[top++] = u;
    instack[u] = true;
    for(int i = head[u]; i!=-1; i = edge[i].next)
    {
        if((i^1)==pre) continue;
        int v = edge[i].to;
        if(!dfn[v])
        {
            Tarjan(v, i);
            low[u] = min(low[u], low[v]);
            if(low[v]>low[u])
                edge[i].iscut = edge[i^1].iscut = true;
        }
        else if(instack[v])
            low[u] = min(low[u], dfn[v]);
    }

    if(low[u]==dfn[u])
    {
        block++;
        int v;
        do
        {
            v = Stack[--top];
            instack[v] = false;
            belong[v] = block;
        }while(v!=u);
    }
}

void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));

    index = 0;
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));

    block = top = 0;
    memset(instack, false, sizeof(instack));
}

int main()
{
    int n, m;
    while(scanf("%d %d",&n,&m) && (n||m))
    {
        init();
        for(int i = 1; i<=m; i++)
        {
            int u, v, w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u, v, w);
            addedge(v, u, w);
        }

        int times = 0;
        for(int i = 1; i<=n; i++)
            if(!dfn[i])
                Tarjan(i, -1), times++;

        if(times>1) //原图不连通,则不需要派人去毁桥。
        {
            printf("%d\n", 0);
            continue;
        }

        if(block==1)    //原图为边双联通图,则不能实现
        {
            printf("%d\n", -1);
            continue;
        }

        int ans = INF;
        for(int u = 1; u<=n; u++)
        for(int i = head[u]; i!=-1; i = edge[i].next)
            if(edge[i].iscut)
                ans = min(ans, edge[i].w);

        printf("%d\n", ans==0?1:ans);   //需要特判是否为0
    }
}