codeforces 700C Break Up 暴力枚举边+边双缩点(有重边)
题意: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 暴力枚举边+边双缩点(有重边)的更多相关文章
- Codeforces 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- codeforces Restore Cube(暴力枚举)
/* 题意:给出立方体的每个顶点的坐标(是由源坐标三个数某几个数被交换之后得到的!), 问是否可以还原出一个立方体的坐标,注意这一句话: The numbers in the i-th output ...
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- CodeForces 496D Tennis Game (暴力枚举)
题意:进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时, 游戏结束.现在给出n次对决的记录,问可能的s和t有多少种,并按s递增 ...
- Codeforces 327A-Flipping Game(暴力枚举)
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 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 ...
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- Codeforces 425A Sereja and Swaps(暴力枚举)
题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...
随机推荐
- Rockethon 2015
A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: ...
- lintcode :Trailing Zeros 尾部的零
题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...
- 再谈PCA
其实之前写过PCA相关的博文,但是由于之前掌握的理论知识有限,所以理解也比较浅.这篇博文,我们以另外一种角度来理解PCA看,这里我假设大家对PCA都有一个初步的了解.首先,我们举一个二维空间中 ...
- 什么是hibernate?
一.什么是hibernate框架?1.通过数据库保存java运行时产生的对象和恢复对象,其实就是实现java对象与关系数据库记录的映射关系称为ORM(Object Relation Mapping), ...
- Linux文件系统介绍
1.ext2/ext3(日志功能)文件系统(Linux标准文件系统.一种索引式文件系统) SuperBlock:Superblock是记录整个filesystem 相关信息的地方,没有Superblo ...
- Visual Studio Support (DDEX)
原文 VS2012,VS2013,and VS2015Pro+NpgsqlDdexProvider+EFv6 how to(by @kenjiuno) Reference: #213 Overview ...
- poi操作oracle数据库导出excel文件2
package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- 基于eclipse创建android的helloworld工程
基于eclipse创建android的helloworld工程 之前用过Android studio感觉很慢,决定采用eclipse来学习Android开发.下面来看是怎么创建的. 选择File--- ...
- 分批次获取git for windows的源代码
$ git initInitialized empty Git repository in d:/SourceCode/GitHub/Git For Windows/Git/.git/ $ git r ...
- [HDOJ3367]Pseudoforest(并查集,贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 求一个无向图上权值最大的伪森林. 伪森林:一个图的连通子图,当且仅当这个子图有且仅有一个环. 既 ...