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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值