Codeforces Round #433 (Div. 2) Jury Meeting(贪心)

来源:互联网 发布:蓝月传奇光翼升阶数据 编辑:程序博客网 时间:2024/05/24 06:10
D. 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 kdays 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 51 1 0 50003 2 0 55002 2 0 600015 0 2 90009 0 1 70008 0 2 6500
output
24500
input
2 4 51 2 0 50002 1 0 45002 1 0 30008 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 128 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.


题意:给你m个航班,然后使得n个特派员在0号城市开会,最少要让这n个特派员一起在这里待上k天,然后每个航班的航线、时间和花费已知,问你满足题意的最小花费。如果不满足,则输出-1

题解:直接贪心即可,正着找去0号城市的最小花费,倒着找回去的最下花费,然后相加即可。

#include<set>  #include<map>         #include<stack>                #include<queue>                #include<vector>        #include<string>      #include<math.h>       #include<stdio.h>                #include<iostream>                #include<string.h>                #include<stdlib.h>        #include<algorithm>       #include<functional>        using namespace std;typedef __int64 ll;#define inf (ll)1e17      #define mod 1000000007                 #define maxn  2100005    #define PI 3.1415926  #define lowbit(x) (x&-x)     #define eps 1e-9   struct node{ll d, f, t, c;}a[maxn];bool comp(node a, node b){return a.d < b.d;}ll b[maxn], dp[maxn], dp1[maxn];int main(void){ll n, k, m, i, j;scanf("%I64d%I64d%I64d", &n, &m, &k);for (i = 0;i <= 2100000;i++)b[i] = -1, dp[i] = inf;for (i = 1;i <= m;i++)scanf("%I64d%I64d%I64d%I64d", &a[i].d, &a[i].f, &a[i].t, &a[i].c);sort(a + 1, a + m + 1, comp);ll num = 0, sum = 0;for (i = 1;i <= m;i++){  if (a[i].f == 0)continue;if (b[a[i].f] == -1)num++, b[a[i].f] = a[i].c, sum += a[i].c;else{if (b[a[i].f] > a[i].c){sum -= b[a[i].f];b[a[i].f] = a[i].c;sum += a[i].c;}}if (num == n)dp[a[i].d] = sum;}num = 0;sum = 0;for (i = 0;i <= 2100000;i++)b[i] = -1, dp1[i] = inf;for (i = m;i > 0;i--){if (a[i].t == 0)continue;if (b[a[i].t] == -1)num++, b[a[i].t] = a[i].c, sum += a[i].c;else{if (b[a[i].t] > a[i].c){sum -= b[a[i].t];b[a[i].t] = a[i].c;sum += a[i].c;}if (num == n)dp1[a[i].d] = sum;}if (num == n)dp1[a[i].d] = sum;}for (i = 1;i <= 1000000;i++)if (dp[i] != inf)break;j = i;for (i = j + 1;i <= 1000000;i++)dp[i] = min(dp[i - 1], dp[i]);for (i = 1000000;i > 0;i--)if (dp1[i] != inf)break;j = i;for (i = j - 1;i > 0;i--)dp1[i] = min(dp1[i], dp1[i + 1]);ll ans = inf;for (i = 1;i <= 1000000;i++){if (dp[i] == inf || dp1[i + k + 1] == inf)continue;ans = min(dp[i] + dp1[i + k + 1], ans);}if (ans == inf)printf("-1\n");elseprintf("%I64d\n", ans);return 0;}


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