Codeforces Round #302 (Div. 2)D

D. Destroying Roads
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 小于l1 s2到t2小于l2
那么就会想到这样的思路
第一个这两个不相交最短,第二个这两个相交最短
不相交的好求
相交的是中间重合一段,然后这一段链接其他两边的情况…

3000数据
堆优化后的dijkstra对每一个点求最短路正好n^2logn

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<queue>
using namespace std;
int jsj = 0x3f3f3f3f;
int n, m, s1, t1, s2, t2, l1, l2;
struct p
{
    int dian, juli;
    bool  operator < (const p&a)const {
        return juli > a.juli;
    }
};
int biaoji[3005][3001], daon[3001][3001], qid[3001];
vector<int>tu[3001];
int main()
{
    cin >> n >> m;
    int q, w;
    for (int a = 1;a <= m;a++)
    {
        cin >> q >> w;
        tu[q].push_back(w);
        tu[w].push_back(q);
    }
    cin >> s1 >> t1 >> l1 >> s2 >> t2 >> l2;
    //qid[1] = s1, qid[2] = t1, qid[3] = s2, qid[4] = t2;
    for (int a = 1;a <= n;a++)qid[a] = a;
    priority_queue<p>qq[3005];
    memset(daon, 0x3f, sizeof(daon));
    for (int a = 1;a <= n;a++)
    {
        daon[a][qid[a]] = 0;
        qq[a].push({ qid[a],0 });
        while (!qq[a].empty())
        {
            p we = qq[a].top();
            qq[a].pop();
            if (biaoji[a][we.dian])continue;
            biaoji[a][we.dian] = 1;
            for (int b = 0;b < tu[we.dian].size();b++)
            {
                if (biaoji[a][tu[we.dian][b]])continue;
                if (daon[a][tu[we.dian][b]] > daon[a][we.dian] + 1)
                {
                    daon[a][tu[we.dian][b]] = daon[a][we.dian] + 1;
                    qq[a].push({ tu[we.dian][b] ,daon[a][tu[we.dian][b]] });
                }
            }
        }
    }
    int jccc = 0;
    int zhijie = daon[s1][t1] + daon[s2][t2];
    if (daon[s1][t1] > l1 || daon[s2][t2] > l2)jccc = 1;
    int jianjie = 0x3f3f3f3f;
    int jccc2 = 1;
    for (int a = 1;a <= n;a++)
    {
        for (int b = 1;b <= n;b++)
        {
            if (daon[a][b] == jsj || daon[a][s1] == jsj || daon[b][t1] == jsj || daon[a][s2] == jsj || daon[b][t2] == jsj)continue;
            int tiaojian = daon[a][b] + daon[a][s1] + daon[b][t1],tiaojian2= daon[a][b] + daon[b][s1] + daon[a][t1];
            int tiaojian3 = daon[a][b] + daon[a][s2] + daon[b][t2], tiaojian4 = daon[a][b] + daon[b][s2] + daon[a][t2];
            if (tiaojian <= l1&&tiaojian3 <= l2)
            {
                jccc2 = 0;
                jianjie = min(jianjie, tiaojian + tiaojian3 - daon[a][b]);
            }
            if (tiaojian2 <= l1&&tiaojian4 <= l2)
            {
                jccc2 = 0;
                jianjie = min(jianjie, tiaojian2 + tiaojian4 - daon[a][b]);
            }
            if (tiaojian <= l1&&tiaojian4 <= l2)
            {
                jccc2 = 0;
                jianjie = min(jianjie, tiaojian + tiaojian4 - daon[a][b]);
            }
            if (tiaojian2 <= l1&&tiaojian3 <= l2)
            {
                jccc2 = 0;
                jianjie = min(jianjie, tiaojian2 + tiaojian3 - daon[a][b]);
            }
        }
    }
    if (jccc&&jccc2)cout << -1;
    else if (jccc == 0 && jccc2 == 1)cout << m - zhijie;
    else if (jccc2 == 0 && jccc == 1)cout << m - jianjie;
    else cout << m - min(zhijie, jianjie);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值