Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路

D - Destroying Roads

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/544/problem/D

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.

 

Sample Input

5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2

Sample Output

0

HINT

 

题意

有n个城镇,m条边权为1的双向边

让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2

题解:

跑一发最短路,然后最后留下的图肯定是出了s1-t1,s2-t2这两条路之外,其他路都被删除了

由于边权为1,那么距离就是边数

那么我们就直接枚举重叠的道路就好了

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

vector<int> e[maxn];
int d[3300][3300];
int vis[3300];
int main()
{
    int n=read(),m=read();
    int s1,s2,t1,t2,d1,d2;
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        e[x].push_back(y);
        e[y].push_back(x);
    }

    scanf("%d%d%d%d%d%d",&s1,&t1,&d1,&s2,&t2,&d2);
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        queue<int> q;
        q.push(i);
        vis[i]=1;
        while(!q.empty())
        {
            int v=q.front();
            q.pop();
            for(int j=0;j<e[v].size();j++)
            {
                int u=e[v][j];
                if(vis[u])
                    continue;
                vis[u]=1;
                d[i][u]=d[i][v]+1;
                q.push(u);
            }
        }
    }
    if(d[s1][t1]>d1||d[s2][t2]>d2)
    {
        puts("-1");
        return 0;
    }
    int ans=d[s1][t1]+d[s2][t2];
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(d[s1][i]+d[i][j]+d[j][t1]<=d1&&d[s2][i]+d[i][j]+d[j][t2]<=d2)
                ans=min(ans,d[s1][i]+d[i][j]+d[j][t1]+d[s2][i]+d[j][t2]);
            if(d[s1][i]+d[i][j]+d[j][t1]<=d1&&d[t2][i]+d[i][j]+d[j][s2]<=d2)
                ans=min(ans,d[s1][i]+d[i][j]+d[j][t1]+d[t2][i]+d[j][s2]);
        }
    }
    cout<<m-ans<<endl;

}

 

posted @ 2015-05-08 13:46  qscqesze  阅读(931)  评论(0编辑  收藏  举报