hdu 1815 Building roads

Building roads

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

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

这条题卡了好久呀 -。-

卡完构图卡空间, 卡完空间卡二分范围 , 右区间要去到 800W , 500W返WA 。

卡完二分范围卡编译器问题 ,

不知道为啥  , 把run() 部分写在 主函数交 C++会超时, 交G++就过。

条题其实很入门 , 依然是 2分 + two-sat ....

处理距离牛与牛之间的距离那里也先大好表,避免超时吧~

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>

using namespace std;
typedef long long LL;

const int N = 1010;
const int M = 1000010;
const int MAX = 8000000;

int n , A , B  , st[N] , top , dis_s1_s2 ;
int eh[N] , et[M] , nxt[M] , tot ;

bool mark[N];

struct node
{
    int x , y ;
}e[N] , h[N<<1] , s[2] ;

int d[510][3] ;

void init()
{
    tot = 0;
    memset ( eh , -1 ,sizeof eh );
    memset ( mark , false , sizeof mark );
}

void addedge( int u , int v)
{
    et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot ++ ;
    et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot ++ ;
}
//--------------------

inline int dis( int x1 , int y1 , int x2 , int y2 ){ return abs( x1 - x2 ) + abs( y1 - y2 ); }

bool dfs( int u )
{
    if( mark[u] ) return true ;
    if( mark[u^1] ) return false ;
    mark[u] = true;
    st[top++]  = u ;
    for( int i = eh[u] ; ~i ; i = nxt[i] ){
        int v = et[i];
        if( !dfs( v^1 ) ) return false ;
    }
    return true ;

}

bool solve( )
{
    for( int i = 0 ; i < 2 * n ; i += 2 ){
        if( !mark[i] && !mark[i+1] ){
            top = 0 ;
            if( !dfs( i ) ){
                while( top > 0 ) mark[ st[--top] ] = false ;
                if( !dfs( i + 1 ) ) return false ;
            }
        }
    }
    return true;
}

bool test( double DIS )
{
    init();

    for( int i = 0 ; i < A + B ; ++i ){
        if( i < A ) addedge( 2*h[i].x , 2*h[i].y ) , addedge( 2*h[i].x^1 , 2*h[i].y^1 ) ;
        else addedge( 2*h[i].x^1 , 2*h[i].y ) , addedge( 2*h[i].x , 2*h[i].y^1 ) ;
    }

    for( int i = 0 ; i < n ; i ++ ){
        for( int j = i + 1 ; j < n ; j ++ ){
            if(  d[i][0] + d[j][0] > DIS  ) addedge( 2*i , 2*j );
            if(  d[i][1] + d[j][0] + dis_s1_s2 > DIS  ) addedge( 2*i^1 , 2*j );
            if(  d[i][0] + d[j][1] + dis_s1_s2 > DIS  ) addedge( 2*i , 2*j^1 );
            if(  d[i][1] + d[j][1] > DIS  ) addedge( 2*i^1 , 2*j^1 );
        }
    }
    return solve();
}

void run()
{
    int x , y ;

    for( int i = 0 ; i < 2 ; ++i )
        scanf("%d%d",&s[i].x,&s[i].y);

    dis_s1_s2 = dis( s[0].x,s[0].y,s[1].x,s[1].y );

    for( int i = 0 ; i < n ; ++i ){
        scanf("%d%d",&x,&y );
        d[i][0] = dis( x , y , s[0].x , s[0].y );
        d[i][1] = dis( x , y , s[1].x , s[1].y );
    }

    for( int i = 0 ; i < A + B ; ++i ){
        scanf("%d%d",&h[i].x,&h[i].y);
        h[i].x -- ;h[i].y -- ;
    }

    int ans = -1 , l = 0 , r = MAX ;

    while( l <= r )
    {
        int mid = ( l + r ) >> 1 ;
        if( test(mid) )
            ans = mid , r = mid - 1 ;
        else
            l = mid + 1 ;
    }
    printf("%d\n",ans);
}

int main()
{
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif
    while( scanf("%d%d%d",&n,&A,&B) != EOF )run();
}
时间: 2024-12-13 01:54:17

hdu 1815 Building roads的相关文章

HDU - 1815 Building roads (2-SAT)

题目大意:给出N个农场,S1场地和S2场地的坐标,要求每个农场要么和S1场地连接要么和S2场地连接,且每个农场之间的连接距离的最大值最小 现在给出A种不可连接限制,表明两个农场不能和同一个场地连接 又给出B种连接限制,表明两个农场要和同一个场地连接 解题思路:这题处理时有点恶心 二分枚举最大距离L,判断一下每个农场可连接的场地(以下的连边表示,a表示和S1连接,!a表示和S2连接) 如果dist(a,S1) > L,那么a农场只能和S2连接了,连边a -> !a (a->!a表示只要df

HDU 1815, POJ 2749 Building roads(2-sat)

HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得任意牛棚两个都可以有道路联通,现在要让每个牛棚都连接一条路到S1或者S2. 有a对牛棚互相有仇恨,所以不能让他们的路连接到同一个中转站.还有b对牛棚互相喜欢,所以他们的路必须连到同一个中专站. 道路的长度是两点的曼哈顿距离. 问最小的任意两牛棚间的距离中的最大值是多少? 思路:二分距离,考虑每两个牛棚之间4种连

hdu oj1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13995    Accepted Submission(s): 5324 Problem Description There are N villages, which are numbered from 1 to N, and you should

hdu 1301 Jungle Roads

链接:hdu 1301 题意:n个村庄,已知n-1村庄分别到其他村庄修路的费用,求是n个村庄连通的最小费用 分析:这个是最小生成树的题,只不过村庄的编号为A-Z的大写字母,操作比较麻烦,可以将其对应转化为1-26, 这样就与普通的最小生成树题一样了 #include<cstdio> #include<algorithm> using namespace std; int f[50],n,m; struct stu { int a,b,c; }t[300]; int cmp(stru

[BZOJ1626][Usaco2007 Dec]Building Roads 修建道路

1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1730  Solved: 727 [Submit][Status][Discuss] Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场之间原本就有道路相连.

HDU 1102 Constructing Roads, Prim+优先队列

题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are conne

poj 3625 Building Roads

题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already conn

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom   LIS 简单题 好题 超级坑

Constructing Roads In JGShining's Kingdom Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) whi

hdu 1301 Jungle Roads (基础最小生成树)

题目: 链接:点击打开链接 题意: 对n个村庄之间的路进行修理, 然后是n-1行,每行的第一组数据时一个大写字母VIL和一个数K,Vil表示从这个村庄出发,K表示刚才的那个字母代表的村庄和其他村庄的路的数目,接下来在同一行是K组数据,每组是一个大写字母和一个数,大写字母表示和第一个村庄连接的村庄,数表示维修他们之间的路所需的费用.现在为了使维修费油最低,只需所维修的路使每个村庄都是直接或间接的连接即可,求最小的费用. 思路: 只需把输入数据处理好即可.其他都是kruskal模板.' 代码: #i