首页 > 代码库 > UVA 1146 Now or later

UVA 1146 Now or later


二分时间+2sat

边加多了....RE了好久......


Nowor later
Time Limit: 9000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

Description

Download as PDF

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.


\epsfbox{p3211.eps}

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 ( 2$ \le$n$ \le$2000). 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 0$ \le$t$ \le$107.

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


Note: The input file corresponds to Table 1.


Robert‘s Hints

Optimization vs. Decision
Robert advises you to work on the decision variant of the problem. It can then be stated as follows: Given an integer p, and an instance of the optimization problem, the question is to decide if there is a solution with security gap p or not. Note that, if you know how to solve the decision variant of an optimization problem, you can build a binary search algorithm to find the optimal solution.

On decision
Robert believes that the decision variant of the problem can be modeled as a very particular boolean satisfaction problem. Robert suggests to associate a boolean variable per aircraft stating whether the aircraft is early (variable takes value ``true‘‘) or late (value ``false‘‘). It should then be easy to see that for some aircraft to land at some time has consequences for the landing times of other aircraft. For instance in Table 1 and with a delay of 10, if aircraft A1 lands early, then aircraft A3 has to land late. And of course, if aircraft A3 lands early, then aircraft A1 has to land late. That is, aircraft A1 and A3 cannot both land early and formula (A1 $ \Rightarrow$ ?A3$ \wedge$ (A3 $ \Rightarrow$ ?A1) must hold.


And now comes Robert‘s big insight: our problem has a solution, if and only if we have no contradiction. A contradiction being something like Ai $ \Leftrightarrow$ ?Ai.



#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;
}