Codeforces Round #433 D. Jury Meeting

Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.

There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.

You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.

Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.

Input

The first line of input contains three integers nm and k (1 ≤ n ≤ 1050 ≤ m ≤ 1051 ≤ k ≤ 106).

The i-th of the following m lines contains the description of the i-th flight defined by four integers difiti and ci (1 ≤ di ≤ 1060 ≤ fi ≤ n0 ≤ ti ≤ n1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.

Output

Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.

If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).

Examples
input
2 6 5
1 1 0 5000
3 2 0 5500
2 2 0 6000
15 0 2 9000
9 0 1 7000
8 0 2 6500
output
24500
input
2 4 5
1 2 0 5000
2 1 0 4500
2 1 0 3000
8 0 1 6000
output
-1
Note

The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 128 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.

In the second sample it is impossible to send jury member from city 2 back home from Metropolis.


題意:有n+1個城市,從1~n號城市每個城市有一個評委要到0號城市和其他n-1個評委一起工作k天(就是說n個評委要同時在0號城市待上k天),並且要把所有評委都送回他原來所在的城市。然後有m班航班,只有從i號城市到0號城市,或者從0號城市到i號城市,問你最少需要花費多少錢。


我們可以處理出來這n個評委從各自的城市都到了0號城市的所有情況,只需從頭到尾掃一遍就行了,當n個評委都在0號城市的時候,就可以記錄一下最後一個評委到的時間,和總花費。

同樣可以處理出來他們回去的情況(從尾到頭掃一遍)。

我們把到0號城市的過程中最晚到的一個評委的時間叫做去的過程中的結束時間,把回各自城市過程中最早走的一個評委的時間叫做回的開始時間:

然後我們只需枚舉評委去0號城市的所有情況,那麼我們只需找到回去過程中開始時間比去的結束時間+k要晚的情況就行了,同時記錄最小值。

然後就是怎麼求比去的結束時間+k要晚的回去情況的最小值了,記錄一下比去的結束時間+k後面的最小值就行了,就是求一波後綴的最小值。

然後我是二分後面的那個值,其實只用把所有後綴的最小值都記錄下來就不用二分了。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;


struct flight
{
    int d,f,t,c;
};

bool cmp(flight a,flight b)
{
    if(a.d == b.d)
        return a.c < b.c;
    return a.d < b.d;
}

int n,m,k;
flight a[100010];
int f[100010];

struct node
{
    int d;
    LL c;
};
bool cmp2(node a,node b)
{
    if(a.d == b.d)
        return a.c < b.c;
    return a.d < b.d;
}
int c1,c2;
node x1[100010],x2[100010];

int binarysearch(int x)
{
    int l = 0,r = c2 - 1;
    int ans = -1;
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(x2[mid].d > x)
        {
            r = mid - 1;
            ans = mid;
        }
        else
            l = mid + 1;
    }
    return ans;

}
int main(void)
{
    int i,j;
    while(scanf("%d%d%d",&n,&m,&k)==3)
    {
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d%d",&a[i].d,&a[i].f,&a[i].t,&a[i].c);
        }
        sort(a+1,a+1+m,cmp);
        memset(f,0,sizeof(f));
        int cnt = 0;
        LL sum = 0;
        c1 = c2 = 0;
        int last = 0;
        for(i=1;i<=m;i++)
        {
            if(a[i].f == 0)
                continue;
            if(f[a[i].f] == 0)
            {
                f[a[i].f] = a[i].c;
                sum += a[i].c;
                cnt++;
                last = a[i].d;
            }
            else
            {
                if(a[i].c < f[a[i].f])
                {
                    sum -= f[a[i].f] - a[i].c;
                    f[a[i].f] = a[i].c;
                    last = a[i].d;
                }
            }
            if(cnt == n)
            {
                x1[c1].d = last;
                x1[c1].c = sum;
                c1++;
            }
        }
        sum = 0;
        last = n;
        cnt = 0;
        memset(f,0,sizeof(f));
        for(i=m;i>=1;i--)
        {
            if(a[i].t == 0)
                continue;
            if(f[a[i].t] == 0)
            {
                f[a[i].t] = a[i].c;
                sum += a[i].c;
                cnt++;
                last = a[i].d;
            }
            else
            {
                if(a[i].c < f[a[i].t])
                {
                    sum -= f[a[i].t] - a[i].c;
                    f[a[i].t] = a[i].c;
                    last = a[i].d;
                }
            }
            if(cnt == n)
            {
                x2[c2].d = last;
                x2[c2].c = sum;
                c2++;
            }
        }
        sort(x2,x2+c2,cmp2);
        for(i=c2-1;i>=1;i--)
        {
            x2[i-1].c = min(x2[i-1].c,x2[i].c);
        }
        LL ans = -1;
        for(i=0;i<c1;i++)
        {
            int p = binarysearch(x1[i].d+k);
            if(p == -1)
                continue;
            if(ans == -1)
                ans = x1[i].c + x2[p].c;
            else
                ans = min(ans,x1[i].c+x2[p].c);
        }
        printf("%I64d\n",ans);
    }

    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章