HDU4005 The war

HDU4005 The war

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

Total Submission(s): 775    Accepted Submission(s): 128

Problem Description

In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situation of the enemy's war zones, and known that these war zones can communicate to each other directly or indirectly through the network. We also know the enemy is going to build a new communication line to strengthen their communication network. Our task is to destroy their communication network, so that some of their war zones can't communicate. Each line has its "cost of destroy". If we want to destroy a line, we must spend the "cost of destroy" of this line. We want to finish this task using the least cost, but our enemy is very clever. Now, we know the network they have already built, but we know nothing about the new line which our enemy is going to build. In this condition, your task is to find the minimum cost that no matter where our enemy builds the new line, you can destroy it using the fixed money. Please give the minimum cost. For efficiency, we can only destroy one communication line.

 

Input

The input contains several cases. For each cases, the first line contains two positive integers n, m (1<=n<=10000, 0<=m<=100000) standing for the number of the enemy's war zones (numbered from 1 to n), and the number of lines that our enemy has already build. Then m lines follow. For each line there are three positive integer a, b, c (1<=a, b<=n, 1<=c<=100000), meaning between war zone A and war zone B there is a communication line with the "cost of destroy " c.

 

Output

For each case, if the task can be finished output the minimum cost, or output ‐1.

 

Sample Input

3 2

1 2 1

2 3 2

4 3

1 2 1

1 3 2

1 4 3

 

Sample Output

-1

3

Hint

For the second sample input: our enemy may build line 2 to 3, 2 to 4,

3 to 4. If they build line 2 to 3, we will destroy line 1 to 4, cost 3. If they

build line 2 to 4, we will destroy line 1 to 3, cost 2. If they build line 3 to 4,

we will destroy line 1 to 2, cost 1. So, if we want to make sure that we can

destroy successfully, the minimum cost is 3. 

*************************************************************************

题目大意:给定一个图,给这个图随机加上一条边,然后去破坏一条边使得图不连通,求保证能完成任务的最小花费。

用tarjan缩点形成一棵树,然后求出这个树中,每个节点的次小值儿子中的最小值。

这题的数据各种坑爹,我交了42次,看了别人的题解才勉强过,太坑爹啊。

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#define MIN(a,b) ((a)<(b)?(a):(b))
using namespace std;

const int N=10010;
const int E=200010;
vector<vector<int> >gra[N],tu[N];
vector<int>temp;
int fa[N],id,now,dfn[N],low[N],edge_up,vis[N],num;
int gro[N],ans;
struct Edge
{
    int le,ri,val;
    bool operator<(const Edge &a)const
    {
        return val>a.val;
    }
}edge[E];
priority_queue<Edge>que;
stack<int>sta;

void find(int s)
{
    int t;
    low[s]=0x3f3f3f3f;
    for(int i=0;i<tu[s].size();i++)
        if(!vis[t=tu[s][i][0]])
        {
            vis[t]=1,find(t);
            low[t]=MIN(low[t],tu[s][i][1]);
            if(low[t]<low[s])ans=MIN(ans,low[s]),low[s]=low[t];
            else ans=MIN(ans,low[t]);
        }
}

void tarjan(int s,int father)
{
    fa[s]=father;
    dfn[s]=low[s]=++now;
    sta.push(s);
    for(int i=0;i<gra[s].size();i++)
    {
        int e=gra[s][i][0];
        if(!dfn[e])
        {
            tarjan(e,s),low[s]=MIN(low[s],low[e]);
            if(low[e]>dfn[s])
            {
                id++;
                while(1)
                {
                    if(sta.empty()==1)break;
                    int t=sta.top();
                    gro[t]=id;
                    sta.pop();
                    if(t==e)break;
                }
                edge[edge_up].le=s;
                edge[edge_up].ri=e;
                edge[edge_up++].val=gra[s][i][1];
            }
        }
        else
            if(e!=father)
                low[s]=MIN(low[s],dfn[e]);
    }
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        while(sta.empty()==0)sta.pop();
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(gro,0,sizeof(gro));
        memset(fa,0,sizeof(fa));
        memset(edge,0,sizeof(edge));
        id=now=edge_up=0;
        for(int i=0;i<=n;i++)
            gra[i].clear(),tu[i].clear();
        for(int i=1;i<=m;i++)
        {
            int a,b,v;
            scanf("%d%d%d",&a,&b,&v);
            temp.clear();
            temp.push_back(b);
            temp.push_back(v);
            gra[a].push_back(temp);
            temp.clear();
            temp.push_back(a);
            temp.push_back(v);
            gra[b].push_back(temp);
        }
        int ddd=0;
        for(int i=1;i<=n;i++)
        {
            if(dfn[i])continue;
            ddd++;
            tarjan(i,0);
            id++;
            while(sta.empty()==0)
            {
                int t=sta.top();
                gro[t]=id;
                sta.pop();
            }
        }
        if(edge_up==0)
        {
            puts("-1");
            continue;
        }
        while(que.empty()==0)que.pop();
        for(int i=0;i<edge_up;i++)
        {
            int a=edge[i].le=gro[edge[i].le];
            int b=edge[i].ri=gro[edge[i].ri];
            int v=edge[i].val;
            que.push(edge[i]);
            temp.clear();
            temp.push_back(b);
            temp.push_back(v);
            tu[a].push_back(temp);
            temp.clear();
            temp.push_back(a);
            temp.push_back(v);
            tu[b].push_back(temp);
        }
        memset(fa,-1,sizeof(fa));
        int a,b,v;
        a=que.top().le;
        b=que.top().ri;
        v=que.top().val;
        que.pop();
        num=0;
        //dfs(a,b);dfs(b,a);
        ans=0x3f3f3f3f;
        memset(vis,0,sizeof(vis));
        vis[a]=vis[b]=1;
        find(a);find(b);
        printf("%d\n",ans<0x3f3f3f?ans:-1);
    }
}

  

转载于:https://www.cnblogs.com/Fatedayt/archive/2011/09/18/2180468.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值