标签:
题意:给一个边长为1的无向图,求删去最多的边使得从a到b距离<=f,从c到d距离<=g,a,b,c,d,f,g都是给定的,求最多删去的边数。
思路:反过来思考,用最少的边构造两条从a到b,从c到d的路径,使得它们满足题目中的条件。于是可以把这两条路径的相对位置分为两种情况,不相交和相交,对于不相交的情况答案就是两组距离之和,对于相交的情况,两条路径肯定共享一段连续路径,对于共享多段的情况可以把中间没共享的取较小值变成共享,得到的距离和不会更大,因此最优情况一定是一段连续的路径。于是可以枚举共享段的两个端点来更新答案,两点之间距离可以通过n次bfs求出。有个小地方需要注意,共享段对于两条路径来说是有方向的,所以需要正反更新。
1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 #include <bitset> 17 #include <functional> 18 #include <numeric> 19 #include <stdexcept> 20 #include <utility> 21 22 using namespace std; 23 24 #define mem0(a) memset(a, 0, sizeof(a)) 25 #define mem_1(a) memset(a, -1, sizeof(a)) 26 #define lson l, m, rt << 1 27 #define rson m + 1, r, rt << 1 | 1 28 #define define_m int m = (l + r) >> 1 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 32 #define rep_down1(a, b) for (int a = b; a > 0; a--) 33 #define all(a) (a).begin(), (a).end() 34 #define lowbit(x) ((x) & (-(x))) 35 #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) {} 36 #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) {} 37 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 38 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 39 #define pchr(a) putchar(a) 40 #define pstr(a) printf("%s", a) 41 #define sstr(a) scanf("%s", a) 42 #define sint(a) scanf("%d", &a) 43 #define sint2(a, b) scanf("%d%d", &a, &b) 44 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c) 45 #define pint(a) printf("%d\n", a) 46 #define test_print1(a) cout << "var1 = " << a << endl 47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl 49 #define mp(a, b) make_pair(a, b) 50 #define pb(a) push_back(a) 51 52 typedef long long LL; 53 typedef pair<int, int> pii; 54 typedef vector<int> vi; 55 56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 58 const int maxn = 3e3 + 7; 59 const int md = 10007; 60 const int inf = 1e9 + 7; 61 const LL inf_L = 1e18 + 7; 62 const double pi = acos(-1.0); 63 const double eps = 1e-6; 64 65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 68 template<class T>T condition(bool f, T a, T b){return f?a:b;} 69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 70 int make_id(int x, int y, int n) { return x * n + y; } 71 72 struct Graph { 73 vector<vector<int> > G; 74 void clear() { G.clear(); } 75 void resize(int n) { G.resize(n + 2); } 76 void add(int u, int v) { G[u].push_back(v); } 77 vector<int> & operator [] (int u) { return G[u]; } 78 }; 79 Graph G; 80 81 int n, m, s1, s2, t1, t2, l1, l2, ans, dist[maxn][maxn]; 82 bool vis[maxn]; 83 84 void chk(int u, int v, int w) { 85 int cost1 = dist[s1][u] + w + dist[v][t1], cost2 = dist[s2][u] + w + dist[v][t2], cost = cost1 + cost2 - w; 86 if (cost1 <= l1 && cost2 <= l2) min_update(ans, cost); 87 swap(u, v); 88 cost2 = dist[s2][u] + w + dist[v][t2]; 89 cost = cost1 + cost2 - w; 90 if (cost1 <= l1 && cost2 <= l2) min_update(ans, cost); 91 } 92 93 void bfs(int s) { 94 queue<pii> q; 95 q.push(make_pair(s, 0)); 96 mem0(vis); 97 vis[s] = true; 98 while (!q.empty()) { 99 pii hnode = q.front(); q.pop(); 100 int u = hnode.first, w = hnode.second; 101 dist[s][u] = w; 102 int sz = G[u].size(); 103 rep_up0(i, sz) { 104 int v = G[u][i]; 105 if (!vis[v]) { 106 vis[v] = true; 107 q.push(make_pair(v, w + 1)); 108 } 109 } 110 } 111 } 112 113 int main() { 114 //freopen("in.txt", "r", stdin); 115 cin >> n >> m; 116 memset(dist, 0x3f, sizeof(dist)); 117 G.clear(); 118 G.resize(n); 119 rep_up0(i, m) { 120 int u, v; 121 sint2(u, v); 122 G.add(u, v); 123 G.add(v, u); 124 } 125 sint3(s1, t1, l1); 126 sint3(s2, t2, l2); 127 rep_up1(i, n) { 128 bfs(i); 129 } 130 if (dist[s1][t1] > l1 || dist[s2][t2] > l2) { 131 puts("-1"); 132 return 0; 133 } 134 ans = dist[s1][t1] + dist[s2][t2]; 135 rep_up1(i, n) { 136 rep_up1(j, n) { 137 chk(i, j, dist[i][j]); 138 } 139 } 140 cout << m - ans << endl; 141 return 0; 142 }
标签:
原文地址:http://www.cnblogs.com/jklongint/p/4486629.html