codeforces 543 B --Destory Roads (最短路+贪心)

题目:http://codeforces.com/problemset/problem/543/B

题意:若干条无向路,长度皆为1,求满足dist[s1][t1] <= l1 && dist[s2][t2] <= l2 条件下能删除的最多的路径。

思路:bfs O(n^2)跑出多源最短路径,此时要求的是两者路径的最大重合且不超过l1, l2的长度len,贪心暴力即可。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int maxn = 3e3+5;
const int inf = 0x3f3f3f3f;

vector<int>vec[maxn];
int n, m, vis[maxn], dist[maxn][maxn];
void bfs(int now){
    queue<P>que; memset(vis, 0, sizeof vis); int start = now;
    que.push(P(now, 0)); vis[now] = 1;
    while(que.size()){
        P now = que.front(); que.pop();
        dist[start][now.first] = now.second;
        for(auto v : vec[now.first]){
            if(vis[v]) continue;
            vis[v] = 1;
            que.push(P(v, now.second+1));
        }
    }
}
int main()
{
    memset(dist, inf, sizeof dist); cin >> n >> m;
    int x, y, s1, t1, l1, s2, t2, l2;
    for(int i=1; i<=m; i++)
        cin >> x >> y, vec[x].push_back(y), vec[y].push_back(x);
    cin >> s1 >> t1 >> l1 >> s2 >> t2 >> l2;
    for(int i=1; i<=n; i++) bfs(i);
    if(dist[s1][t1] > l1 || dist[s2][t2] > l2) {puts("-1"); return 0;}

    int res = dist[s1][t1] + dist[s2][t2];
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            int w1 = min(dist[s1][i]+dist[i][j]+dist[j][t1], dist[s1][j]+dist[j][i]+dist[i][t1]);
            int w2 = min(dist[s2][i]+dist[i][j]+dist[j][t2], dist[s2][j]+dist[j][i]+dist[i][t2]);
            if(w1 > l1 || w2 > l2) continue;
            res = min(res, w1+w2-dist[i][j]);
        }
    }
    cout << m - res << endl;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值