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 kdays 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 ≤ 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 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.

题意:n名成员分别位于1->n的地方,现在送到0点开会,然后一起呆k天,送回来,现在给我们时刻表 需要天数 起点 终点 花费,问花费要最小

解法:

1 因为路上要时间,所以得考虑最后一个到是什么时候,然后之后的花费都是来的最小花费

2 再提前考虑最后一个走是什么时候,依然是最小花费,其中需要注意不存在的地方

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=;
struct node{
int day , s , t , cost;
bool operator <(const node &r) const{
return day < r.day;
}
}e[MAXN] , g[MAXN];
int vis[MAXN];
ll ct[ * MAXN] , ans = -;
int main(){
int d , s , t , c , n , m , k , p1 = , p2 = ;
scanf("%d%d%d",&n , &m , &k);
memset(ct , - , sizeof(ct));
for(int i = ; i < m ; i ++){
scanf("%d%d%d%d",&d,&s,&t,&c);
if(s == && t == ) continue;
if(t == ){
e[p1].day = d , e[p1].s = s , e[p1].t = t , e[p1].cost = c;
p1 ++;
}else if(s == ){
g[p2].day = d , g[p2].s = s , g[p2].t = t , g[p2].cost = c;
p2 ++;
}
}
sort(e , e + p1); sort(g , g + p2);
int sum = ;
ll play = , res = ;
for(int i = ; i < p1 ; i ++){
d = e[i].day , s = e[i].s , t = e[i].t , c = e[i].cost;
if(vis[s] == ){
sum ++;
vis[s] = c;
play += c;
}else{
if(c < vis[s]){
play -= vis[s] - c;
vis[s] = c;
}
}
if(sum == n){
ct[d] = play;
}
}
for(int i = ; i < MAXN * ; i ++){
if(ct[i - ] != -){
if(ct[i] == -) ct[i] = ct[i - ];
}
}
memset(vis , , sizeof(vis));
sum = , play = ;
for(int i = p2 - ; i >= ; i --){
d = g[i].day , s = g[i].s , t = g[i].t , c = g[i].cost;
if(d - k - < || ct[d - k - ] == -) break;
if(vis[t] == ){
sum ++;
vis[t] = c;
play += c;
}else{
if(c < vis[t]){
play -= vis[t] - c;
vis[t] = c;
}
}
if(sum == n){
if(ans == - || play + ct[d - k - ] < ans) ans = play + ct[d - k - ];
}
}
printf("%lld\n",ans);
}

Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) D的更多相关文章

  1. Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises)

    A. Fraction 题目链接:http://codeforces.com/contest/854/problem/A 题目意思:给出一个数n,求两个数a+b=n,且a/b不可约分,如果存在多组满足 ...

  2. Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) D. Jury Meeting(双指针模拟)

    D. Jury Meeting time limit per test 1 second memory limit per test 512 megabytes input standard inpu ...

  3. Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) C

    Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...

  4. Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) B

    Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartme ...

  5. Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) A

    Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned tha ...

  6. Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) D mt19937

    https://codeforces.com/contest/1040/problem/D 用法 mt19937 g(种子); //种子:time(0) mt19937_64 g(); //long ...

  7. 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) B】Shashlik Cooking

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 翻转一次最多影响2k+1个地方. 如果n<=k+1 那么放在1的位置就ok.因为能覆盖1..k+1 如果n<=2k+1 ...

  8. 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) A】Palindrome Dance

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] i从1..n/2循环一波. 保证a[i]和a[n-i+1]就好. 如果都是2的话填上min(a,b)*2就好 其他情况跟随非2的. ...

  9. Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)

    A. Even Subset Sum Problem 题意 给出一串数,找到其中的一些数使得他们的和为偶数 题解 水题,找到一个偶数或者两个奇数就好了 代码 #include<iostream& ...

随机推荐

  1. HTML5 SVG实现过山车动画

    HTML5 SVG实现过山车动画是一款jQuery特效很酷的HTML5 SVG动画,这款HTML5动画是过山车效果,主要是利用了SVG的path动画来实现的,效果非常酷. http://www.hui ...

  2. ThinkPHP基础(1)

    多层MVC模式 M:Model 数据模型层,负责数据操作 V:View 视图层,负责显示视图 C:Controller 控制器,实现业务逻辑 控制器访问及路由解析 通过url地址get参数找到指定的控 ...

  3. nodejs && apidoc

    1. 安装nodejs      http://www.nodejs.org      源码编译      configure —prefix=/usr/local/nodejs      make ...

  4. MySQL5.6中新增特性、不推荐使用的功能以及废弃的功能

    虽然已经使用MySQL5.6版本有一段时间了,但由于没有和之前的版本作过详细比较,所以对于哪些重要的或者不太重要的特性是在新版本中引入的,还有哪些特性已经或者将要从旧版本中移除的并没有一个十分全面的了 ...

  5. 洛谷 1131 [ZJOI2007]时态同步——树形dp

    题目:https://www.luogu.org/problemnew/show/P1131 因为越高,调节一个影响到的越多,所以底下只要把子树间的差异消除了就行了,与其他部分的差异由更高的边调节. ...

  6. poj2356Find a multiple——鸽巢定理运用

    题目:http://poj.org/problem?id=2356 N个数,利用鸽巢定理可知应有N+1个前缀和(包括0),因此其%N的余数一定有重复: 同余的两个前缀和之差一定为N的倍数,据此得出答案 ...

  7. 如何将Eclipse中编写的java项目导出?

    转自:https://zhidao.baidu.com/question/347808396.html1.导入项目 当下载了包含Eclipse 项目的源代码文件后,我们可以把它导入到当前的Eclips ...

  8. OpenCPN介绍及编译

    OpenCPN介绍及编译 OpenCPN是一个航海应用软件系统,采用wxWidgets界面框架,支持OpenGL,可以跨平台运行在Windows , Linux , Mac电脑上. OpenCPN是一 ...

  9. 第3章 编写ROS程序-1

    1.创建工作区和功能包 在我们写任何程序之前,第一步是创建一个容纳我们的功能包的工作区,然后再创建功能包本身. 创建工作区  使用标准的mkdir命令行去创建一个目录,我们将把这个新的目录称作工作区目 ...

  10. 35.Docker安装Mysql挂载Host Volume

    连个文件系统有块区域Area,我们要做的是把两个Area做文件映射 jesse腾讯云上有个linux的环境,版本比较老了 简书的地址: https://www.jianshu.com/p/b3bf64 ...