• HDU1815 Building roads(二分+2-SAT)


    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

    思路是没有问题的,但是写的有点丑,很多地方还可以合并。一开始思路就没有问题的,但是这种题就是找bug很头疼。QwQ!

    结果是maxn开了510,忘记了两倍,晕死了。

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int maxn=1010;
    const int B=1;
    const int R=2;
    const int W=0;
    vector<int>G[maxn];
    vector<int>G2[maxn];
    int dis[3][maxn],Dis;//Dis是S点,T点 
    int x,y,x1,y1,x2,y2,a,b,n,ans;
    int col[maxn],q[maxn],num;
    void init()
    {
        for(int i=1;i<=2*n;i++) G[i].clear();
        for(int i=1;i<=2*n;i++) G2[i].clear();
        ans=-1;
    }
    void scan()
    {
            int i;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            Dis=abs(x1-x2)+abs(y1-y2);
            for(i=1;i<=n;i++){
                    scanf("%d%d",&x,&y);
                    dis[1][i]=abs(x-x1)+abs(y-y1);
                    dis[2][i]=abs(x-x2)+abs(y-y2);
            }
            for(i=1;i<=a;i++){
                    scanf("%d%d",&x,&y);
                    G[x].push_back(y+n);
                    G[x+n].push_back(y);
                    G[y].push_back(x+n);
                    G[y+n].push_back(x);
            }
            for(i=1;i<=b;i++){
                    scanf("%d%d",&x,&y);
                    G[x].push_back(y);
                    G[y].push_back(x);
                    G[x+n].push_back(y+n);
                    G[y+n].push_back(x+n);
            }
            
    }
    bool dfs(int u)
    {
        if(col[u]==R) return false;
        if(col[u]==B) return true;
        col[u]=B;
        col[u>n?u-n:u+n]=R;
        q[++num]=u;
        for(int i=0;i<G[u].size();i++)  if(!dfs(G[u][i])) return false;
        for(int i=0;i<G2[u].size();i++) if(!dfs(G2[u][i])) return false;
        return true;
    }
    bool check(int x)
    {
        int i,j;
        for(i=1;i<=n;i++) if(dis[1][i]>x&&dis[2][i]>x) return false;
        for(i=1;i<=2*n;i++) G2[i].clear();
        for(i=1;i<=2*n;i++) col[i]=0; 
        for(i=1;i<=n;i++)
         for(j=i+1;j<=n;j++){
              int d1=dis[1][i]+dis[1][j];
             int d2=dis[1][i]+dis[2][j]+Dis;
             int d3=dis[2][i]+dis[2][j];
             int d4=dis[2][i]+dis[1][j]+Dis;
             if(d1>x&&d2>x&&d3>x&&d4>x) return false;
             if(d1>x){
                    G2[i].push_back(j+n);
                    G2[j].push_back(i+n);
             }
             if(d2>x){
                    G2[i].push_back(j);
                    G2[j+n].push_back(i+n);
             }
             if(d3>x){
                    G2[i+n].push_back(j);
                    G2[j+n].push_back(i);
             }
             if(d4>x){
                    G2[i+n].push_back(j+n);
                    G2[j].push_back(i);
             }
        }
         for(i=1;i<=2*n;i++){
              if(col[i]) continue;
              num=0;
              if(!dfs(i)){
                  for(j=1;j<=num;j++) {
                      col[q[j]>n?q[j]-n:q[j]+n]=W;
                      col[q[j]]=W;
                  }
                  if(!dfs(i>n?i-n:i+n)) return false;    
              }      
         }
         return true;
    }
    int main()
    {
         while(~scanf("%d%d%d",&n,&a,&b)){
                init();
                int L=0,R=8000000;
                scan();
                while(L<=R){
                    int mid=(L+R)>>1;
                    if(check(mid)){ ans=mid;R=mid-1;}
                    else  L=mid+1;
                }
                printf("%d
    ",ans);
         }
         return 0;
    }

    下面这样建图有些问题,读者可以思考一下。

             if(d2<=x&&d1>x&&d3>x){
                    G2[i].push_back(j+n);
                    G2[j+n].push_back(i);
             } 
             if(d1<=x&&d2>x&&d4>x){
                    G2[i].push_back(j);
                    G2[j].push_back(i);
             }
             if(d4<=x&&d3>x&&d1>x){
                    G2[i+n].push_back(j);
                    G2[j].push_back(i+n);
             }
             if(d3<=x&&d4>x&&d2>x){
                    G2[i+n].push_back(j+n);
                    G2[j+n].push_back(i+n);
             }
  • 相关阅读:
    hdu 1163 Eddy's digital Roots (数学)
    hdu 2546 饭卡 (01背包)
    hdu 1059 Dividing(多重DP)
    晚霞
    最佳学习方法
    [备忘]求两数最大公约,最小公倍数
    不眠的夏夜
    超女唱《八荣八耻》:好完美的恶搞啊
    公司展会上的德国MM
    我玩游戏
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7862618.html
Copyright © 2020-2023  润新知