Jury Meeting Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises)(贪心,预处理?)

Jury Meeting
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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 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 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 1, 2, 8 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.

题目大意:懒得说了

题目思路:这几场cf div2 d题都挺有趣的。。。。不过这一场abc题真是简单如狗,d题想清楚也是蛮水的,显然我是没想清楚,主要是这个贪心策略没想好,剩下真的是简单如狗,,,对于这道题,我们按照时间大小排序(想出这里,剩下的就很简单了),然后处理出满足条件的前缀和后缀最小值,相加找出最小值即可

ac代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 1e5+5;
struct node{
    int d,f,t;
    LL c;
}E[maxn];
LL dp1[1000000+5];
LL dp2[1000000+5];
LL last[maxn];
int vis[maxn];
int n,m,k;
int cmp(node a,node b){
    return a.d<b.d;
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0;i<m;i++){
        scanf("%d%d%d%I64d",&E[i].d,&E[i].f,&E[i].t,&E[i].c);
    }
    sort(E,E+m,cmp);
    memset(vis,0,sizeof(vis));
    for(int i =0;i<=1000000+2;i++){
        dp1[i] = INF;
    }
    int num = 0;
    LL sum = 0;
    for(int i = 0;i<m;i++){
        if(E[i].f){
            if(vis[E[i].f]==0){
                num++;
                vis[E[i].f] = 1;
                last[E[i].f] = E[i].c;
                sum+=E[i].c;
            }
            else{
                if(E[i].c<last[E[i].f]){
                    sum-=last[E[i].f];
                    sum+=E[i].c;
                    last[E[i].f ] =E[i].c;
                }
            }
        }
        if(num==n){
             dp1[E[i].d] = min(sum,dp1[E[i].d]);
        }
    }
    memset(vis,0,sizeof(vis));
    for(int i = 0;i<=1000000+2;i++){
        dp2[i] = INF;
    }
    sum = 0;
    num = 0;
    for(int i = m-1;i>=0;i--){
         if(E[i].t){
            if(vis[E[i].t]==0){
                num++;
                vis[E[i].t] = 1;
                last[E[i].t] = E[i].c;
                sum+=E[i].c;
            }
            else{
                if(E[i].c<last[E[i].t]){
                    sum-=last[E[i].t];
                    sum+=E[i].c;
                    last[E[i].t ] =E[i].c;
                }
            }
        }
        if(num==n){
            dp2[E[i].d] = min(sum,dp2[E[i].d]);
       // cout<<dp2[E[i].d]<<' '<<i<<endl;
        }
    }
    LL ans = INF;
    for(int i = 1;i<=1000000;i++){
        dp1[i] = min(dp1[i],dp1[i-1]);
    }
    for(int i = 1000000;i>=1;i--){
        dp2[i] = min(dp2[i],dp2[i+1]);
    }
    for(int i = 1;i<=1000000;i++){
        if(i+k+1>1000000){
            break;
        }
        /*if(dp2[i+k+1]<INF)
        cout<<dp1[i]<<' '<<dp2[i+k+1]<<endl;*/
        ans = min(dp1[i]+dp2[i+k+1],ans);
    }
    if(ans>=INF) puts("-1");
    else printf("%I64d\n",ans);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值