码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-08 10:05:08      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:upper   logs   cost   nes   ber   als   lines   spec   strong   

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 ≤ 1050 ≤ m ≤ 1051 ≤ 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 ≤ 1060 ≤ fi ≤ n,0 ≤ 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.

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

解法:

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

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

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int MAXN=100005;
 5 struct node{
 6     int day , s , t , cost;
 7     bool operator <(const node &r) const{
 8         return day < r.day;
 9     }
10 }e[MAXN] , g[MAXN];
11 int vis[MAXN];
12 ll ct[10 * MAXN] , ans = -1;
13 int main(){
14     int d , s , t , c , n , m , k , p1 = 0 , p2 = 0;
15     scanf("%d%d%d",&n , &m , &k);
16     memset(ct , -1 , sizeof(ct));
17     for(int i = 0 ; i < m ; i ++){
18         scanf("%d%d%d%d",&d,&s,&t,&c);
19         if(s == 0 && t == 0) continue;
20         if(t == 0){
21             e[p1].day = d , e[p1].s = s , e[p1].t = t , e[p1].cost = c;
22             p1 ++;
23         }else if(s == 0){
24             g[p2].day = d , g[p2].s = s , g[p2].t = t , g[p2].cost = c;
25             p2 ++;
26         }
27     }
28     sort(e , e + p1);
29 
30     sort(g , g + p2);
31     int sum = 0;
32     ll play = 0 , res = 0;
33     for(int i = 0 ; i < p1 ; i ++){
34         d = e[i].day , s = e[i].s , t = e[i].t , c = e[i].cost;
35         if(vis[s] == 0){
36             sum ++;
37             vis[s] = c;
38             play += c;
39         }else{
40             if(c < vis[s]){
41                 play -= vis[s] - c;
42                 vis[s] = c;
43             }
44         }
45         if(sum == n){
46             ct[d] = play;
47         }
48     }
49     for(int i = 1 ; i < MAXN * 10 ; i ++){
50         if(ct[i - 1] != -1){
51             if(ct[i] == -1) ct[i] = ct[i - 1];
52         }
53     }
54     memset(vis , 0 , sizeof(vis));
55     sum = 0 , play = 0;
56     for(int i = p2 - 1; i >= 0 ; i --){
57         d = g[i].day , s = g[i].s , t = g[i].t , c = g[i].cost;
58         if(d - k - 1 < 0 || ct[d - k - 1] == -1) break;
59         if(vis[t] == 0){
60             sum ++;
61             vis[t] = c;
62             play += c;
63         }else{
64             if(c < vis[t]){
65                 play -= vis[t] - c;
66                 vis[t] = c;
67             }
68         }
69         if(sum == n){
70             if(ans == -1 || play + ct[d - k - 1] < ans) ans = play + ct[d - k - 1];
71         }
72     }
73     printf("%lld\n",ans);
74 }

 

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

标签:upper   logs   cost   nes   ber   als   lines   spec   strong   

原文地址:http://www.cnblogs.com/yinghualuowu/p/7492879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!