[CodeForces 700D]Break Up(双连通分量+枚举)

mac2023-01-25  28

文章目录

题目题目大意分析代码

题目

Description Again, there are hard times in Berland! Many towns have such tensions that even civil war is possible. There are n n n towns in Reberland, some pairs of which connected by two-way roads. It is not guaranteed that it is possible to reach one town from any other town using these roads. Towns s s s and t t t announce the final break of any relationship and intend to rule out the possibility of moving between them by the roads. Now possibly it is needed to close several roads so that moving from s s s to t t t using roads becomes impossible. Each town agrees to spend money on closing no more than one road, therefore, the total number of closed roads will be no more than two. Help them find set of no more than two roads such that there will be no way between s s s and t t t after closing these roads. For each road the budget required for its closure was estimated. Among all sets find such that the total budget for the closure of a set of roads is minimum.

Input The first line of the input contains two integers n n n and m m m 2   ≤   n   ≤   1000 2 ≤ n ≤ 1000 2n1000, 0   ≤   m   ≤   30   000 0 ≤ m ≤ 30 000 0m30000) — the number of towns in Berland and the number of roads. The second line contains integers s s s and t t t 1   ≤   s ,   t   ≤   n , s   ≠   t 1 ≤ s, t ≤ n,s ≠ t 1s,tn,s=t) — indices of towns which break up the relationships. Then follow m lines, each of them contains three integers x i x_i xi, y i y_i yi and w i w_i wi 1   ≤   x i ,   y i   ≤   n , 1   ≤   w i   ≤   1 0 9 1 ≤ x_i, y_i ≤ n, 1 ≤ w_i ≤ 10^9 1xi,yin,1wi109) — indices of towns connected by the i i i-th road, and the budget on its closure. All roads are bidirectional. It is allowed that the pair of towns is connected by more than one road. Roads that connect the city to itself are allowed.

Output In the first line print the minimum budget required to break up the relations between s s s and t t t, if it is allowed to close no more than two roads. In the second line print the value c c c 0   ≤   c   ≤   2 0 ≤ c ≤ 2 0c2) — the number of roads to be closed in the found solution. In the third line print in any order c c c diverse integers from 1 1 1 to m m m — indices of closed roads. Consider that the roads are numbered from 1 1 1 to m m m in the order they appear in the input. If it is impossible to make towns s s s and t t t disconnected by removing no more than 2 2 2 roads, the output should contain a single line -1. If there are several possible answers, you may print any of them.

Sample Input 1

6 7 1 6 2 1 6 2 3 5 3 4 9 4 6 4 4 6 5 4 5 1 3 1 3

Sample Output 1

8 2 2 7

Sample Input 2

6 7 1 6 2 3 1 1 2 2 1 3 3 4 5 4 3 6 5 4 6 6 1 5 7

Sample Output 2

9 2 4 5

Sample Input 3

5 4 1 5 2 1 3 3 2 1 3 4 4 4 5 2

Sample Output 3

1 1 2

Sample Input 4

2 3 1 2 1 2 734458840 1 2 817380027 1 2 304764803

Sample Output 4

-1

题目大意

给出一个无向图,可能有重边和自环,给定 s s s t t t,至多删除两条边,让 s s s t t t不连通,问方案的权值和最小为多少,并且输出删的边。

分析

参考博客:codeforces 700C Break Up 暴力枚举边+边双缩点(有重边) (我觉得他写得太妙了,所以就直接粘贴了)


分类讨论:

s s s t t t本就不连通,输出0即可; s s s t t t连通但是只有一条完全不相同的路径; s s s t t t连通但是只有两条条完全不相同的路径; s s s t t t连通但是有不少于 3 3 3条完全不相同的路径。

对于后三种情况,枚举删掉的其中一条边,然后双缩点,再讨论:

删边后, s s s t t t在一个双连通分量,此时符合上述第4个情况,无解;本身就不连通,这时只删除枚举的边就好了;连通的话,找到 s s s连通分量到 t t t连通分量中最小的桥就好了,删除的是枚举的边和最小 的桥边。

最终把合法的方案取最小即可。

代码

#include<bits/stdc++.h> using namespace std; #define MAXN 1000 #define MAXM 30000 #define INF 0x7fffffff int W[MAXM+5]; struct Edge{ int v,id; }pre[MAXN+5]; vector<Edge> G[MAXN+5]; bool Reach(int S,int T){ queue<int> Q; pre[S].v=-1,Q.push(S); while(!Q.empty()){ int u=Q.front();Q.pop(); for(auto e:G[u]) if(!pre[e.v].v){ pre[e.v]={u,e.id}; Q.push(e.v); } } return pre[T].v; } int vis[MAXN+5]; bool Path[MAXM+5]; bool dfs(int u,int fa,int T,int Cut){ if(vis[u]!=-1) return vis[u]; vis[u]=0; bool ret=0; for(auto e:G[u]){ int v=e.v,i=e.id; if(i==Cut) continue; if(v!=fa&&dfs(v,u,T,Cut)){ vis[u]=1; Path[e.id]=1;//表示这条边属于S->T的某条路径上 } } return vis[u]; } int cnt; int CutEdge[MAXM+5]; int dfn[MAXN+5],Low[MAXN+5]; void Tarjan(int u,int faEdge,int Cut){ dfn[u]=Low[u]=++cnt; for(auto e:G[u]){ int v=e.v,i=e.id; if(i==Cut) continue; if(!dfn[v]){ Tarjan(v,i,Cut); Low[u]=min(Low[u],Low[v]); if(Low[v]>dfn[u]) CutEdge[i]=1; } else if(i!=faEdge) Low[u]=min(Low[u],dfn[v]); } } int main(){ int N,M,S,T; scanf("%d%d%d%d",&N,&M,&S,&T); for(int i=1;i<=M;i++){ int u,v; scanf("%d%d%d",&u,&v,&W[i]); G[u].push_back({v,i}), G[v].push_back({u,i}); } if(!Reach(S,T))//不连通 return puts("0\n0"),0; int Ans1=0,Ans2=0,Cost=INF; for(int u=T;u!=S;u=pre[u].v){ //在某条路径上删一条边 //因为想让图不连通,不论是哪条路径,必定要删此路径上的一条边 //所以具体哪条路径不重要,随便找一条就行了 int i=pre[u].id; memset(vis,-1,sizeof vis); memset(Path,0,sizeof Path); vis[T]=1; if(!dfs(S,-1,T,i)){//删一条即可 if(W[i]<Cost){ Cost=W[i]; Ans1=i,Ans2=0; } continue; } cnt=0; memset(dfn,0,sizeof dfn); memset(Low,0,sizeof Low); memset(CutEdge,0,sizeof CutEdge); Tarjan(S,-1,i);//双缩点 for(int j=1;j<=M;j++) if(Path[j]&&CutEdge[j])//找各路径上的桥 if(W[i]+W[j]<Cost) Cost=W[Ans1=i]+W[Ans2=j]; } if(Cost==INF) return puts("-1"),0; printf("%d\n",Cost); if(!Ans2) printf("1\n%d\n",Ans1); else printf("2\n%d %d\n",Ans1,Ans2); return 0; }
最新回复(0)