HDU 1815 building roads

Source: Internet
Author: User

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 descriptionfarmer John's farm has n barns, and there are some cows that live in each barn. the cows like to drop around und, 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 shoshould connect.

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 |.

 

Inputthe 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 shoshould note that all the coordinates are in the range [-1000000,100 0000].

 

Outputyou 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 input4 1 112750 28546 15361 320556706 388710754 816612668 1938015788 160593 42 3

 

Sample output53246

 

 

This question has been stuck for a long time -. -

Card space after card layout, card space within two points, right range to 800 W, W back to WA.

Problem with the binary range card Compiler,

I don't know why. Writing the run () section in the main function to commit C ++ will time out, And the commit g ++ will pass.

 

The question is actually very quick start, still 2 + two-sat ....

Processing the distance between a cow and a cow is also a good table. Avoid timeout ~

 

 

#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();}

 

 

 

HDU 1815 building roads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.