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



題目大意:有n個人要去0城市參加會議,有m架航班從n個位置到0或者從0到n,現在要求n個人都到0城市後開k天的會回去,飛機當天不算在k天開會之間,要求算出最小花費

解題思路:這是一個貪心,但是D題一般是很難做的,這道題首先我們按照航班起飛時間對飛機進行排序,然後對於去0城市飛機統計第i天是否能夠全部到達,然後倒着貪心回去的飛機,然後遍歷飛機去0城市的第i天的最小花費,和回去的最小花費,之後求和的最小值

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;

struct point
{
	int d,s,e,c;
}a[1000005];
LL s[1000005],e[1000005],ans,sum;
int i,n,m,k,j,flag,x,MAX;
int check[1000005];
bool cmp(point x,point y){return x.d<y.d;}

int main()
{
	cin>>n>>m>>k;
	MAX =0;
	for(i=1;i<=m;i++)
	{
		cin>>a[i].d>>a[i].s>>a[i].e>>a[i].c;
		MAX=max(MAX,a[i].d);
	}
	sort(a+1,a+1+m,cmp);
	flag=n;
	for(i=1;i<=m;i++)
	{
		if(a[i].s)
		{
			if(check[a[i].s]==0)
			{
				check[a[i].s]=a[i].c;
				flag--;
				if(!flag)//說明截止到當前,他們都能飛到0城市
				{
					for(j=1;j<=n;j++)
						s[a[i].d]+=check[j];
					ans=s[a[i].d];
				}
			}
			else if(a[i].c<check[a[i].s])
			{
				ans-=check[a[i].s]-a[i].c;
				check[a[i].s]=a[i].c;
				if(!flag)//若已經都能飛回去後,出現當前飛機更便宜,在之前的基礎上sum,更新花費sum
				{
					s[a[i].d]=ans;
				}
			}
		}
	}
	if(flag)
	{
		cout<<-1<<endl;
		return 0;
	}
	memset(check,0,sizeof(check));
	flag = n;
	for(i=m;i>=1;i--)
	{
		if(a[i].e)
		{
			if(check[a[i].e]==0)
			{
				check[a[i].e]=a[i].c;
				flag--;
				if(flag==0)
				{
					for(j=1;j<=n;j++)
						e[a[i].d]+=check[j];
					ans=e[a[i].d];
				}
			}
			else if(a[i].c<check[a[i].e])
			{
				ans-=check[a[i].e]-a[i].c;
				check[a[i].e]=a[i].c;
				if(flag==0)
				{
					e[a[i].d]=ans;
				}
			}
		}
	}
	if(flag!=0)
	{
		cout<<-1<<endl;
		return 0;
	}
	for(i=1;i<=MAX;i++)
	{
		if(s[i]==0)//如果當前i天無法判斷是否已經到了,那麼若是前一天i-1天可以判斷出來,就s[i]=s[i-1]
			s[i]=s[i-1];
		else if(s[i-1])
			s[i]=min(s[i],s[i-1]);
	}
	for(i=MAX;i>=1;i--)
	{
		if(e[i]==0)//同上
			e[i]=e[i+1];
		else if(e[i+1])
			e[i]=min(e[i],e[i+1]);
	}
	sum=1e18;
	for(i=1;i<=MAX-k-1;i++)
	{
		if(s[i]!=0&&e[i+k+1]!=0)
		{
			sum=min(sum,s[i]+e[i+k+1]);
		}
	}
	if(sum==1e18)
		cout<<-1<<endl;
	else
		cout<<sum<<endl;
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章