[Reserved] $ CF543B $ explanations

Read the original

Background: \ (codeforces \) \ (Round \) \ (\ # 302 \) \ (. (Div 1) \) \ (B \) title, \ (Codeforces543B \)
given as a whole right side \ ( 1 \) in Fig. In the guaranteed point \ (S_1 \) point \ (T_l \) a distance of no more than \ (L_1 \) and point \ (S_2 \) point \ (T_2 \) a distance of no more than \ (L_2 \) under conditions , seeking the deletion of the number of edges up to. If there is no legitimate program output \ (--1 \) .
This is the most critical problem is the transformation model. At first I did not think of death because of positive solutions to ignore the whole right side is \ (1 \) .
Set point \ (I \) point \ (J \) is the shortest length of \ (DIS [I] [J] \) . At this condition can be converted into the original request point \ (S_1 \) point \ (T_l \) is the shortest path and the point \ (S_2 \)Point \ (T_2 \) path through the shortest path and the number of edges.
Of course, this has \ (3 \) cases:
The first one is the point \ (S_1 \) point \ (T_l \) is shortest and the point \ (S_2 \) point (T_2 \) \ Shortest no overlapping portions. This time adding two shortest values directly on the line.
The second is the point \ (s_1 \) to the point \ (t_1 \) is the shortest and the point \ (s_2 \) to the point \ (t_2 \) is the shortest at the point \ (i \) and point \ (j \ ) overlap. This time may be \ (O (n ^ 2) \) enumeration of overlapping dots \ (I \) and \ (J \) , in ensuring the path length limitation updating answer on the line, the answer is \ (dis [s_1] [ I] + DIS [S_2] [I] + DIS [I] [J] + DIS [J] [T_l] + DIS [J] [T_2] \) .
The third is the point \ (s_1 \) point\ (T_l \) is shortest and the point \ (S_2 \) point \ (T_2 \) is the shortest at the point \ (I \) and the point \ (J \) between the overlapping, but the point \ (S_2 \) the point \ (t_2 \) is the shortest path from point \ (i \) to point \ (j \) , and the point \ (s_1 \) to the point \ (t_1 \) is the shortest path from point \ (j \) to point \ (i \) . This time the same as above, the answer is \ (dis [s_1] [j ] + dis [s_2] [i] + dis [i] [j] + dis [i] [t_1] + dis [j] [t_2] \) .
As most any two points short-circuit evaluation Well, do \ (n \) times \ (bfs \) on the line.
The whole process is so, if you have any do not understand, see the comments section! Thank you for your patience to read!
code show as below:

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int ret=0,f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
int n,m,x,y,s1,s2,t1,t2,l1,l2,ans;
int num,head[6005],dis[3005][3005],vis[3005];
struct edge
{
    int ver,nxt;
}e[6005];
queue<int> q;
inline void adde(int u,int v)
{
    e[++num].ver=v;
    e[num].nxt=head[u];
    head[u]=num;
}
inline void bfs(int s)
{
    memset(vis,0,sizeof(vis));
    q.push(s);
    vis[s]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(register int i=head[x];i;i=e[i].nxt)
        {
            int y=e[i].ver;
            if(vis[y])
                continue;
            vis[y]=1;
            dis[s][y]=dis[s][x]+1;
            q.push(y);
        }
    }
}
int main()
{
    n=read();
    m=read();
    for(register int i=1;i<=m;i++)
    {
        x=read();
        y=read();
        adde(x,y);
        adde(y,x);
    }
    for(register int i=1;i<=n;i++)
        bfs(i);
    s1=read();
    t1=read();
    l1=read();
    s2=read();
    t2=read();
    l2=read();
    if(dis[s1][t1]>l1||dis[s2][t2]>l2)
    {
        printf("-1\n");
        return 0;
    }
    ans=dis[s1][t1]+dis[s2][t2];
    for(register int i=1;i<=n;i++)
    {
        for(register int 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][j]+dis[i][j]+dis[i][t1]<=l1&&dis[s2][i]+dis[i][j]+dis[j][t2]<=l2)
                ans=min(ans,dis[s1][j]+dis[i][j]+dis[i][t1]+dis[s2][i]+dis[j][t2]);
        }
    }
    printf("%d\n",m-ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Peter0701/p/11519439.html