Codeforces Round #433 D. Jury Meeting

来源:互联网 发布:淘宝店铺导航条背景色 编辑:程序博客网 时间:2024/05/19 18:12

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 51 1 0 50003 2 0 55002 2 0 600015 0 2 90009 0 1 70008 0 2 6500
output
24500
input
2 4 51 2 0 50002 1 0 45002 1 0 30008 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 longusing 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;}