题意:给一个边长为1的无向图,求删去最多的边使得从a到b距离<=f,从c到d距离<=g,a,b,c,d,f,g都是给定的,求最多删去的边数。

思路:反过来思考,用最少的边构造两条从a到b,从c到d的路径,使得它们满足题目中的条件。于是可以把这两条路径的相对位置分为两种情况,不相交和相交,对于不相交的情况答案就是两组距离之和,对于相交的情况,两条路径肯定共享一段连续路径,对于共享多段的情况可以把中间没共享的取较小值变成共享,得到的距离和不会更大,因此最优情况一定是一段连续的路径。于是可以枚举共享段的两个端点来更新答案,两点之间距离可以通过n次bfs求出。有个小地方需要注意,共享段对于两条路径来说是有方向的,所以需要正反更新。

 #pragma comment(linker, "/STACK:10240000,10240000")

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt5(name, a, b, c, d, e) name(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0): a(a), b(b), c(c), d(d), e(e) {}
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 3e3 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } struct Graph {
vector<vector<int> > G;
void clear() { G.clear(); }
void resize(int n) { G.resize(n + ); }
void add(int u, int v) { G[u].push_back(v); }
vector<int> & operator [] (int u) { return G[u]; }
};
Graph G; int n, m, s1, s2, t1, t2, l1, l2, ans, dist[maxn][maxn];
bool vis[maxn]; void chk(int u, int v, int w) {
int cost1 = dist[s1][u] + w + dist[v][t1], cost2 = dist[s2][u] + w + dist[v][t2], cost = cost1 + cost2 - w;
if (cost1 <= l1 && cost2 <= l2) min_update(ans, cost);
swap(u, v);
cost2 = dist[s2][u] + w + dist[v][t2];
cost = cost1 + cost2 - w;
if (cost1 <= l1 && cost2 <= l2) min_update(ans, cost);
} void bfs(int s) {
queue<pii> q;
q.push(make_pair(s, ));
mem0(vis);
vis[s] = true;
while (!q.empty()) {
pii hnode = q.front(); q.pop();
int u = hnode.first, w = hnode.second;
dist[s][u] = w;
int sz = G[u].size();
rep_up0(i, sz) {
int v = G[u][i];
if (!vis[v]) {
vis[v] = true;
q.push(make_pair(v, w + ));
}
}
}
} int main() {
//freopen("in.txt", "r", stdin);
cin >> n >> m;
memset(dist, 0x3f, sizeof(dist));
G.clear();
G.resize(n);
rep_up0(i, m) {
int u, v;
sint2(u, v);
G.add(u, v);
G.add(v, u);
}
sint3(s1, t1, l1);
sint3(s2, t2, l2);
rep_up1(i, n) {
bfs(i);
}
if (dist[s1][t1] > l1 || dist[s2][t2] > l2) {
puts("-1");
return ;
}
ans = dist[s1][t1] + dist[s2][t2];
rep_up1(i, n) {
rep_up1(j, n) {
chk(i, j, dist[i][j]);
}
}
cout << m - ans << endl;
return ;
}
05-21 12:36