51 nod 1444 破坏道路(BFS+枚举)

题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1444
思路:先求出各点的最短路(n<=3000),然后枚举重叠部分,寻找最少路数
代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 3005;
typedef long long ll;
int n,m;

struct node
{
    int v,w;
    node(int v=0,int w=0):v(v),w(w){}
};
vector<int> G[N];
int vis[N],d[N][N];
void bfs()
{
    for(int s = 1;s <= n;s++)
    {
        memset(vis,0,sizeof(vis));
        queue<int> q;
        q.push(s);
        vis[s] = 1;
        while(!q.empty())
        {
            int u = q.front();q.pop();
            for(int i = 0;i < G[u].size();i++)
            {
                int v = G[u][i];
                if(!vis[v])
                {
                    vis[v] = 1;
                    d[s][v] = d[s][u]+1;
                    q.push(v);
                }
            }
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    int u,v;
    for(int i = 0;i < m;i++)
    {
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int s1,t1,l1,s2,t2,l2;
    scanf("%d%d%d%d%d%d",&s1,&t1,&l1,&s2,&t2,&l2);
    bfs();
    if(d[s1][t1] > l1 || d[s2][t2] > l2)
        return 0 * printf("-1\n");
    int minn = 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] <= l1 && d[s2][i]+d[i][j]+d[j][t2] <= l2)
                minn = min(minn, 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] <= l1 && d[t2][i]+d[i][j]+d[j][s2] <= l2)
                minn = min(minn, d[s1][i]+d[i][j]+d[j][t1]+d[t2][i]+d[j][s2]);
        }
    }
    printf("%d\n",m-minn);
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值