Destroying Roads CodeForces - 543B(最短路)

mac2026-05-20  5

In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any city you can get to any other one by moving along the roads.

You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to city t1 in at most l1 hours and get from city s2 to city t2 in at most l2 hours.

Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.

Input The first line contains two integers n, m (1 ≤ n ≤ 3000, ) — the number of cities and roads in the country, respectively.

Next m lines contain the descriptions of the roads as pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.

The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2, respectively (1 ≤ si, ti ≤ n, 0 ≤ li ≤ n).

Output Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.

Examples Input 5 4 1 2 2 3 3 4 4 5 1 3 2 3 5 2 Output 0 Input 5 4 1 2 2 3 3 4 4 5 1 3 2 2 4 2 Output 1 Input 5 4 1 2 2 3 3 4 4 5 1 3 2 3 5 1 Output -1

题意: 给你一张无向图,给你s1,t1,s2,t2。要求删掉最多的边,使得s1到t1的最短距离不超过l1,s2到t2的最短距离不超过t2。 思路: 先跑一遍最短路求出所有点之间的最短路距离。枚举两段路的交线段,两个最短路减去交线段就是所留下来的边。但是记住要交换s1和t1反向跑一遍。因为可能出现s1到t1是正向走交线段,s2到t1是反向走交线段,最后还是算了交线段两次。

#include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 6e3 + 7; int dis[maxn][maxn],vis[maxn]; int head[maxn],to[maxn],nex[maxn],tot = 0; int n,m; void add(int x,int y) { to[++tot] = y; nex[tot] = head[x]; head[x] = tot; } void spfa(int s) { for(int i = 1;i <= n;i++)dis[s][i] = INF; queue<int>q; q.push(s); vis[s] = 1; dis[s][s] = 0; while(!q.empty()) { int now = q.front();q.pop(); vis[now] = 0; for(int i = head[now];i;i = nex[i]) { int v = to[i]; if(dis[s][v] > dis[s][now] + 1) { dis[s][v] = dis[s][now] + 1; if(vis[v] == 0) { vis[v] = 1; q.push(v); } } } } } int main() { scanf("%d%d",&n,&m); for(int i = 1;i <= m;i++) { int x,y;scanf("%d%d",&x,&y);add(x,y);add(y,x); } for(int i = 1;i <= n;i++)spfa(i); int s1,t1,l1,s2,t2,l2;scanf("%d%d%d%d%d%d",&s1,&t1,&l1,&s2,&t2,&l2); if(dis[s1][t1] > l1 || dis[s2][t2] > l2) { printf("-1\n"); return 0; } int ans = dis[s1][t1] + dis[s2][t2]; for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { if(dis[i][j] == INF)continue; int tmp1 = dis[s1][i] + dis[i][j] + dis[j][t1]; int tmp2 = dis[s2][i] + dis[i][j] + dis[j][t2]; if(tmp1 > l1 || tmp2 > l2)continue; ans = min(ans,tmp1 + tmp2 - dis[i][j]); } } swap(s1,t1); for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { if(dis[i][j] == INF)continue; int tmp1 = dis[s1][i] + dis[i][j] + dis[j][t1]; int tmp2 = dis[s2][i] + dis[i][j] + dis[j][t2]; if(tmp1 > l1 || tmp2 > l2)continue; ans = min(ans,tmp1 + tmp2 - dis[i][j]); } } printf("%d\n",m - ans); return 0; }
最新回复(0)