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 ...
随机推荐
- HttpWebRequest和HttpWebResponse
原文:http://blog.csdn.net/haitaofeiyang/article/details/18362225 申公前几日和一个客户做系统对接,以前和客户对接一般采用webservice ...
- Java注解全面解析
1.基本语法 注解定义看起来很像接口的定义.事实上,与其他任何接口一样,注解也将会编译成class文件. @Target(ElementType.Method) @Retention(Retentio ...
- PKUSC 模拟赛 day2 上午总结
今天上午考得不是很好,主要还是自己太弱QAQ 开场第一题给的图和题意不符,搞了半天才知道原来是走日字形的 然后BFS即可 #include<cstdio> #include<cstr ...
- Python中Lambda, filter, reduce and map 的区别
Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...
- python 获取当前调用函数名等log信息
import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 lineNumber = sys._getframe().f_ ...
- Ubuntu下编译运行Kamailio
kamailio----配置没有成功,这个文档过几天删除,因为这个项目的文档非常少,而且qq群里的人也不活跃,现在正在研究Freeswitch,如果能够满足,就不研究这个了,这篇文档会删除. Kama ...
- iOS开发--浅谈CocoaAsyncSocket编程
Socket就是一种特殊的文件.它是一个连接了两个用户的文件,任何一个用户向Socket里写数据,另一个用户都能看得到,不管这两个用户分布在世界上相距多么遥远的角落,感觉就像坐在一起传纸条一样. 这么 ...
- vc2005中没有classwizard这个命令
vc2005中没有classwizard这个命令了 2005下怎么添加鼠标事件 vc2005中没有classwizard这个命令了 取代classwizard 中的添加消息映射,添加类,等等的功能主要 ...
- QT里使用sqlite的问题,好多坑
1. 我使用sqlite,开发机上好好的,测试机上却不行.后来发现是缺少驱动(Driver not loaded Driver not loaded),代码检查了又检查,发现应该是缺少dll文件(系统 ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...