[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 nn 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 ss and tt 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 ss to tt 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 ss and tt 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 nn and mm2n10002 ≤ n ≤ 1000, 0m300000 ≤ m ≤ 30 000) — the number of towns in Berland and the number of roads.
The second line contains integers ss and tt1s,tn,st1 ≤ s, t ≤ n,s ≠ t) — indices of towns which break up the relationships.
Then follow m lines, each of them contains three integers xix_i, yiy_i and wiw_i1xi,yin,1wi1091 ≤ x_i, y_i ≤ n, 1 ≤ w_i ≤ 10^9) — indices of towns connected by the ii-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 ss and tt, if it is allowed to close no more than two roads.
In the second line print the value cc0c20 ≤ c ≤ 2) — the number of roads to be closed in the found solution.
In the third line print in any order cc diverse integers from 11 to mm — indices of closed roads. Consider that the roads are numbered from 11 to mm in the order they appear in the input.
If it is impossible to make towns ss and tt disconnected by removing no more than 22 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

題目大意

給出一個無向圖,可能有重邊和自環,給定sstt,至多刪除兩條邊,讓sstt不連通,問方案的權值和最小爲多少,並且輸出刪的邊。

分析

參考博客:codeforces 700C Break Up 暴力枚舉邊+邊雙縮點(有重邊)
(我覺得他寫得太妙了,所以就直接粘貼了)


分類討論:

  • sstt本就不連通,輸出0即可;
  • sstt連通但是隻有一條完全不相同的路徑;
  • sstt連通但是隻有兩條條完全不相同的路徑;
  • sstt連通但是有不少於33條完全不相同的路徑。

對於後三種情況,枚舉刪掉的其中一條邊,然後雙縮點,再討論:

  • 刪邊後,sstt在一個雙連通分量,此時符合上述第4個情況,無解;
  • 本身就不連通,這時只刪除枚舉的邊就好了;
  • 連通的話,找到ss連通分量到tt連通分量中最小的橋就好了,刪除的是枚舉的邊和最小
    的橋邊。

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

代碼

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章