Codeforces 853B - Jury Meeting 【贪心】


B. Jury Meeting


time limit per test  1second

memory limit per test      512megabytes


Country of Metropolia is holding Olympiad of Metrpolises soon.It mean that all jury members of the olympiad should meet together inMetropolis (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 meetingpoint for all jury members. For each city from 1 to n there is exactly one jury memberliving there. Olympiad preparation is a long and demanding process thatrequires k days of work. For all of these k days each of the n jury members should be present inMetropolis to be able to work on problems.

You know the flight schedule in the country (jury membersconsider 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 alwaystakes off at the same day it arrives. On his arrival day and departure day jurymember is not able to discuss the olympiad. All flights in Megapolia depart andarrive at the same day.

Gather everybody for k days in the capital is a hardobjective, 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 jurymembers to Metrpolis, so that they can work together for k days and then send them back to theirhome cities. Cost of the arrangement is defined as a total cost of tickets forall used flights. It is allowed for jury member to stay in Metropolis for morethan k days.

Input

The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106).

The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day ofdeparture (and arrival), the departure city, the arrival city and the ticketcost.

Output

Output the only integer that is the minimum cost of gatheringall 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 theirhome 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 firstsample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is tosend 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个城市是人们需要聚集的地方。还有m条航班,每条航班从0到其他城市或从其他城市到0,当天即可抵达,现在需要选定一个时间段,长度为k天,使得这一个时间段里人们都在0城市工作(航班抵达和离开0城市的那一天不能进行工作),问把n个人送到0城市,工作完成后再送回去的最小花费。


【思路】


我们利用贪心的思维,我们去枚举长度为k的区间[l,r],使得所有人能在第l天之前抵达0城市,第r天之后离开0城市。


那么我们需要做的便是算出能使n个人在第i天及以前能够全部到达0城市的最小花费ss[i],以及在第i天及以后能够全部离开0城市的最小花费tt[i],最后扫一遍即可。


具体细节见代码。


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))

typedef long long ll;
const int maxn = 1000005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int n,m,k;
int dis[maxn];
ll ss[maxn],tt[maxn];

struct node
{
    int d,u,v,c;
}e[maxn];

bool cmp(const node &a,const node &b)
{
    return a.d<b.d;
}

int main()
{
    int Max=0;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d%d%d",&e[i].d,&e[i].u,&e[i].v,&e[i].c);
        Max=max(Max,e[i].d);
    }
    sort(e+1,e+1+m,cmp);                   //根据航班时间排序
    ll sum;
    int cnt=n;
    for(int i=1;i<=m;i++)                  //由于是抵达时间,我们从前往后枚举
    {
        int now=e[i].d;
        if(e[i].u)
        {
            if(!dis[e[i].u])
            {
                dis[e[i].u]=e[i].c;
                cnt--;
                if(!cnt)                  //在这个时间所有人都可以抵达,故计算ss[i]
                {
                    sum=0;
                    for(int i=1;i<=n;i++)
                        sum+=dis[i];
                    ss[now]=sum;
                }
            }
            else if(dis[e[i].u]>e[i].c)    //更新最小值
            {
                if(!cnt)
                {
                    sum-=(dis[e[i].u]-e[i].c);
                    dis[e[i].u]=e[i].c;
                    ss[now]=sum;
                }
                else dis[e[i].u]=e[i].c;
            }
        }
    }
    if(cnt!=0)                     //不是所有城市的人都能到达0城市
    {
        puts("-1");
        return 0;
    }
    mst(dis,0);
    cnt=n;
    for(int i=m;i>=1;i--)          //由于是抵达时间,我们从后往前枚举
    {
        int now=e[i].d;
        if(e[i].v)
        {
            if(!dis[e[i].v])
            {
                dis[e[i].v]=e[i].c;
                cnt--;
                if(!cnt)
                {
                    sum=0;
                    for(int i=1;i<=n;i++)
                        sum+=dis[i];
                    tt[now]=sum;
                }
            }
            else if(dis[e[i].v]>e[i].c)
            {
                if(!cnt)
                {
                    sum-=(dis[e[i].v]-e[i].c);
                    dis[e[i].v]=e[i].c;
                    tt[now]=sum;
                }
                else dis[e[i].v]=e[i].c;
            }
        }
    }
    if(cnt!=0)
    {
        puts("-1");
        return 0;
    }
    for(int i=1;i<=Max;i++)                 //前缀的思想
    {
        if(!ss[i])  ss[i]=ss[i-1];
        else if(ss[i-1]) ss[i]=min(ss[i],ss[i-1]);
    }
    for(int i=Max;i>=1;i--)
    {
        if(!tt[i])  tt[i]=tt[i+1];
        else if(tt[i+1]) tt[i]=min(tt[i],tt[i+1]);
    }
    ll ans=1e18;
    for(int i=1;i<=Max-k-1;i++)
    {
        if(ss[i]&&tt[i+k+1])            //第i天前所有人都能到0城市且第i+k+1天后所有人都能离开0城市
            ans=min(ans,ss[i]+tt[i+k+1]);
    }
    if(ans!=1e18) printf("%I64d\n",ans);
    else puts("-1");
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值