首页 > 技术文章 > 【预处理】Codeforces Round #433 - 853B. Jury Meeting

doub7e 2017-09-07 10:31 原文

Jury Meeting
 

Source

http://codeforces.com/contest/853/problem/B

Description

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

 

Solution

题意:有分别来自城市1...n的n个人要聚集在城市0连续至少k天召开会议(刚到达和要返程的那两天不能算进去)。有m张机票,描述的是在di天从城市fi到城市ti的花费是ci,现在问最小的花费是多少。

思路:预处理出一个数组l[i],代表在第l天或之前所有人都能到达城市0的最小花费,再求出一个数组r[i],代表在第r天或之后所有人都能回家的最小花费。然后就可以枚举连续k天的起始时刻,有起始时刻自然就知道结束时刻,这样就可以O(1)的得到一个花费,记录最小花费即可得到答案。注意代码中将1e12视为INF是因为一张机票最贵1e6,选择所有机票也不可能超过1e12,并且计算l[]和r[]时也不会爆long long。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct Node{
 5     int day,from,to,cost;
 6     bool operator < (const Node & x) const{
 7         return day < x.day;
 8     }
 9 }f[100010];
10 
11 int n,m,k;
12 long long cost[100010],l[1000100],r[1000100];
13 
14 int main(){
15     cin >> n >> m >> k;
16     for(int i = 1;i <= m;++i)
17         scanf("%d%d%d%d",&f[i].day,&f[i].from,&f[i].to,&f[i].cost);
18     sort(f+1,f+1+m);
19     for(int i = 0;i <= n;++i) cost[i] = 1e12;
20     for(int i = 0;i <= 1000001;++i) l[i] = cost[1] * n;
21     int pre = 0;
22     for(int i = 1;i <= m;++i){
23         if(f[i].from == 0) continue;
24         if(f[i].cost < cost[f[i].from]){
25             l[f[i].day] = l[pre] - cost[f[i].from] + f[i].cost;
26             cost[f[i].from] = f[i].cost;
27         }
28         else l[f[i].day] = l[pre];
29         pre = f[i].day;
30     }
31     for(int i = 1;i <= 1000001;++i)
32         if(l[i] == cost[0] * n) l[i] = l[i-1];
33 
34 
35     for(int i = 0;i <= n;++i) cost[i] = 1e12;
36     for(int i = 0;i <= 1000001;++i) r[i] = cost[1] * n;
37     pre = 1000001;
38     for(int i = m;i >= 1;--i){
39         if(f[i].to == 0) continue;
40         if(f[i].cost < cost[f[i].to]){
41             r[f[i].day] = r[pre] - cost[f[i].to] + f[i].cost;
42             cost[f[i].to] = f[i].cost;
43         }
44         else r[f[i].day] = r[pre];
45         pre = f[i].day;
46     }
47     for(int i = 1000000;i >= 1;--i)
48         if(r[i] == cost[0] * n) r[i] = r[i+1];
49 
50     long long ans = 1e18;
51     for(int i = 1;i <= 1000000 - k - 1;++i){
52         if(l[i] < 1e12 && r[i+k+1] < 1e12) ans = min(ans,l[i]+r[i+k+1]);
53     }
54     if(ans != 1e18) cout << ans;
55     else cout << -1;
56 
57     return 0;
58 }

 

推荐阅读