zoukankan      html  css  js  c++  java
  • TTTTTTTTTTT POJ 2749 修牛棚 2-Sat + 路径限制 变形

    Building roads
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7019   Accepted: 2387

    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

    题意:题意:给出n个牛棚、两个特殊点S1,S2的坐标。S1、S2直连。牛棚只能连S1或S2
               还有,某些牛棚只能连在同一个S,某些牛棚不能连在同一个S
               求使最长的牛棚间距离最小  距离是曼哈顿距离
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <algorithm>
    #include <set>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long Ull;
    #define MM(a,b) memset(a,b,sizeof(a));
    const double eps = 1e-10;
    const int  inf =0x7f7f7f7f;
    const double pi=acos(-1);
    const int maxn=100+1000;
    
    struct Point{
       int x,y;
       void read()
       {
           scanf("%d %d",&x,&y);
       }
    }p[510];
    vector<int> g[2*maxn],G[2*maxn];
    stack<int> S;
    Point s1,s2;
    int n,a,b,u,v,scc_cnt,dfs_clock,pre[2*maxn],sccno[2*maxn],lowlink[2*maxn];
    
    int dis(Point a,Point b)
    {
       return abs(a.x-b.x)+abs(a.y-b.y);
    }
    
    void add_edgeG(int u,int v)
    {
       G[u].push_back(v);
    }
    
    void add_edge(int u,int v)
    {
       g[u].push_back(v);
    }
    
    void build(int mid)
    {
       for(int i=1;i<=n;i++)
          for(int j=i+1;j<=n;j++)
           {
                int u=i<<1,v=j<<1;
                if(dis(p[i],s1)+dis(s1,p[j])>mid)
                   {
                       add_edge(u,v^1);
                       add_edge(v,u^1);
                   }
                if(dis(p[i],s2)+dis(s2,p[j])>mid)
                   {
                       add_edge(u^1,v);
                       add_edge(v^1,u);
                   }
                if(dis(p[i],s1)+dis(s1,s2)+dis(s2,p[j])>mid)
                   {
                       add_edge(u,v);
                       add_edge(v^1,u^1);
                   }
                if(dis(p[i],s2)+dis(s2,s1)+dis(s1,p[j])>mid)
                   {
                       add_edge(u^1,v^1);
                       add_edge(v,u);
                   }
           }
    }
    
    void tarjan(int u)
    {
         pre[u]=lowlink[u]=++dfs_clock;
         S.push(u);
         for(int i=0;i<g[u].size();i++)
         {
             int v=g[u][i];
             if(!pre[v])
             {
                 tarjan(v);
                 lowlink[u]=min(lowlink[v],lowlink[u]);
             }
             else if(!sccno[v])
                 lowlink[u]=min(lowlink[u],pre[v]);
         }
    
         if(pre[u]==lowlink[u])
         {
             scc_cnt++;
             for(;;)
             {
                 int w=S.top();S.pop();
                 sccno[w]=scc_cnt;
                 if(w==u) break;
             }
         }
    }
    
    bool find_scc(int mid)
    {
         for(int i=2;i<=2*n+1;i++) g[i].clear();
         for(int u=2;u<=2*n+1;u++)
         for(int i=0;i<G[u].size();i++)
             g[u].push_back(G[u][i]);
         build(mid);
    
         MM(lowlink,0);
         MM(sccno,0);
         MM(pre,0);
         scc_cnt=dfs_clock=0;
         for(int i=2;i<=2*n+1;i++)
            if(!pre[i])
               tarjan(i);
    
         for(int i=2;i<=2*n;i+=2)
            {
                if(sccno[i]==sccno[i^1])
                 return false;
            }
         return true;
    }
    
    int main()
    {
        while(~scanf("%d %d %d",&n,&a,&b))
        {
             for(int i=2;i<=2*n+1;i++) G[i].clear();
             s1.read();
             s2.read();
             for(int i=1;i<=n;i++)  p[i].read();
    
             for(int i=0;i<a;i++)
             {
                 scanf("%d %d",&u,&v);
                 u<<=1;
                 v<<=1;
                 add_edgeG(u,v^1);
                 add_edgeG(v,u^1);
                 add_edgeG(u^1,v);
                 add_edgeG(v^1,u);
             }
    
             for(int i=0;i<b;i++)
             {
                 scanf("%d %d",&u,&v);
                 u<<=1;
                 v<<=1;
                 add_edgeG(u,v);
                 add_edgeG(v,u);
                 add_edgeG(u^1,v^1);
                 add_edgeG(v^1,u^1);
             }
    
             int l=-1,r=5*1e6+100;
             while(r-l>1)
             {
                 int mid=(l+r)>>1;
                 if(find_scc(mid))
                        r=mid;
                 else l=mid;
             }
             if(r>5*1e6) printf("-1
    ");
             else printf("%d
    ",r);
        }
        return 0;
    }
    

      分析:与模板题最大的不同就是要求在满足有连接方案存在的情况下,图中存在的最长简单路径尽可能短,处理方式如下:

    1.因为是最长路径尽可能短,所以二分最大路径。

    2.二分出一个mid值后,将每两个节点之间的四种组合的路径长度与mid比较,从而根据要让所有路径长度

    都不能超过mid这一假设条件进行再次连边;

    3.因为要多次连边,所以需要设置两个邻接表,一个记录根据喜欢与讨厌的关系保存边,另一个在前一个的基础上加上路径限制

  • 相关阅读:
    (4)ES6解构赋值-字符串篇
    (3)ES6解构赋值-对象篇
    (2)ES6解构赋值-数组篇
    (1)ES6中let,const,对象冻结,跨模块常量,新增的全局对象介绍
    MySQL中char与varchar区别,varchar最大长度是多少?
    集成学习实战——Boosting(GBDT,Adaboost,XGBoost)
    集成学习——Boosting(GBDT,Adaboost,XGBoost)
    集成学习(Random Forest)——实践
    集成学习——Bagging
    决策树实践
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5498397.html
Copyright © 2011-2022 走看看