首页 > 代码库 > UVA 1146 Now or later
UVA 1146 Now or later
二分时间+2sat
边加多了....RE了好久......
Nowor later
[Submit] [Go Back] [Status] Description As you must have experienced, instead of landing immediately, an aircraft sometimes waits in a holding loop close to the runway. This holding mechanism is required by air traffic controllers to space apart aircraft as much as possible on the runway (while keeping delays low). It is formally defined as a ``holding pattern‘‘ and is a predetermined maneuver designed to keep an aircraft within a specified airspace (see Figure 1 for an example). Figure 1: A simple Holding Pattern as described in a pilot text book. Jim Tarjan, an air-traffic controller, has asked his brother Robert to help him to improve the behavior of the airport. Input The input file, that contains all the relevant data, contains several test cases Each test case is described in the following way. The first line contains the number n of aircraft ( 2n2000). This line is followed by n lines. Each of these lines contains two integers, which represent the early landing time and the late landing time of an aircraft. Note that all times t are such that 0t107. Output For each input case, your program has to write a line that conttains the maximal security gap between consecutive landings. Sample Input 10 44 156 153 182 48 109 160 201 55 186 54 207 55 165 17 58 132 160 87 197 Sample Output 10
|
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=4010; struct Edge { int to,next; }edge[maxn*maxn*4]; int Adj[maxn],Size; void init() { Size=0; memset(Adj,-1,sizeof(Adj)); } void Add_Edge(int u,int v) { edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++; } int Low[maxn],DFN[maxn],Instack[maxn],Belong[maxn]; int Index,Stack[maxn],top,scc; void Tarjan(int u) { DFN[u]=Low[u]=++Index; Stack[top++]=u; Instack[u]=1; int v; for(int i=Adj[u];~i;i=edge[i].next) { v=edge[i].to; if(!DFN[v]) { Tarjan(v); Low[u]=min(Low[v],Low[u]); } else if(Instack[v]) { Low[u]=min(Low[u],DFN[v]); } } if(DFN[u]==Low[u]) { scc++; do { v=Stack[--top]; Instack[v]=0; Belong[v]=scc; }while(v!=u); } } bool ck(int n) { memset(DFN,0,sizeof(DFN)); memset(Instack,0,sizeof(Instack)); Index=scc=top=0; for(int i=0;i<n;i++) { if(!DFN[i]) Tarjan(i); } for(int i=0;i<n;i+=2) { if(Belong[i]==Belong[i^1]) return false; } return true; } int air[maxn][2],n; bool test(int mid) { init(); for(int i=0;i<n;i++) { for(int k1=0;k1<2;k1++) { for(int j=i+1;j<n;j++) { for(int k2=0;k2<2;k2++) { if(abs(air[i][k1]-air[j][k2])<mid) { Add_Edge(i*2+k1,j*2+1-k2); Add_Edge(j*2+k2,i*2+1-k1); } } } } } return ck(n*2); } int main() { while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { scanf("%d%d",&air[i][0],&air[i][1]); } int ans=-1,low=0,high=1e7+10,mid; while(low<=high) { mid=(low+high)/2; if(test(mid)) ans=mid,low=mid+1; else high=mid-1; } printf("%d\n",ans); } return 0; }