poj 2749
Building roads
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6091 | Accepted: 2046 |
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|.
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].
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
二分答案走2 - sat
View Code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <stack> 7 8 using namespace std; 9 10 const int MAX_N = 505; 11 int N,M,A,B; 12 bool fri[MAX_N][MAX_N],hate[MAX_N][MAX_N]; 13 int low[MAX_N * 2],pre[MAX_N * 2],cmp[MAX_N * 2]; 14 int dfs_clock,scc_cnt; 15 int x[MAX_N],y[MAX_N]; 16 stack<int> S; 17 vector<int> G[2 * MAX_N]; 18 19 void dfs(int u) { 20 low[u] = pre[u] = ++dfs_clock; 21 S.push(u); 22 for(int i = 0; i < G[u].size(); ++i) { 23 int v = G[u][i]; 24 if(!pre[v]) { 25 dfs(v); 26 low[u] = min(low[u],low[v]); 27 } else if(!cmp[v]) { 28 low[u] = min(low[u],pre[v]); 29 } 30 } 31 32 if(low[u] == pre[u]) { 33 ++scc_cnt; 34 for(;;) { 35 int x = S.top(); S.pop(); 36 cmp[x] = scc_cnt; 37 if(x == u) break; 38 } 39 } 40 } 41 42 bool scc() { 43 dfs_clock = scc_cnt = 0; 44 memset(cmp,0,sizeof(cmp)); 45 memset(pre,0,sizeof(pre)); 46 47 for(int i = 2; i <= 2 * N + 1; ++i) if(!pre[i]) dfs(i); 48 49 for(int i = 2; i <= N + 1; ++i) { 50 if(cmp[i] == cmp[N + i]) return false; 51 } 52 return true; 53 } 54 55 int dis(int i,int j) { 56 return abs(x[i] - x[j]) + abs(y[i] - y[j]); 57 } 58 59 void build(int x) { 60 for(int i = 2; i <= 2 * N + 1; ++i) { 61 G[i].clear(); 62 } 63 64 for(int i = 2; i <= N + 1; ++i) { 65 for(int j = i + 1; j <= N + 1; ++j) { 66 if(fri[i][j]) { 67 G[i].push_back(j); 68 G[i + N].push_back(j + N); 69 G[j].push_back(i); 70 G[j + N].push_back(i + N); 71 } 72 if(hate[i][j]) { 73 G[i].push_back(j + N); 74 G[j].push_back(i + N); 75 G[j + N].push_back(i); 76 G[i + N].push_back(j); 77 } 78 79 if(dis(i,0) + dis(j,0) > x) { 80 G[i].push_back(j + N); 81 G[j].push_back(i + N); 82 } 83 if(dis(i,1) + dis(j,1) > x) { 84 G[i + N].push_back(j); 85 G[j + N].push_back(i); 86 } 87 if(dis(i,1) + dis(j,0) + dis(0,1) > x) { 88 G[i + N].push_back(j + N); 89 G[j].push_back(i); 90 } 91 if(dis(i,0) + dis(j,1) + dis(0,1)> x) { 92 G[i].push_back(j); 93 G[j + N].push_back(i + N); 94 } 95 } 96 } 97 98 } 99 void solve() { 100 int l = 0,r = 12e6 + 7; 101 102 //printf("r = %d\n",r); 103 104 while(l < r) { 105 int mid = (l + r) / 2; 106 build(mid); 107 if(scc()) r = mid; 108 else l = mid + 1; 109 } 110 build(l); 111 if(scc()) 112 printf("%d\n",l); 113 else 114 printf("-1\n"); 115 } 116 117 int main() 118 { 119 //freopen("sw.in","r",stdin); 120 scanf("%d%d%d",&N,&A,&B); 121 for(int i = 0; i <= N + 1; ++i) { 122 scanf("%d%d",&x[i],&y[i]); 123 } 124 125 for(int i = 1; i <= A; ++i) { 126 int a,b; 127 scanf("%d%d",&a,&b); 128 hate[a + 1][b + 1] = 1; 129 } 130 131 for(int i = 1; i <= B; ++i) { 132 int a,b; 133 scanf("%d%d",&a,&b); 134 fri[a + 1][b + 1] = 1; 135 } 136 137 solve(); 138 139 return 0; 140 }