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

本文探讨如何通过至多删除两条边,使指定的两个节点在无向图中不再连通,同时使得删除边的总成本最小。采用暴力枚举加边双缩点的方法,对不同连通情况进行了分类讨论,实现了解决方案的最优化。

题目

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;
}
当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值