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 prob Lem preparation process.
There are n + 1 cities consecutively numbered from 0 to N. City 0 are Metropolis that's the meeting point for all jury mem Bers. For the 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 this k days each of the N jury members should is present in Metropolis to is able to work on problems.
You are know the flight schedule in the country (jury members consider themselves important enough to only use flights for TRA nsportation). 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 he arrival day and departure day jury the ' not ' able to discuss the Olympiad. All flights in Megapolia depart and arrive on the same day.
Gather everybody for K-days in the "a hard objective, doing" while spending the minimum possible En harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can wor K together for K-days and then send them to their home cities. The cost of the arrangement is defined as a total cost of tickets to all used flights. It is allowed for jury member to stay in Metropolis for more than K days.
Input
The "a" of input contains three integers n, m and K (1≤n≤10^5, 0≤m≤10^5, 1≤k≤10^6).
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≤10^6, 0≤fi≤n, 0≤ti≤n, 1≤ci≤10^6, exactly one of fi and Ti equals zero), the day of departure (and arr ival), the departure city, the arrival and the ticket.
Output
Output The only integer this is the minimum cost of gathering all jury to the 0 for K-days and then sending them Back to their home cities.
If It is impossible to gather everybody into Metropolis for K 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 6000
0 0 9000 2 9 0
7000
1 8 0 2
examples Output
24500
the
N members need to take part in a continuous K-day meeting, there are m flights, every flight has a corresponding fee, now we want to send this n members to the city of No. 0, after the meeting to send them back to the original city, for the smallest cost.
train of Thought
Suppose there is a flight sequence: [1,2,2,1,1,3,..., 3,2,1] [1,2,2,1,1,3,..., 3,2,1], where the number represents the city in which it took off/arrived, and the departure time of the flight is represented horizontally.
Obviously, it takes all of us to attend a meeting of K-K days, that is, we can choose 1,2 1,2 's departure time at random to minimize the total cost before 3 3 is present.
We make Res[i] res[i] represent the minimum cost of departure from the I-I city, A[i] a[i] represents the minimum cost that all MPs can reach as of the first day of I.
We know that the a[i] a[i is meaningless until all the city's required flights have appeared.
We can iterate through all the starting time by the above mentioned to calculate a a array.
Similarly, the return flight can also be handled in this way, and eventually get b b arrays.
By a,b the properties of the A,B array we can push back and forward the minimum cost of the journey/return of all the time.
Then only need to find the interval of K K ai+bi+k+1 a_i+b_{i+k+1} of the minimum, if not, output −1-1.
AC Code
#include <bits/stdc++.h> using namespace std;
inline int read () {int x=0,c=0,f=1; for (; c< ' 0 ' | | C> ' 9 ';
C=getchar ()) f=c!= '-';
for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) x=x*10+c-' 0 ';
return f?x:-x;
} template <typename t> inline bool Scan_d (T &ret) {char C;
int sgn; if (c = GetChar (), C = EOF) return 0;
EOF while (c!= '-' && (C < ' 0 ' | | c > ' 9 ')} {if ((c = GetChar ()) = EOF) return 0; } SGN = (c = = '-')?
-1:1; ret = (c = = '-')?
0: (C-' 0 ');
while (c = GetChar (), C >= ' 0 ' && C <= ' 9 ') ret = ret * + (C-' 0 ');
RET *= SGN;
return 1;
} template<typename t> void print (T x) {static char s[33], *s1;
S1 = s;
if (!x) *s1++ = ' 0 ';
if (x < 0) Putchar ('-'), x =-X;
while (x) *s1++ = (x + ' 0 '), x/= 10;
while (S1--! = s) putchar (*S1);
} template<typename t> void println (T x) {print (x);
Putchar (' \ n '); typedef __int64 LL;
const int MAXN = 1E5+10;
Const LL inf = __int64_max__;
struct node {int d,f,t,c;
BOOL operator< (const node &x) {return d<x.d;
}} EDGE[MAXN];
int n,m,k;
LL A[MAXN*10];
LL B[MAXN*10];
LL RES[MAXN];
LL D[MAXN];
int MAXR;
void Tode () {LL ans=inf;
for (int i=maxr; i>=1; i--) {if (!b[i)) b[i]=b[i+1];
else if (b[i+1]) b[i]=min (b[i],b[i+1]);
for (int i=1; i<=maxr; i++) {if (!a[i)) a[i]=a[i-1];
else if (A[i-1]) a[i]=min (a[i],a[i-1]);
for (int i=1; i<=maxr-k-1; i++) if (a[i]&&b[i+k+1)) Ans=min (ans,a[i]+b[i+k+1));
println ((ans!=inf?ans:-1));
} void Solve () {LL now = inf;
int cnt = n;
for (int i=1; i<=m; i++)//departure {int nod = EDGE[I].D;
if (EDGE[I].F) {if (!res[edge[i].f]) {res[edge[i].f]=edge[i].c; if (!) ( --CNT))//cnt = 0 means all cities have already appeared {now=0;
Computes a now represents the current cost for (int i=1; i<=n; i++) now+=res[i];
A[nod]=now; } else if (RES[EDGE[I].F]>EDGE[I].C) {if (!cnt)//test knot at current position
The minimum cost of the tail is {NOW-=RES[EDGE[I].F];
RES[EDGE[I].F]=EDGE[I].C;
NOW+=EDGE[I].C;
A[nod]=now; else res[edge[i].f]=edge[i].c;
Update minimum Cost}}} Now=inf,cnt=n;
for (int i=m; i>=1; i--)//return {int nod=edge[i].d;
if (edge[i].t) {if (!d[edge[i].t]) {d[edge[i].t]=edge[i].c; if (!) (
--cnt)) {now=0;
for (int i=1; i<=n; i++) Now+=d[i];
B[nod]=now;
} else if (D[EDGE[I].T]>EDGE[I].C) {if (!cnt) {
NOW-=D[EDGE[I].T];
D[EDGE[I].T]=EDGE[I].C;
NOW+=EDGE[I].C;
B[nod]=now;
else d[edge[i].t]=edge[i].c;;
}} tode ();
int main () {Scan_d (n);
Scan_d (m);
Scan_d (k);
for (int i=1; i<=m; i++) {scan_d (EDGE[I].D);
Scan_d (EDGE[I].F);
Scan_d (EDGE[I].T);
Scan_d (EDGE[I].C);
MAXR = max (MAXR,EDGE[I].D); Sort (edge+1,edge+m+1);
Sort solve by Time ();
return 0; }