2013
12-23

# HDU 1815 Building roads-图论-[解题报告] C++

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

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

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.

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

53246

2—SAT：如果a与b矛盾，建边a—>b‘；

stdio.h>
#include<stack>
#include<string.h>
#define N 2000
#define M 1000
using namespace std;
struct edage
{
int ed;
int next;
}E[M*M];
struct op
{
int x,y;
}P[N],P0,P1;
int n,low[N],dfs[N],ins[N],map[N*2],idx,ans,first[N],rfirst[N],belong[N],num,rnum,len;
stack<int>Q;
{
E[num].ed=y;
E[num].next=first[x];
first[x]=num++;
}
int dis(op a,op b)
{
return abs(a.x-b.x)+abs(a.y-b.y);
}
void Tarjan(int x)
{
int p,v;
low[x]=dfs[x]=idx++;
ins[x]=1;
Q.push(x);
for(p=first[x];p!=-1;p=E[p].next)
{
v=E[p].ed;
if(dfs[v]==-1)
{
Tarjan(v);
low[x]=low[x]>low[v]?low[v]:low[x];
}
else if(ins[v]==1)
{
low[x]=low[x]>dfs[v]?dfs[v]:low[x];
}
}
if(dfs[x]==low[x])
{
do
{
v=Q.top();
Q.pop();
ins[v]=0;
belong[v]=ans;
}while(v!=x);
ans++;
}
}
int sum=0;
int judge(int d,int min,int max)
{
int i,j;
memcpy(first,rfirst,sizeof(first));
num=rnum;
for(i=1;i<=n;i++)//枚举两点之间的距离
for(j=i+1;j<=n;j++)
{
if(map[i]+map[j]>d)//连接相同同中转站不符合
{
}
if(map[i]+map[j+n]+len>d)//连接不同的中转站不符合
{
}
if(map[i+n]+map[j]+len>d)//连接不同的中转站不符合
{
}
if(map[i+n]+map[j+n]>d)//连接相同同中转站不符合
{
}
}
memset(low,0,sizeof(low));
memset(ins,0,sizeof(ins));
memset(dfs,-1,sizeof(dfs));
memset(belong,0,sizeof(belong));
idx=ans=1;
for(i=1;i<=n*2;i++)
{
if(dfs[i]==-1)
Tarjan(i);
}
for(i=1;i<=n;i++)
if(belong[i]==belong[i+n])
return 0;
return 1;
}
int main()
{
int i,m,k,min,max,mid,flag,x,y,mmax;
while(scanf("%d%d%d",&n,&m,&k)!=-1)
{
memset(first,-1,sizeof(first));
num=0;
scanf("%d%d%d%d",&P0.x,&P0.y,&P1.x,&P1.y);
for(i=1;i<=n;i++)
scanf("%d%d",&P[i].x,&P[i].y);
min=999999999;
max=-1;
for(i=1;i<=n;i++)
{
map[i]=dis(P[i],P0);
map[i+n]=dis(P[i],P1);
if(min>map[i])
min=map[i];
if(min>map[i+n])
min=map[i+n];
if(max<map[i])
max=map[i];
if(max<map[i+n])
max=map[i+n];
}
len=dis(P0,P1);
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
}
for(i=0;i<k;i++)
{
scanf("%d%d",&x,&y);
}
memcpy(rfirst,first,sizeof(first));
rnum=num;
min=min*2,max=max*2+len;
mmax=-1;flag=0;
while(min<=max)
{
mid=(min+max)/2;
if(judge(mid,min,max))
{
max=mid-1;
mmax=mid;
}
else min=mid+1;
}
printf("%d\n",mmax);
}
return 0;
}

1. /*
* =====================================================================================
*
* Filename: 1366.cc
*
* Description:
*
* Version: 1.0
* Created: 2014年01月06日 14时52分14秒
* Revision: none
* Compiler: gcc
*
* Author: Wenxian Ni (Hello World~), [email protected]
* Organization: AMS/ICT
*
* =====================================================================================
*/

#include
#include

using namespace std;

int main()
{
stack st;
int n,i,j;
int test;
int a[100001];
int b[100001];
while(cin>>n)
{
for(i=1;i>a[i];
for(i=1;i>b[i];
//st.clear();
while(!st.empty())
st.pop();
i = 1;
j = 1;

while(in)
break;
}
while(!st.empty()&&st.top()==b[j])
{
st.pop();
j++;
}
}
if(st.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

2. 我没看懂题目
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
第二个是7 0 6 -1 1 -6 7输出是14 7 7
不知道题目例子是怎么得出来的