首页 > 代码库 > HDU 4005 The war Tarjan+dp

HDU 4005 The war Tarjan+dp

The war

 

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 21 2 12 3 24 31 2 11 3 21 4 3
 
Sample Output
 
-13
 
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 theybuild 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 candestroy successfully, the minimum cost is 3.
 
 
题意:
  给你一个n点m边的无向图
  有边权
  现在你可以选任意两个没有边相连的点连一条边,求新图的割边最小边的最大值
题解:
  考虑缩环之后就是一个树
  加一条边形成环,那么这个原树的最小边必然要在这个环内才能使得答案更加优
  找到这条边的两个端点,dfs这两个点,尽量走含有边权最小的链,这个dp处理即可
  dp[u][0/1]分别表示从u这个点开始走一条链含有的最小值和次小值
  最后就是两个端点走出的链的次小值取最小就是答案
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<vector>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")#define ls i<<1#define rs ls | 1#define pii pair<int,int>#define MP make_pairtypedef long long LL;const long long INF = 1e18;const double Pi = acos(-1.0);const int N = 1e4+10, M = 1e5, mod = 1e9+7, inf = 2e9;int ans,scc,t,top,tot,head[N],n,m,dp[N][2],a[N],b[N],c[N];struct edge{int id,to,next,value;}e[M];void add(int u,int v,int w) {e[t].next = head[u];e[t].to=v;e[t].value=http://www.mamicode.com/w;e[t].id=0;head[u]=t++;}int dfn[N],q[N],inq[N],low[N],belong[N],hav[N];vector<pair<int ,int > > G[N];void init() {        for(int i = 0; i <= n; ++i) dp[i][0] = dp[i][1] = inf;        for(int i = 1; i <= n; ++i) G[i].clear();        memset(hav,0,sizeof(hav));        memset(dfn,0,sizeof(dfn));        memset(head,-1,sizeof(head));        t = tot = top = scc = 0;}void dfs(int u) {        low[u] = dfn[u] = ++tot;        q[++top] = u; inq[u] = 1;        for(int i = head[u]; i!=-1; i = e[i].next) {            int to = e[i].to;            if(e[i].id) continue;            e[i].id = e[i ^ 1].id = 1;            if(!dfn[to]) {                dfs(to);                low[u] = min(low[u],low[to]);            } else if(inq[to]) low[u] = min(low[u],dfn[to]);        }        if(low[u] == dfn[u]) {            scc++;            do{                inq[q[top]] = 0;                belong[q[top]] = scc;            }while(u != q[top--]);        }}void dfs_ans(int u,int fa) {        if(u == -1) return ;        int fi = 0;        for(int i = 0; i < G[u].size(); ++i) {            int to = G[u][i].first;            int value =http://www.mamicode.com/ G[u][i].second;            if(to == fa) continue;            dfs_ans(to,u);           if(!fi) {                dp[u][0] = min(value,dp[to][0]);                dp[u][1] = dp[to][1];                fi = 1;            } else {                if(min(value,dp[to][0]) < dp[u][0])  dp[u][1] = min(dp[u][0],min(dp[to][1],dp[u][1])),dp[u][0] = min(value,dp[to][0]);                else dp[u][1] = min(dp[u][1],min(value,dp[to][0]));            }        }}void Tarjan() {        int mi = inf, s = -1, t = -1;        for(int i = 1; i <= n; ++i) if(!dfn[i]) dfs(i);        for(int i = 1; i <= m; ++i) {            int fx = belong[a[i]];            int fy = belong[b[i]];            if(fx != fy) {                G[fx].push_back(MP(fy,c[i]));                G[fy].push_back(MP(fx,c[i]));               // cout<<fx<<" "<<fy<<endl;                if(c[i] < mi) {                    s = fx,t = fy;                    mi = c[i];                }            }        }        ans = inf;        dfs_ans(s,t);        dfs_ans(t,s);        if(s != -1 && t != -1)ans = min(dp[s][1],dp[t][1]);        if(ans == inf) printf("%d\n",-1);        else printf("%d\n",ans);}int main() {        while(~scanf("%d%d",&n,&m)) {            init();            for(int i = 1; i <= m; ++i) {                scanf("%d%d%d",&a[i],&b[i],&c[i]);                add(a[i],b[i],c[i]);add(b[i],a[i],c[i]);            }            Tarjan();        }    return 0;}

 

HDU 4005 The war Tarjan+dp