题意:
给一张图,给定起点s和终点t,问,至多删除两条边,能否让s与t不连通,如果能,最小代价是多少?
题解:
首先搜出一条s到t的道路,要删除的边至少有一条属于这条路,否则,该图一定能找到一条s到达t的路径。枚举之,,删除它。在剩下的图中,寻找桥,如果删除这条边后,该图无法形成一条s到t的路径,更新ans。找桥用tarjan,同时要特判当前这条边是否能够沟通s和t
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1010;
struct E{
int to,cost,num;
E(int _to = 0,int _cost = 0,int _num = 0) {
to = _to; cost = _cost; num = _num;
}
}edgs[maxn*60];
int n,m,cnt,tot,E1,E2,C1,C2,s,t,ans = ~0U>>1,now,a2,a3,a4,pass[maxn],
dfs_clock,vis[maxn],low[maxn],fa[maxn],dfn[maxn],from[maxn];
vector <int> v[maxn];
queue <int> Q;
void dfs(int x,int Fa) {
dfn[x] = low[x] = ++dfs_clock;
if (x == t) pass[x] = cnt;
for (int i = 0; i < v[x].size(); i++) {
if (edgs[v[x][i]].num == E1) continue;
E e = edgs[v[x][i]];
if ((v[x][i]^1) == Fa) continue;
if (vis[e.to] != cnt) {
from[e.to] = v[x][i];
vis[e.to] = cnt; dfs(e.to,v[x][i]);
low[x] = min(low[x],low[e.to]);
if (pass[e.to] == cnt) pass[x] = cnt;
}
else {
low[x] = min(low[x],low[e.to]);
if (pass[e.to] == cnt) pass[x] = cnt;
}
}
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m >> s >> t;
for (int i = 1; i <= m; i++) {
int x,y,w; scanf("%d%d%d",&x,&y,&w);
edgs[tot] = E(y,w,i);
edgs[tot+1] = E(x,w,i);
v[x].push_back(tot++);
v[y].push_back(tot++);
}
++cnt;
Q.push(s);
while(!Q.empty()) {
int k = Q.front(); Q.pop();
for (int i = 0; i < v[k].size(); i++) {
E e = edgs[v[k][i]];
if (vis[e.to] == cnt) continue;
vis[e.to] = cnt;
fa[e.to] = v[k][i]^1;
Q.push(e.to);
}
}
if (vis[t] != cnt) {printf("0\n0"); return 0;}
for (int i = t; i != s; i = edgs[fa[i]].to) {
E1 = edgs[fa[i]].num; C1 = edgs[fa[i]].cost;
if (C1 >= ans) continue;
dfs_clock = 0; ++cnt; now = C1;
vis[s] = cnt; dfs(s,tot);
if (vis[t] != cnt) {
if (now < ans) ans = now,a2 = 1,a3 = E1;
continue;
}
for (int j = 1; j <= n; j++)
if (j != s && vis[j] == cnt && low[j] >= dfn[j] && pass[j] == cnt) {
E2 = edgs[from[j]].num;
int Now = now + edgs[from[j]].cost;
if (Now < ans) ans = Now,a2 = 2,a3 = E1,a4 = E2;
}
}
if (ans == ~0U>>1) cout << -1;
else {
printf("%d\n%d\n",ans,a2);
printf("%d ",a3);
if (a2 == 2) cout << a4;
}
return 0;
}