zoukankan      html  css  js  c++  java
  • Codeforces Round #302 (Div. 1) B. Destroying Roads

    传送门:http://codeforces.com/problemset/problem/543/B

    B. Destroying Roads

    time limit per test:2 seconds
    memory limit per test:256 megabytes

      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

    题目大意:给你一个无向图,问你保证两对起点与终点的情况下,能删掉边的最大数量。
    题解:

      注意到此题就是求解两条符合条件的路径尽量有更多的重合的部分。我们想,两个起点在某一个点重合了,分开之后,再次重合?不可能啊!所以显然的排除了这一种情况,我们就可以枚举两个点,这样代表重合的点和分开的点,然后每次去最小即可。但是有一点,不能使用Floyd,因为边权都是1,所以n2的bfs即可!考试的时候就是思维固化,直接就想Floyd了。
      还有一种特殊情况,就是两条路径压根不会相交,这样把初始值赋值为这个即可。

     1 #include<queue>
     2 #include<cstdio>
     3 #include<vector>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 #define RG register
     8 #define LL long long
     9 #define min(a,b) (a)>(b)?(b):(a)
    10 #define fre(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
    11 using namespace std;
    12 const int MAXN=3100,INF=0x3f3f3f3f;
    13 int n,m,s1,t1,l1,s2,t2,l2,num;
    14 int head[MAXN],to[MAXN*2],Next[MAXN*2];
    15 int dis[MAXN][MAXN];
    16 bool vis[MAXN];
    17 queue<int>Q;
    18 void add(int f,int t)
    19 {
    20    Next[++num]=head[f];
    21    to[num]=t;
    22    head[f]=num;
    23 }
    24 void bfs(RG int S)
    25 {
    26    memset(vis,0,sizeof vis);
    27    dis[S][S]=0;
    28    Q.push(S); vis[S]=1;
    29    while(!Q.empty())
    30       {
    31          RG int u=Q.front();
    32          Q.pop();
    33          for(int i=head[u];i;i=Next[i])
    34             {
    35                RG int v=to[i];
    36                if(vis[v])continue;
    37                dis[S][v]=dis[S][u]+1;
    38                vis[v]=1;
    39                Q.push(v);
    40             }
    41       }
    42 }
    43 int main()
    44 {
    45    scanf("%d%d",&n,&m);
    46    int Min;
    47    memset(dis,0x3f3f3f3f,sizeof dis);
    48    for(int i=1,a,b;i<=m;i++)
    49       {
    50          scanf("%d%d",&a,&b);
    51          add(a,b); add(b,a);
    52       }
    53    scanf("%d%d%d%d%d%d",&s1,&t1,&l1,&s2,&t2,&l2);
    54    for(int i=1;i<=n;i++) bfs(i);
    55    Min=dis[s1][t1]+dis[s2][t2];
    56    for(RG int i=1,val1,val2;i<=n;i++)
    57       for(RG int j=1;j<=n;j++)
    58          {
    59             val1=dis[s1][i]+dis[s2][i]+dis[i][j]+dis[j][t1]+dis[j][t2];
    60             val2=dis[s1][i]+dis[t2][i]+dis[i][j]+dis[j][t1]+dis[j][s2];
    61             Min=min(Min,min(val1,val2));
    62          }
    63    if(dis[s1][t1]>l1 || dis[s2][t2]>l2) printf("That's all trouble!
    ");
    64    else printf("%d
    ",m-Min);
    65    return 0;
    66 }
  • 相关阅读:
    java异常
    集群、分布式、负载均衡区别
    基本数据结构的比较
    记录一次tomcat问题排查记录:org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
    记录 Java 的 BlockingQueue 中的一些坑
    Java高级面试题解析(二):百度Java面试题前200页(精选)
    学习netty遇到的关于 LineBasedFrameDecoder 的问题
    ThreadPoolExecutor 入参 corePoolSize 和 maximumPoolSize 的联系
    TreeMap 的排序冲突吗
    checkbox jquery操作总结
  • 原文地址:https://www.cnblogs.com/D-O-Time/p/7757814.html
Copyright © 2011-2022 走看看