Destroying Roads(CF-302B)

Problem Description
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
제목: n개 점 m개의 경계권이 1인 무방향변을 제시하고, s1에서 t1까지의 거리가 d1을 초과하지 않고, s2에서 t2까지의 거리가 t2를 초과하지 않도록 하며, 가능한 한 가장 많은 도로를 파괴하고, 파괴할 도로의 수량을 출력할 수 있다면, 그렇지 않으면 출력-1
사고방식: s1/s2에서 t1/t2까지의 거리가 d1/d2를 초과하지 않고 가장 많은 도로를 파괴해야 하기 때문에 s1/s2에서 t1/t2까지의 최단로를 구한 다음에 d1/d2 범위에서 도달할 수 있는지 판단한다. 만약에 범위에서 도달할 수 있다면 s1-t1, s2-t2 두 도로의 변수를 더하고 이 두 도로의 중첩된 변수를 일일이 열거하여 찾는다. 마지막 결과는 전체 변수를 두 도로의 변수를 빼고 중첩된 변수를 더하는 것이다.
Source Program
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PI acos(-1.0)
#define E 1e-6
#define MOD 16007
#define INF 0x3f3f3f3f
#define N 5001
#define LL long long
using namespace std;
int n,m;
vector G[N];
int dis[N][N];
bool vis[N];
void SPFA(){
    for(int i=1;i<=n;i++){
        memset(vis,false,sizeof(vis));
        vis[i]=true;
        queue Q;
        Q.push(i);
        while(!Q.empty()){
            int x=Q.front();
            Q.pop();
            for(int j=0;jd1||dis[s2][t2]>d2)
            printf("-1
"); else{ int res=dis[s1][t1]+dis[s2][t2]; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(dis[s1][i]+dis[i][j]+dis[j][t1]<=d1&&dis[s2][i]+dis[i][j]+dis[j][t2]<=d2) res=min(res,dis[s1][i]+dis[i][j]+dis[j][t1]+dis[s2][i]+dis[j][t2]); if(dis[s1][i]+dis[i][j]+dis[j][t1]<=d1&&dis[t2][i]+dis[i][j]+dis[j][s2]<=d2) res=min(res,dis[s1][i]+dis[i][j]+dis[j][t1]+dis[t2][i]+dis[j][s2]); } } printf("%d
",m-res); } } return 0; }

좋은 웹페이지 즐겨찾기