The war
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2839 Accepted Submission(s): 686
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 题意:给一个连通的无向图,加上一条边后,需要删除一条边,使得这张图不连通思路:炸肯定要炸桥,先将图进行缩点,然后建树,树边是要炸的,找个权值最小的边,以这边的两个端点开始分别为树根,找两棵树中的各一条路径,使得路径包含的边尽量小,其实也就是找出所有节点与其子节点的边的次小值中的最小值。代码:#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <cmath> #include <map> #include <stack> using namespace std; const int N=10003; const int M=200010; const int inf=0x7fffffff; int dfn[M],low[M],belong[M]; bool instack[M]; int head[M]; int vis[M]; int cnt,index,ans,num; int n,m; stack<int>s; struct edge { int u,v,w; int next; }e[M]; vector<edge>ee[M]; void addedge(int u,int v,int w) { e[cnt].v=v; e[cnt].w=w; e[cnt].next=head[u]; head[u]=cnt++; e[cnt].v=u; e[cnt].w=w; e[cnt].next=head[v]; head[v]=cnt++; } void init() { memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(belong,0,sizeof(belong)); memset(vis,0,sizeof(vis)); memset(instack,false,sizeof(instack)); while(!s.empty()) s.pop(); for(int i=0;i<=n;i++) { ee[i].clear(); } num=cnt=index=0; } void tarjan(int u) { dfn[u]=low[u]=++index; s.push(u); instack[u]=true; int v; for(int i=head[u];i!=-1;i=e[i].next) { if(vis[i])continue; vis[i]=vis[i^1]=1; v=e[i].v; if(!dfn[v]) { tarjan(v); low[u]=min(low[v],low[u]); } else if(instack[v]) { low[u]=min(dfn[v],low[u]); } } if(dfn[u]==low[u]) { num++; do{ v=s.top(); s.pop(); belong[v]=num; instack[v]=false; }while(u!=v); } } int dfs(int u,int v) { int fm=inf,sm=inf; for(int i=0;i<ee[u].size();i++) { int k=ee[u][i].v; if(k==v)continue; int sum=min(ee[u][i].w,dfs(k,u)); if(sum<=fm) { sm=fm; fm=sum; } else sm=min(sum,sm); } ans=min(ans,sm); return fm; } edge build_tree() { edge tmp;tmp.w=inf; for(int i=1;i<=n;i++) { for(int j=head[i];j!=-1;j=e[j].next) { int u=belong[i]; int v=belong[e[j].v]; if(u==v)continue; edge newedge={u,v,e[j].w,0}; if(e[j].w<tmp.w) { tmp.u=u; tmp.v=v; tmp.w=e[j].w; } ee[u].push_back(newedge); } } return tmp; } int main() { while(~scanf("%d%d",&n,&m)) { init(); int i; int a,b,c; for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); } tarjan(1); edge tmp=build_tree(); ans=inf; if(num==1) { printf("-1\n"); continue; } dfs(tmp.u,tmp.v); dfs(tmp.v,tmp.u); if(ans==inf) { printf("-1\n"); } else printf("%d\n",ans); } return 0; }