CF543B Destroying Roads 题解

See no solution to a problem on the contribution of a wave of chanting

analysis:

This question is actually the minimum we want to find a figure two most shorted (the good side more deleted).

Let's consider one of the most short-circuit,Do not ask me how I will.Obviously, that is, s and t n- run a shortest path and then on the line.

Then there two myself!Is not that to do it twice, I was too huge a!

It is certainly possible

- but only one case

Taking into account our two paths may be coincidence, we had to enumerate coincidence shortest about the endpoint , the path is divided into 5 sections (section 6?) To deal with.

Then the basic problem is solved, starting with the pre-treatment the shortest path between two points to note here to bfsDo not tell me you want to use Floyd

If the final answer than the m big on -1.

l1, l2 restrictions do not forget to duck judgment!

Code:

#include<cstdio>
#include<queue>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int d[3005][3005];
int vis[3005];
vector<int>e[3005];
void bfs(int s)
{
    memset(vis,0,sizeof(vis));
    queue<int>q;
    q.push(s);
    //vis[s]=1;
    d[s][s]=0;
    while(!q.empty())
    {
        int x=q.front();q.pop();
        if(vis[x])continue;
        vis[x]=1;
        for(int i=0;i<e[x].size();i++)
        {
            int y=e[x][i];
            if(d[s][y]==-1)
            {
                d[s][y]=d[s][x]+1;
                //printf("%d\n",d[s][y]);
                q.push(y);
            }
        } 
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    memset(d,-1,sizeof(d));
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        e[x].push_back(y);
        e[y].push_back(x);
    }
    int s1,t1,l1,s2,t2,l2;
    scanf("%d%d%d",&s1,&t1,&l1);
    scanf("%d%d%d",&s2,&t2,&l2);
    for(int i=1;i<=n;i++)
    {
    //  printf("qaq\n");
        bfs(i);
    }
    /*for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=n;j++)
        {
            printf("%d ",d[i][j]);
        }
        printf("\n");
    }*/
    int ans=999999999;
    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)continue;
            if(d[s2][i]+d[i][j]+d[j][t2]>l2)continue;
            ans=min(ans,d[s1][i]+d[i][j]+d[j][t1]+d[s2][i]+d[i][j]+d[j][t2]-d[i][j]);
        } 
    }
    if(d[s1][t1]<=l1&&d[s2][t2]<=l2)
    {
        ans=min(ans,d[s1][t1]+d[s2][t2]);
    }
    swap(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)continue;
            if(d[s2][i]+d[i][j]+d[j][t2]>l2)continue;
            ans=min(ans,d[s1][i]+d[i][j]+d[j][t1]+d[s2][i]+d[i][j]+d[j][t2]-d[i][j]);
        } 
    }
    if(d[s1][t1]<=l1&&d[s2][t2]<=l2)
    {
        ans=min(ans,d[s1][t1]+d[s2][t2]);
    }
    if(ans>m)printf("-1\n");
    else
    printf("%d\n",m-ans);
    return 0;
} 

Guess you like

Origin www.cnblogs.com/ShineEternal/p/11222368.html