您的位置:首页 > 其它

codeforces 853B 思维 差分

2017-11-18 20:34 375 查看
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 n, m 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 di, fi, ti 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).

Example

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)个城市,其中第1~n个城市每个城市有一个人,第0个城市是人们需要聚集的地方。还有m条航班,每条航班从0到其他城市或从其他城市到0,当天即可抵达,现在需要选定一个时间段,长度为k天,使得这一个时间段里人们都在0城市工作(航班抵达和离开0城市的那一天不能进行工作),问把n个人送到0城市,工作完成后再送回去的最小花费。

分析:找出共同能呆在这个城市的可能的时间的并集,然后找最优的答案,例如 是l,r,那么枚举l和r间的区间,找到
4000
在[1,l-1] [r+1,1e6] 内所有人来或者回去的最小话费,这里可以用到差分一下,就是从1开始维护一个arrive花费的最小值,遇到更小的时间,减去旧的话费,加上新的花费,就可以得到前面所有最小值的加和,反过来也一样

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<int,int> > g[100010],b[100010];
const ll inf = 1e13;
ll du[1000010],du1[1000010];

int main()
{
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=m;i++)
{
int d,f,t,c;
scanf("%d%d%d%d",&d,&f,&t,&c);
if(t==0) g[f].push_back(make_pair(d,c));
if(f==0) b[t].push_back(make_pair(d,c));
}
int l=-1,r=10000000;
for(int i=1;i<=n;i++)
{
sort(g[i].begin(),g[i].end());
sort(b[i].begin(),b[i].end());
reverse(b[i].begin(),b[i].end());
if(g[i].size()) l=max(l,g[i][0].first+1);
if(b[i].size()) r=min(b[i][0].first-1,r);
if(g[i].size()==0||b[i].size()==0)
{
printf("-1\n");
return 0;
}
}
if(r-l+1<k||l==-1||r==10000000)
{
printf("-1\n");
return 0;
}
for(int i=1;i<=n;i++)
{
ll cost=inf;
du[1]+=cost;
for(int j=0;j<g[i].size();j++)
{
if(g[i][j].second<cost)
{
du[g[i][j].first]-=cost;
du[g[i][j].first]+=g[i][j].second;
cost=g[i][j].second;
}
}

cost=inf;
du1[1000000]+=cost;
for(int j=0;j<b[i].size();j++)
{
if(b[i][j].second<cost)
{
du1[b[i][j].first]-=cost;
du1[b[i][j].first]+=b[i][j].second;
cost=b[i][j].second;
}
}
}
for(int i=2;i<=(int)1e6;i++)
du[i]+=du[i-1];
for(int i=1000000-1;i>=1;i--)
du1[i]+=du1[i+1];
// for(int i=1;i<=10;i++)
//  cout<<du1[i]<<" ";
// cout<<endl;
ll ans=inf;
for(int i=l;i+k-1<=r;i++)
ans=min(ans,du[i-1]+du1[i+k]);
cout<<ans<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: