题意:n个点,m条无向边,每个边有权值,给你 s 和 t,问你至多删除两条边,让s,t不连通,问方案的权值和最小为多少,并且输出删的边

分析:n<=1000,m是30000  s,t有4种情况(首先定义完全不相同的路径,即两条路径上没有一条边是一样的)

1. s,t本就不连通,输出0即可

2. s,t连通但是只有一条完全不相同的路径

3. s,t连通但是只有两条条完全不相同的路径

4. s,t连通但是有>=3条完全不相同的路径(这种情况无解)

情况1预处理即可,要解决的问题是2,3,4乍一看,情况很多,其实可以用枚举法

首先找出一条从s到t的路径,发现s到t,如果不连通,必须删除这条路径中一条边

于是可以枚举删除的第一条边,剩下的做边双缩点,这时分开讨论

(1)删边后,s和t在一个双连通分量,此时符合上述情况4,无解

(2)本身就不连通,这时只删除枚举的边就好了

(3)连通的话,找到s连通分量到t连通分量中最小的桥就好了,删除的是枚举的边和最小的桥边

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

#include <cstdio>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e3+;
const int INF = 1e9+;
typedef long long LL;
int s,t,n,m;
int a[N*],b[N*],c[N*];
vector<int>g[N];
bool ban[*N];
int dfn[N],low[N],clk,bel[N],cnt;
stack<int>st;
void targin(int u,int f){
dfn[u]=low[u]=++clk;
bool flag=;st.push(u);
for(int i=;i<g[u].size();++i){
if(ban[g[u][i]])continue;
int v=u==a[g[u][i]]?b[g[u][i]]:a[g[u][i]];
if(v==f&&!flag){flag=;continue;}
if(!dfn[v]){
targin(v,u);
low[u]=min(low[u],low[v]);
}
else low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
++cnt;int x;
do{
x=st.top();
st.pop();
bel[x]=cnt;
}while(x!=u);
}
}
int path[N*];
int vis[N];
bool find_pre(int u,int d){
vis[u]=d;
if(u==t)return true;
for(int i=;i<g[u].size();++i){
int v=u==a[g[u][i]]?b[g[u][i]]:a[g[u][i]];
if(vis[v])continue;
path[d]=g[u][i];
if(find_pre(v,d+))return true;
}
return false;
}
vector<int>G[N];
int find_brige(int u,int f,int id){
if(bel[t]==u)return id;
for(int i=;i<G[u].size();++i){
int x=bel[a[G[u][i]]],y=bel[b[G[u][i]]];
int v=u==x?y:x;
if(v==f)continue;
int tmp=id;if(c[G[u][i]]<c[id])tmp=G[u][i];
tmp=find_brige(v,u,tmp);
if(tmp)return tmp;
}
return ;
}
int main(){
c[]=INF;
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=;i<=m;++i){
scanf("%d%d%d",&a[i],&b[i],&c[i]);
if(a[i]!=b[i])
g[a[i]].push_back(i),g[b[i]].push_back(i);
}
if(!find_pre(s,)){printf("0\n0\n");return ;}
int ans1=-,id;
int ans2=-,id1,id2;
for(int i=;i<vis[t];++i){
ban[path[i]]=true;
memset(dfn,,sizeof(dfn));cnt=clk=;
for(int j=;j<=n;++j)if(!dfn[j])targin(j,);
if(bel[s]==bel[t]){ban[path[i]]=false;continue;}
for(int j=;j<N;++j)G[j].clear();
for(int j=;j<=m;++j){
if(ban[j]||bel[a[j]]==bel[b[j]])continue;
G[bel[a[j]]].push_back(j);
G[bel[b[j]]].push_back(j);
}
int cur=find_brige(bel[s],,);
if(!cur){
if(ans1==-||ans1>c[path[i]])
ans1=c[path[i]],id=path[i];
}
else{
if(ans2==-||ans2>c[path[i]]+c[cur]){
ans2=c[path[i]]+c[cur];
id1=cur;id2=path[i];
}
}
ban[path[i]]=false;
}
if(ans1==-&&ans2==-)printf("-1\n");
else if(ans1!=-&&ans2==-){
printf("%d\n1\n%d\n",ans1,id);
}
else if(ans1==-&&ans2!=-){
printf("%d\n2\n%d %d\n",ans2,id1,id2);
}
else{
if(ans1<=ans2)printf("%d\n1\n%d\n",ans1,id);
else printf("%d\n2\n%d %d\n",ans2,id1,id2);
}
return ;
}

codeforces 700C Break Up 暴力枚举边+边双缩点(有重边)的更多相关文章

  1. Codeforces 626E Simple Skewness(暴力枚举+二分)

    E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  2. codeforces Restore Cube(暴力枚举)

    /* 题意:给出立方体的每个顶点的坐标(是由源坐标三个数某几个数被交换之后得到的!), 问是否可以还原出一个立方体的坐标,注意这一句话: The numbers in the i-th output ...

  3. Diverse Garland CodeForces - 1108D (贪心+暴力枚举)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  4. CodeForces 496D Tennis Game (暴力枚举)

    题意:进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时, 游戏结束.现在给出n次对决的记录,问可能的s和t有多少种,并按s递增 ...

  5. Codeforces 327A-Flipping Game(暴力枚举)

    A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举

    B. Covered Path Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...

  7. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  8. Codeforces 425A Sereja and Swaps(暴力枚举)

    题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...

  9. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

随机推荐

  1. SDUT1281Cup

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1281 题意 : 一个杯子,告诉你底面半径,顶端 ...

  2. JSP include标签和include指令

    test1.jsp <% int a = 5; out.println(a); %> test2.jsp <jsp:include page="/test1.jsp&quo ...

  3. PHP输入流php://input [转]

    对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述. "php://input allows you to read raw POST data. It is ...

  4. org.apache.common.io-FileUtils详解

    org.apache.common.io---FileUtils详解 getTempDirectoryPath():返回临时目录路径; public static String getTempDire ...

  5. linux内核--进程与线程

    http://blog.csdn.net/yusiguyuan/article/details/12154823 在<linux内核设计与实现>中第三章讲解了进程管理,在关于进程和线程的概 ...

  6. Android 类似时间轴的实现

    想要实现图片中的的时间轴的效果,设定了三种颜色,但是出来的只有一个黑色,还不是设定好的,而且长度很长的话不能滚动,下面上代码: 布局文件: <LinearLayout xmlns:android ...

  7. SoapUI test WCF

    http://blogs.msdn.com/b/nabeelp/archive/2008/03/07/obscure-error-addressfilter-mismatch-at-the-endpo ...

  8. Visaul Studio2015安装以及c++单元测试使用方法

    Visual Studio 2015安装流程     vs2015是一款十分好用的IDE,接下来就介绍一下安装流程.这里采用在线安装方式,从官网下载使得安装更加安全. 第一步:在百度中搜索Visual ...

  9. GetKeyState和GetAsyncKeyState以及GetKeyboardState函数的用法与区别

    GetKeyState.GetAsyncKeyState.GetKeyboardState函数的区别: 1.BOOL GetKeyboardState( PBYTE lpKeyState );获得所有 ...

  10. Effective C++学习笔记 条款06:如不想使用编译器自动生成的函数,就该明确拒绝

    一.为驳回编译器自动提供的机能,可将相应成员函数声明为private并且不予实现.(如果你仅仅是自己不实现的话,编译器会帮你实现) 如: class A { public: A(const strin ...