Jury Meeting CodeForces

来源:互联网 发布:pop3默认端口号 编辑:程序博客网 时间:2024/06/05 11:52

Country of Metropolia is holding Olympiad ofMetrpolises soon. It mean that all jury members of the olympiad should meettogether in Metropolis (the capital of the country) for the problem preparationprocess.

There are n + 1 citiesconsecutively numbered from 0 to n. City 0 isMetropolis that is the meeting point for all jury members. For each cityfrom 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 beable to work on problems.

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

Gather everybody for k days in the capital is a hard objective, doing thatwhile spending the minimum possible money is even harder. Nevertheless, yourtask is to arrange the cheapest way to bring all of the jury members toMetrpolis, 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 usedflights. It is allowed for jury member to stay in Metropolis for morethan k days.

Input

The first line of input contains threeintegers nm 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 difiti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ 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 minimumcost of gathering all jury members in city 0 for k days and then sending them back to their homecities.

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

Example

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 inMetropolis in the first sample test is to use flights that take place ondays 1, 2, 8 and 9.The only alternative option is to send jury member from second city back homeon day 15, that would cost 2500 more.

In the second sample it is impossible to sendjury member from city 2 backhome from Metropolis.

 

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>usingnamespacestd;longlong num,tot,d,x,y,z,p,n,m,k,t,dl[4000000+10],dr[4000000+10],l[4000000+10],r[4000000+10];struct node{    longlong d,t,c;}a[100000+10],b[100000+10];boolcmp(node a,node b){    return a.d<b.d;}boolcmp2(node a,node b){    return a.d>b.d;}intmain(){// freopen("x.in","r",stdin);// freopen("x.out","w",stdout);    scanf("%lld%lld%lld",&n,&m,&k);    memset(dl,-1,sizeof(dl));    memset(dr,-1,sizeof(dr));    memset(l,-1,sizeof(l));    memset(r,-1,sizeof(r));    longlong l1=0;    tot=0;    num=0;    longlong l2=0;    for(int i=1;i<=m;++i)    {        scanf("%lld%lld%lld%lld",&d,&x,&y,&p);        if(y==0)        {            a[++l1].d=d;            a[l1].c=p;            a[l1].t=x;        }        if(x==0)        {            b[++l2].d=d;            b[l2].c=p;            b[l2].t=y;        }    }    sort(a+1,a+1+l1,cmp);    sort(b+1,b+1+l2,cmp2);    for(int i=1;i<=l1;++i)    {        if(dl[a[i].t]==-1)        {            num=num+1;            dl[a[i].t]=a[i].c;            tot=tot+a[i].c;            if(num==n)            l[a[i].d]=tot;        }         if(dl[a[i].t]>a[i].c)        {            tot=tot+a[i].c-dl[a[i].t];            dl[a[i].t]=a[i].c;            if(num==n)            l[a[i].d]=tot;        }    }    for(int i=1;i<=a[l1].d;++i)        if(l[i]!=-1&&l[i+1]==-1)            l[i+1]=l[i];    tot=0;num=0;    for(int i=1;i<=l2;++i)    {   if(dr[b[i].t]==-1)        {            num=num+1;            dr[b[i].t]=b[i].c;            tot=tot+b[i].c;            if(num==n)            r[b[i].d]=tot;        }         if(dr[b[i].t]>b[i].c)        {            tot=tot+b[i].c-dr[b[i].t];            dr[b[i].t]=b[i].c;            if(num==n)            r[b[i].d]=tot;        }    }    for(int i=b[1].d;i>=1;--i)        if(r[i-1]==-1&&r[i]!=-1)            r[i-1]=r[i];    longlong ans=11731173111173111;    for(int i=1;i<=a[l1].d;++i)        if (ans>l[i]+r[i+k+1]&&l[i]!=-1&&r[i+k+1]!=-1)        ans=l[i]+r[i+k+1];    if (ans==11731173111173111)        printf("-1\n");    else        printf("%lld\n",ans);  //  fclose(stdin);   // fclose(stdout);    return0;}


题意大概是有n个人从1到n号城市要坐飞机去0号城市开会,且飞机的目的地和终点一定有一个是0,全部人到达以后在0处至少呆K天,k+1天后可以走(也可以不走),求最小花费。

思路:

一开始没看到目的地终点一定有个是0,还以为要弄一个路径会随时间推移的最短路径。。。。。有毒

主要是思路是数组l[i]表示,第i天所有人都到0城市的费用,r[i]表示所有人在i这天开完会准备回家的最小费用。

求l的问题分解为求每个人到0城市的问题,比如l[3]就代表所有人在第三天到0城市集结完毕的最小费用(注意可能有人第一天就到了,有人第二天就到了,这个三指的是所有人在第三天集结完毕的费用)。

而求r的问题也类比l的方法,r[4]代表第四天大家就可以回家的最小费用,(注意有人可以第五天回,有人可以第六天回去)

构造l,r的方法就是每个读进来,然后判断是否该点出现过没有,如果没有num=num+1(当num=n时刻代表有一个可行的l诞生了,)如果该点出现过,判断是否比以前的花费要少,要少则更新总花费。)

值得一提的是这样并不能使每个l,r都有他们应有的值,

例如l[4]=3;此时l[5]=-1(初值);但是我们想到如果在第4天集结完毕,顺推到第5天也是成立的

所以当l[i]=-1&&l[i-1]!=-1时我们要更新l[i]=l[i-1]的值。

r同理。

再o(n)扫一遍就可以求出ans了。

时间复杂度应该是快排的O(nlongn),(其他都是在O(m)及以下完成的)

(想想如果排序用桶排,岂不是O(k)内解决。。。。。。)

阅读全文
0 0
原创粉丝点击