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

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 nm (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 aibi (1 ≤ ai, bi ≤ nai ≠ 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, s1t1l1 and s2t2l2, respectively (1 ≤ si, ti ≤ n0 ≤ li ≤ n).

Output

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

Sample test(s)
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条道路,然后给你两个起点a1,a2,两个终点b1,b2,城市与城市之间的道路长度都为1,还有限制时间,问找出a1-》b1,a2-》b2的道路,然后摧毁那些没有走过的道路,问最多可以摧毁多少条道路。。。。。

这道题因为录得长度就是1,所以,最大摧毁道路数=总数-走过的最小道路数;

思路:
这里我我们先预处理一下每两个点之间的最短路
然后我们直接枚举重叠的道路就好了

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#define nn 3090
#define inff 0x3fffffff
#define eps 1e6+100
#define ll long long
using namespace std;
struct node
{
    int en;
    int len;
    int next;
}road[nn*1500];
int n,m;
int num;
int p[nn*1500];


void init()
{
    memset(p,-1,sizeof(p));
    num=0;
}


void add(int s,int e,int le)
{
    road[num].en=e;
    road[num].len=le;
    road[num].next=p[s];
    p[s]=num++;
}
struct dian
{
    int id;
    int val;
    friend bool operator<(dian a,dian b)
    {
        return a.val>b.val;
    }
}sta,tem;
priority_queue<dian> que;
int dis[nn][nn];           //dis[i][j]表示从i到j的最短路




void dij(int s,int e)  //预处理每两个点的最短路
{
    int i,j;
    for(i=1;i<=n*10;i++)
        dis[s][i]=inff;
    dis[s][s]=0;
    sta.id=s;
    sta.val=0;
    que.push(sta);


    while(que.size())
    {
        tem=que.top();
        que.pop();
         if(dis[s][tem.id]<tem.val)
            continue;
        for(i=p[tem.id];i!=-1;i=road[i].next)
        {
            int w=road[i].en;
            if(dis[s][w]>dis[s][tem.id]+road[i].len)
            {
                dis[s][w]=dis[s][tem.id]+road[i].len;
                sta.id=w;
                sta.val=dis[s][w];
                que.push(sta);
            }
        }
    }
}
int main()
{
    int i,j;
    int s,e;
    int s1,l1,s2,l2,t1,t2;
    int k1,k2;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        init();
        for(i=0;i<m;i++)
        {
            scanf("%d %d",&s,&e);
            add(s,e,1);
            add(e,s,1);
        }
        scanf("%d %d %d",&s1,&t1,&l1);
        scanf("%d %d %d",&s2,&t2,&l2);


     for(i=1;i<=n;i++)
        dij(i,n);
        if(dis[s1][t1]>l1 || dis[s2][t2]>l2)
        {
            printf("-1");
            continue;
        }
        int ans=dis[s1][t1]+dis[s2][t2];
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                
    if(dis[s1][i]+dis[i][j]+dis[j][t1]<=l1 && dis[s2][i]+dis[i][j]+dis[j][t2]<=l2)
                   ans=min(ans,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]<=l1 && dis[t2][i]+dis[i][j]+dis[j][s2]<=l2)
                    ans=min(ans,dis[s1][i]+dis[i][j]+dis[j][t1]+dis[t2][i]+dis[j][s2]);  

// 这里我还是想的不是很通,这里我也是看别人这样写的        
            }
        }
        printf("%d\n",m-ans);
    }
}




 
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值