• HDU1815(二分+2-SAT)


    Building roads

    Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1569    Accepted Submission(s): 490


    Problem Description

    Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some roads to connect these barns. If he builds roads for every pair of different barns, then he must build N * (N - 1) / 2 roads, which is so costly that cheapskate John will never do that, though that's the best choice for the cows. 

    Clever John just had another good idea. He first builds two transferring point S1 and S2, and then builds a road connecting S1 and S2 and N roads connecting each barn with S1 or S2, namely every barn will connect with S1 or S2, but not both. So that every pair of barns will be connected by the roads. To make the cows don't spend too much time while dropping around, John wants to minimize the maximum of distances between every pair of barns. 

    That's not the whole story because there is another troublesome problem. The cows of some barns hate each other, and John can't connect their barns to the same transferring point. The cows of some barns are friends with each other, and John must connect their barns to the same transferring point. What a headache! Now John turns to you for help. Your task is to find a feasible optimal road-building scheme to make the maximum of distances between every pair of barns as short as possible, which means that you must decide which transferring point each barn should connect to. 

    We have known the coordinates of S1, S2 and the N barns, the pairs of barns in which the cows hate each other, and the pairs of barns in which the cows are friends with each other. 

    Note that John always builds roads vertically and horizontally, so the length of road between two places is their Manhattan distance. For example, saying two points with coordinates (x1, y1) and (x2, y2), the Manhattan distance between them is |x1 - x2| + |y1 - y2|. 

     

    Input

    The first line of input consists of 3 integers N, A and B (2 <= N <= 500, 0 <= A <= 1000, 0 <= B <= 1000), which are the number of barns, the number of pairs of barns in which the cows hate each other and the number of pairs of barns in which the cows are friends with each other. 

    Next line contains 4 integer sx1, sy1, sx2, sy2, which are the coordinates of two different transferring point S1 and S2 respectively. 

    Each of the following N line contains two integer x and y. They are coordinates of the barns from the first barn to the last one. 

    Each of the following A lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows hate each other. 

    The same pair of barns never appears more than once. 

    Each of the following B lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows are friends with each other. The same pair of barns never appears more than once. 

    You should note that all the coordinates are in the range [-1000000, 1000000]. 
     

    Output

    You just need output a line containing a single integer, which represents the maximum of the distances between every pair of barns, if John selects the optimal road-building scheme. Note if there is no feasible solution, just output -1. 
     

    Sample Input

    4 1 1 12750 28546 15361 32055 6706 3887 10754 8166 12668 19380 15788 16059 3 4 2 3
     

    Sample Output

    53246
     

    Source

     

    思路:二分枚举最大值limit,然后重新构图,用2-SAT判定可行性。用Xi表示第i个牛棚连到S1,~Xi表示连到S2,检查每一个约束条件,构图:

    1.hate关系的i,j Xi->~Xj ~Xi->Xj Xj->~Xi ~Xj->Xi
    2.friend关系的i,j Xi->Xj ~Xi->~Xj Xj->Xi ~Xj->~Xi
    接下来的也要检查,因为引入参数,就是多了约束条件了
    这四种情况就是i,j到达对方的所有情况了
    3.dist(i,S1)+dist(S1,j)>limit Xi->~Xj Xj->Xi
    4.dist(i,S2)+dist(S2,j)>limit ~Xi->Xj ~Xj->Xi
    5.dist(i,S1)+dist(S1,S2)+dist(S2,j)>limit Xi->Xj ~Xj->~Xi
    5.dist(i,S2)+dist(S2,S1)+dist(S1,j)>limit ~Xi->~Xj Xj->Xi

    然后求强连通分量判断Xi与~Xi是否在同一个连通分量中,是的话就有矛盾。

      1 //2017-08-28
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <iostream>
      5 #include <algorithm>
      6 #include <vector>
      7 #include <cmath>
      8 
      9 using namespace std;
     10 
     11 const int N = 5010;
     12 const int M = N*N*2;
     13 int head[N], rhead[N], tot, rtot;
     14 struct Edge{
     15     int to, next;
     16 }edge[M], redge[M];
     17 
     18 void init(){
     19     tot = 0;
     20     rtot = 0;
     21     memset(head, -1, sizeof(head));
     22     memset(rhead, -1, sizeof(rhead));
     23 }
     24 
     25 void add_edge(int u, int v){
     26     edge[tot].to = v;
     27     edge[tot].next = head[u];
     28     head[u] = tot++;
     29 
     30     redge[rtot].to = u;
     31     redge[rtot].next = rhead[v];
     32     rhead[v] = rtot++;
     33 }
     34 
     35 vector<int> vs;//后序遍历顺序的顶点列表
     36 bool vis[N];
     37 int cmp[N];//所属强连通分量的拓扑序
     38 
     39 //input: u 顶点
     40 //output: vs 后序遍历顺序的顶点列表
     41 void dfs(int u){
     42     vis[u] = true;
     43     for(int i = head[u]; i != -1; i = edge[i].next){
     44         int v = edge[i].to;
     45         if(!vis[v])
     46           dfs(v);
     47     }
     48     vs.push_back(u);
     49 }
     50 
     51 //input: u 顶点编号; k 拓扑序号
     52 //output: cmp[] 强连通分量拓扑序
     53 void rdfs(int u, int k){
     54     vis[u] = true;
     55     cmp[u] = k;
     56     for(int i = rhead[u]; i != -1; i = redge[i].next){
     57         int v = redge[i].to;
     58         if(!vis[v])
     59           rdfs(v, k);
     60     }
     61 }
     62 
     63 //Strongly Connected Component 强连通分量
     64 //input: n 顶点个数
     65 //output: k 强连通分量数;
     66 int scc(int n){
     67     memset(vis, 0, sizeof(vis));
     68     vs.clear();
     69     for(int u = 0; u < n; u++)
     70       if(!vis[u])
     71         dfs(u);
     72     int k = 0;
     73     memset(vis, 0, sizeof(vis));
     74     for(int i = vs.size()-1; i >= 0; i--)
     75       if(!vis[vs[i]])
     76         rdfs(vs[i], k++);
     77     return k;
     78 }
     79 
     80 int n, A, B, dis_s1[N], dis_s2[N], dis_s1_s2;
     81 struct Point{
     82     int x, y;
     83 }point[N], s1, s2, hate[N], friends[N];
     84 
     85 //input: 两个点
     86 //output: 两点间距离
     87 double distance(Point a, Point b){
     88     return abs(a.x-b.x) + abs(a.y-b.y);
     89 }
     90 
     91 bool check(int limit){
     92     init();
     93     // i 表示 i 连 s1, NOT i 表示 i 连 s2
     94     for(int i = 0; i < n; i++){
     95         bool fg = true;
     96         if(distance(point[i], s1) > limit){
     97               add_edge(i, i+n);
     98             fg = false;
     99         }
    100         if(distance(point[i], s2) > limit){
    101             if(!fg)return false;
    102               add_edge(i+n, i);
    103         }
    104         for(int j = i+1; j < n; j++){
    105             if(dis_s1[i] + dis_s1[j] > limit){
    106                 add_edge(i, j+n);// i -> s1, j -> s2
    107                 add_edge(j, i+n);// j -> s1, i -> s2
    108             }
    109             if(dis_s2[i] + dis_s2[j] > limit){
    110                 add_edge(i+n, j);// i -> s2, j -> s1
    111                 add_edge(j+n, i);// j -> s2, i -> s1
    112             }
    113             if(dis_s1[i] + dis_s1_s2 + dis_s2[j] > limit){
    114                 add_edge(i, j);// i -> s1, j -> s1
    115                 add_edge(j+n, i+n);// j -> s2, i -> s2
    116             }
    117             if(dis_s2[i] + dis_s1_s2 + dis_s1[j] > limit){
    118                 add_edge(i+n, j+n);// i -> s2, j -> s2
    119                 add_edge(j, i);// j -> s1, i -> s1
    120             }
    121         }
    122     }
    123     for(int i = 0; i < A; i++){
    124         int u = hate[i].x, v = hate[i].y;
    125         add_edge(u, v+n);
    126         add_edge(v+n, u);
    127         add_edge(v, u+n);
    128         add_edge(u+n, v);
    129     }
    130     for(int i = 0; i < B; i++){
    131         int u = friends[i].x, v = friends[i].y;
    132         add_edge(u, v);
    133         add_edge(v, u);
    134         add_edge(u+n, v+n);
    135         add_edge(v+n, u+n);
    136     }
    137     scc(2*n);
    138     for(int i = 0; i < n; i++){
    139         if(cmp[i] == cmp[i+n])
    140               return false;
    141     }
    142     return true;
    143 }
    144 
    145 int main()
    146 {
    147     std::ios::sync_with_stdio(false);
    148     //freopen("inputE.txt", "r", stdin);
    149     while(cin>>n>>A>>B){
    150         cin>>s1.x>>s1.y>>s2.x>>s2.y;
    151         dis_s1_s2 = distance(s1, s2);
    152         for(int i = 0; i < n; i++){
    153               cin>>point[i].x>>point[i].y;
    154             dis_s1[i] = distance(point[i], s1);
    155             dis_s2[i] = distance(point[i], s2);
    156         }
    157         for(int i = 0; i < A; i++){
    158               cin>>hate[i].x>>hate[i].y;
    159             hate[i].x--;
    160             hate[i].y--;
    161         }
    162         for(int i = 0; i < B; i++){
    163             cin>>friends[i].x>>friends[i].y;
    164             friends[i].x--;
    165             friends[i].y--;
    166         }
    167         //r 开小了HDU会TLE,ORZ。。。
    168         int l = 0, r = 8000000, mid, ans = -1;
    169         while(l <= r){
    170             mid = (l+r)/2;
    171             if(check(mid)){
    172                 ans = mid;
    173                 r = mid-1;
    174             }else l = mid+1;
    175         }
    176         cout<<ans<<endl;
    177     }
    178     return 0;
    179 }
  • 相关阅读:
    .net core实现的全程序跟踪
    gmap.net
    Spring Cloud实践:降级、限流、滚动、灰度、AB、金丝雀的实现思路
    服务的协作:服务间的消息传递——《微服务设计》读书笔记
    使用消息系统进行微服务间通讯时,如何保证数据一致性
    How to distribute a database among microservices
    微服务间如何选择推送和拉取数据
    Android 怎么使用Bitmap+Canvas 自适应屏幕
    Android 音乐播放器之--错误状态下调用导致的异常
    Android应用截图和SurfaceView截图问题总结
  • 原文地址:https://www.cnblogs.com/Penn000/p/7443233.html
Copyright © 2020-2023  润新知