Question:
There are N cowshed... there is no link between them .. now we are going to establish a transfer station in two places .. each cowshed builds a path to a transfer station... some cows are hostile to each other .. cannot be connected to the same transfer station .. some cows are friendly to each other .. it must be in the same Transfer Station... please find a path construction solution .. the shortest distance between cowshed... note .. the cowshed can only go through the transfer station .. there is a path between the two transfer stations... the path length is the distance between the two points ..
Question:
The longest side of the second... then let's figure out... if an edge is greater than the maximum value of the current enumeration... so we need to make the two cowshed not to be connected like this... other Hostile and friendly relationships are well established...
This question has plagued me for a long time ~~~ The reason is that the array is small !! The WA... RE returned by POJ is eaten ?..... I am struggling for a long time, isn't it an algorithm problem ....
Program:
#include<iostream>#include<stdio.h>#include<cmath>#include<queue>#include<stack>#include<string.h>#include<map>#include<set>#include<algorithm>#define oo 1000000007#define MAXN 2005<<1#define MAXM 500005<<1#define ll long longusing namespace std;struct node{ int y,next;}line[MAXM];int cow[MAXN][2],Hate[MAXN][2],Friend[MAXN][2],Lnum,_next[MAXN];int dfn[MAXN],low[MAXN],tp[MAXN],tpnum,DfsIndex;bool instack[MAXN];stack<int> mystack;void addline(int x,int y){ line[++Lnum].next=_next[x],_next[x]=Lnum,line[Lnum].y=y;}void tarjan(int x){ mystack.push(x),instack[x]=true; dfn[x]=low[x]=++DfsIndex; for (int k=_next[x];k;k=line[k].next) { int y=line[k].y; if (!dfn[y]) { tarjan(y); low[x]=min(low[x],low[y]); }else if (instack[y]) low[x]=min(low[x],dfn[y]); } if (low[x]==dfn[x]) { tpnum++; do { x=mystack.top(); mystack.pop(); instack[x]=false; tp[x]=tpnum; }while (low[x]!=dfn[x]); }}bool _2sat(int N,int A,int B,int Dis,int M){ int i,x,y; Lnum=0,memset(_next,0,sizeof(_next)); for (x=0;x<N;x++) for (y=x+1;y<N;y++) { if (cow[x][0]+cow[y][0]>M) addline(x<<1,y<<1|1),addline(y<<1,x<<1|1); if (cow[x][0]+cow[y][1]+Dis>M) addline(x<<1,y<<1),addline(y<<1|1,x<<1|1); if (cow[x][1]+cow[y][0]+Dis>M) addline(x<<1|1,y<<1|1),addline(y<<1,x<<1); if (cow[x][1]+cow[y][1]>M) addline(x<<1|1,y<<1),addline(y<<1|1,x<<1); } for (i=1;i<=A;i++) { x=Hate[i][0],y=Hate[i][1]; addline(x<<1,y<<1|1),addline(x<<1|1,y<<1); addline(y<<1,x<<1|1),addline(y<<1|1,x<<1); } for (i=1;i<=B;i++) { x=Friend[i][0],y=Friend[i][1]; addline(x<<1,y<<1),addline(x<<1|1,y<<1|1); addline(y<<1,x<<1),addline(y<<1|1,x<<1|1); } while (!mystack.empty()) mystack.pop(); memset(dfn,0,sizeof(dfn)); memset(instack,false,sizeof(instack)); DfsIndex=tpnum=0; for (i=0;i<(N<<1);i++) if (!dfn[i]) tarjan(i); for (i=0;i<N;i++) if (tp[i<<1]==tp[i<<1|1]) return false; return true; } int main(){ int N,A,B,s0x,s0y,s1x,s1y,Dis,i; while (~scanf("%d%d%d",&N,&A,&B)) { scanf("%d%d%d%d",&s0x,&s0y,&s1x,&s1y); Dis=abs(s0x-s1x)+abs(s0y-s1y); for (i=0;i<N;i++) { int x,y; scanf("%d%d",&x,&y); cow[i][0]=abs(x-s0x)+abs(y-s0y); cow[i][1]=abs(x-s1x)+abs(y-s1y); } for (i=1;i<=A;i++) { int x,y; scanf("%d%d",&x,&y),x--,y--; Hate[i][0]=x,Hate[i][1]=y; } for (i=1;i<=B;i++) { int x,y; scanf("%d%d",&x,&y),x--,y--; Friend[i][0]=x,Friend[i][1]=y; } int l=-1,r=oo,mid; while (r-l>1) { mid=l+r>>1; if (!_2sat(N,A,B,Dis,mid)) l=mid; else r=mid; } if (r==oo) printf("-1\n"); else printf("%d\n",r); } return 0;}