您的位置:首页 > 产品设计 > UI/UE

HDU1815 Building roads (2-SAT)

2017-07-24 16:45 295 查看
Building roads
Time Limit : 10000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 23   Accepted Submission(s) : 8
[align=left]Problem Description[/align]
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|.

 

[align=left]Input[/align]
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].
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

4 1 1
12750 28546 15361 32055
6706 3887
10754 8166
12668 19380
15788 16059
3 4
2 3

 

Sample Output
53246

根据矛盾建图:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#define MAXN 1010
using namespace std;

typedef struct nodess
{
int x;
int y;
}nodess;
nodess s1,s2;
nodess barn[MAXN];
nodess hate[MAXN];
nodess love[MAXN];
int n,m1,m2;
int sta[MAXN],top;
int mark[MAXN]; //mark[x]标记x这个点有没有加入到匹配
vector<int> map[MAXN];
int dis1[MAXN],dis2[MAXN],dis_;

int addedge(int mid);
int ttdistance(nodess a,nodess b)
{
int ans;
ans = abs(a.x-b.x)+abs(a.y-b.y);
return ans;
}

//2-sat()每一次调用只进行一次搜索
int dfs(int x) //从x寻找下一个点,此时x假设已经被选中
{
if(mark[x^1])return 0; //如果他的兄弟已被选中过了,说明此时x不能选择,该次整条路的匹配是错误的
if(mark[x])return 1; //如果它已经被选择过了,则那说明下面一定匹配成功,无需再向下搜索
mark[x]=1; //若x,x^1与其兄弟都没被加入匹配过,则x加入,并向下搜索有没有路
sta[top++]=x; //用于记录这条路被标记的点,用于这后如果这条路搜索失败后,将该路上mark的标记删除
for(int i=0;i<map[x].size();i++)
{
if(!dfs(map[x][i])) return 0; //如果下面搜索失败的话(即搜索到了两个兄弟节点),说明整条路的搜索都是错误的
}
return 1;

}

int Twosat(int mid)
{
if(addedge(mid)==0)return 0;
memset(mark,0,sizeof(mark));
for(int i=0;i<n;i+=2)
{
if(mark[i]==0&&mark[i^1]==0) //枚举每一对尚未确定的Ai, Ai‘
{
top=0;
if(dfs(i)==0) //如果从i点出发找不到路,即匹配失败,则从他的兄弟点找i^1,进行一次搜索
{
while(top) mark[sta[--top]]=0; //将mark数组返回原来的状态
if(dfs(i^1)==0) return 0; //如果他的兄弟点也匹配失败,则无法匹配,因为Ai Ai^1这一对无法加入到已匹配队伍中
}
}
}
return 1;
}

int addedge(int mid)
{
int a,b;
for(int i=0;i<m1;i++)
{
a=hate[i].x*2;
b=hate[i].y*2;
map[a].push_back(b^1);
map[a^1].push_back(b);
map[b].push_back(a^1);
map[b^1].push_back(a);
}
for(int i=0;i<m2;i++)
{
a=love[i].x*2;
b=love[i].y*2;
map[a].push_back(b);
map[a^1].push_back(b^1);
map[b].push_back(a);
map[b^1].push_back(a^1);
}
for(int i=0;i<n;i++)
{
int flag=0;
a=i*2;
if(dis1[i]>mid) //不能和S1相连
{
map[a].push_back(a^1);
flag=1;
}
if(dis2[i]>mid) //不能和S2相连
{
if(flag) return 0;
map[a^1].push_back(a);
}
for(int j=i+1;j<n;j++)
{
b=j*2;
if(dis1[i]<=mid&&dis1[j]<=mid&&dis1[i]+dis1[j]>mid)
{
map[a].push_back(b^1);
map[b].push_back(a^1);
}
if(dis2[i]<=mid&&dis2[j]<=mid&&dis2[i]+dis2[j]>mid)
{
map[a^1].push_back(b);
map[b^1].push_back(a);
}
if(dis1[i]<=mid&&dis2[j]<=mid&&dis1[i]+dis_+dis2[j]>mid)
{
map[a].push_back(b);
map[b^1].push_back(a^1);
}
if(dis2[i]<=mid&&dis1[j]<=mid&&dis2[i]+dis_+dis1[j]>mid)
{
map[a^1].push_back(b^1);
map[b].push_back(a);
}
}
}
return 1;
}

int main()
{
int i,a,b,l,r,ans;
while(scanf("%d%d%d",&n,&m1,&m2)!=EOF)
{
scanf("%d%d%d%d",&s1.x,&s1.y,&s2.x,&s2.y);
for(i=0;i<n;i++)
{
scanf("%d%d",&barn[i].x,&barn[i].y);
}
for(i=0;i<m1;i++)
{
scanf("%d%d",&hate[i].x,&hate[i].y);
hate[i].x--;hate[i].y--;
}
for(i=0;i&
4000
lt;m2;i++)
{
scanf("%d%d",&love[i].x,&love[i].y);
love[i].x--;love[i].y--;
}
for(i=0;i<n;i++)
{
dis1[i]=ttdistance(barn[i],s1);
dis2[i]=ttdistance(barn[i],s2);
}
dis_=ttdistance(s1,s2);
l=0;
r=8000000;
ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
for(i=0;i<2*n;i++) map[i].clear();
if(Twosat(mid))
{
ans=mid;
r=mid-1;
}
else
{
l=mid+1;
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息