【2-SAT】HDU1815 building roads

B u i l d i n g   r o a d s Building\ roads

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

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
POJ Monthly - 2006.01.22 - zhucheng

题目大意:

给出N头牛和两个汇点坐标,每头牛都要和汇点连接(两个汇点之间也有连接),其中有些牛不能连接到同一个汇点,有些牛只能连接到同一个汇点,要求计算出符合上述条件的任意两头牛之间的距离的最大值的最小值。

首先看到那个最大值的最小值,目测就要用二分来做。因为有两个汇点,还有一些约束条件,所以可以根据2-SAT进行建图,建图规则如下:
一头牛只能连接到两个汇点中的一个点,所以可以根据这个性质,进行建边,首先计算出每头牛到两个汇点sa、sb的距离dist[ i ]、dist[ i+n ]和两个汇点之间的距离sdist,对距离的最大值进行二分,根据当前二分的距离d建边
dist[i]+dist[j]>d:i->j+n , j->i+n
dist[i+n]+dist[j+n]>d:i+n->j , j+n->i
dist[i]+sdist+dist[j+n]>d:i->j , j+n->i+n
dist[i+n]+sdist+dist[j]>d:i+n->j+n , j->i

然后再根据hate和like建边
hate[i].cowa->hate[i].cowb+n
hate[i].cowa+n->hate[i].cowb
hate[i].cowb->hate[i].cowa+n
hate[i].cowb+n->hate[i].cowa

like[i].cowa->like[i].cowb
like[i].cowb->like[i].cowa
like[i].cowa+n->like[i].cowb+n
like[i].cowb+n->like[i].cowa+n

然后每次二分建图然后跑一边强连通分量分解,判断是否出现矛盾

CODE

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
const int maxn = 1e3+7;
struct COOR{
    int x,y;
};
struct PAIR{
    int cowa,cowb;
};
int n,a,b,sdist;
COOR sa,sb,cow[maxn];
PAIR hate[maxn],like[maxn];
int dist[maxn];
int id[maxn],topoid[maxn],used[maxn];
vector<int> G[maxn];
vector<int> rG[maxn];
void add_edge(int u,int v){
    G[u].push_back(v);
    rG[v].push_back(u);
}
int my_distance(COOR a,COOR b){
    int dx = abs(a.x-b.x);
    int dy = abs(a.y-b.y);
    return dx+dy;
}

bool input(){
    if(scanf("%d",&n)==EOF) return false;
    scanf("%d %d",&a,&b);
    scanf("%d %d %d %d",&sa.x,&sa.y,&sb.x,&sb.y);
    sdist = my_distance(sa,sb);
    for(int i=1;i<=n;i++){
        scanf("%d %d",&cow[i].x,&cow[i].y);
        dist[i] = my_distance(cow[i],sa);
        dist[i+n] = my_distance(cow[i],sb);
    }
    for(int i=1;i<=a;i++) scanf("%d %d",&hate[i].cowa,&hate[i].cowb);
    for(int i=1;i<=b;i++) scanf("%d %d",&like[i].cowa,&like[i].cowb);
    return true;
}
void init(){
    for(int i=0;i<maxn;i++) G[i].clear();
    for(int i=0;i<maxn;i++) rG[i].clear();
}
void build(int d){
    init();
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(dist[i]+dist[j]>d) add_edge(i,j+n),add_edge(j,i+n);
            if(dist[i+n]+dist[j+n]>d) add_edge(i+n,j),add_edge(j+n,i);
            if(dist[i]+sdist+dist[j+n]>d) add_edge(i,j),add_edge(j+n,i+n);
            if(dist[i+n]+sdist+dist[j]>d) add_edge(i+n,j+n),add_edge(j,i);
        }
    }
    for(int i=1;i<=a;i++){
        add_edge(hate[i].cowa,hate[i].cowb+n);
        add_edge(hate[i].cowb,hate[i].cowa+n);
        add_edge(hate[i].cowa+n,hate[i].cowb);
        add_edge(hate[i].cowb+n,hate[i].cowa);
    }
    for(int i=1;i<=b;i++){
        add_edge(like[i].cowa,like[i].cowb);
        add_edge(like[i].cowb,like[i].cowa);
        add_edge(like[i].cowa+n,like[i].cowb+n);
        add_edge(like[i].cowb+n,like[i].cowa+n);
    }
}
void dfs(int cur,int &k){
    used[cur] = 1;
    for(int i=0;i<G[cur].size();i++){
        int u = G[cur][i];
        if(!used[u]) dfs(u,k);
    }
    id[++k] = cur;
}
void rdfs(int cur,int k){
    used[cur] = 1;
    topoid[cur] = k;
    for(int i=0;i<rG[cur].size();i++){
        int u = rG[cur][i];
        if(!used[u]) rdfs(u,k);
    }
}
bool check(int mid){
    int cnt = 0; memset(used,0,sizeof(used));
    for(int i=1;i<=2*n;i++) if(!used[i]) dfs(i,cnt);
    int k = 0; memset(used,0,sizeof(used));
    for(int i=2*n;i>=1;i--) if(!used[id[i]]) rdfs(id[i],++k);
    for(int i=1;i<=n;i++) if(topoid[i]==topoid[i+n]) return false;
    return true;
}
int solve(){
    int l=0,r=10000000,mid,ans=-1;
    while(l<=r){
        mid = (l+r)>>1;
        build(mid);
        if(check(mid)){
            ans = mid;
            r = mid-1;
        }
        else l = mid+1;
    }
    return ans;
}
int main(){
    while(input()) printf("%d\n",solve());
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43710979/article/details/89445839
今日推荐