HDU

来源:互联网 发布:ct重建算法 编辑:程序博客网 时间:2024/06/08 04:25

The war

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2815    Accepted Submission(s): 673


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.
 

Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
 

     题意: 给一幅图 敌人会加一条边,你要删一条边,使得图不连通且花费费用最少

    分析: 缩点之后,从最小边的两个端点 dfs  找到里面的次小边就是答案啦~~

AC 代码:
#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<stack> #include<algorithm>#define inf 1e9using namespace std;struct edge{int u,v,w;edge(){}edge(int uu,int vv,int ww){u=uu,v=vv,w=ww;}};vector<edge>G,Gs;vector<int>v[20020];int low[20010],viss[20010],dfn[20010];int cnt,index;void init(){G.clear();Gs.clear();for(int i=0;i<=15010;i++)v[i].clear();memset(dfn,0,sizeof(dfn));memset(viss,0,sizeof(viss));cnt=0,index=0;}void addedge(int from,int to,int val){G.push_back(edge(from,to,val));G.push_back(edge(to,from,val));int m=G.size();v[from].push_back(m-2);v[to].push_back(m-1);}stack<int>S;vector<int>vs[20010];void tarjan(int x,int pre){dfn[x]=low[x]=++index;S.push(x);int flag=0;for(int i=0;i<v[x].size();i++){int y=G[v[x][i]].v;if(y==pre&&!flag){flag=1;continue;}if(!dfn[y]){tarjan(y,x);low[x]=min(low[x],low[y]);}else if(!viss[y])low[x]=min(low[x],dfn[y]);}if(low[x]==dfn[x]){cnt++;while(1){int y=S.top();S.pop();viss[y]=cnt;if(x==y)break;}}}int ans;int dfs(int x,int pre){int fi=inf,ci=inf;for(int i=0;i<vs[x].size();i++){if(Gs[vs[x][i]].v==pre)continue;ci=min(min(dfs(Gs[vs[x][i]].v,x),Gs[vs[x][i]].w),ci);if(fi>ci){int t=fi;fi=ci;ci=t;}}ans=min(ans,ci);return fi;}int main(){int n,m;while(scanf("%d%d",&n,&m)==2){int mina,minb,minw;init();minw=inf;for(int i=0;i<m;i++){int a,b,w;scanf("%d%d%d",&a,&b,&w);addedge(a,b,w);}for(int i=1;i<=n;i++){if(!dfn[i])tarjan(i,-1);}for(int i=0;i<=cnt;i++)vs[i].clear();mina=-1,minb=-1;for(int i=1;i<=n;i++){for(int j=0;j<v[i].size();j++){int y=G[v[i][j]].v;if(viss[i]!=viss[y]){Gs.push_back(edge(viss[i],viss[y],G[v[i][j]].w));vs[viss[i]].push_back(Gs.size()-1);if(G[v[i][j]].w<minw){minw=G[v[i][j]].w;mina=viss[i];minb=viss[y];}}}}if(mina==-1||minb==-1){printf("-1\n");continue;}ans=inf;dfs(minb,mina);dfs(mina,minb);if(ans!=inf)printf("%d\n",ans);elseprintf("-1\n");} }


原创粉丝点击