Codeforces 854 D. Jury Meeting(技巧)

网友投稿 274 2022-08-30

Codeforces 854 D. Jury Meeting(技巧)

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

Examples output

24500

题意

n 名议员需要参加一场持续 k 天的会议,有 m 次航班,每一次的航班都有相应的费用,现在我们要将这 n 名议员送到 0 号城市,参加完会议后送他们回原来的城市,求最小的花费。

思路

假设有航班序列: [1,2,2,1,1,3,...,3,2,1]

显然,要参加一场持续 k 天的会议需要所有人到场,即在 3 未出现之前我们可以随意选择 1,2

我们令 res[i] 代表从 i 城市出发所需的最小花费, a[i] 代表截至到第 i

我们知道,所有城市所需的航班未出现之前, a[i]

通过上面所说的我们可以正向遍历所有出发时间算得 a

同理,返程航班也可以这样处理,最终得到 b

通过 a,b

然后只需要找出间隔为 k 的 ai+bi+k+1 的最小值即可,若无,输出 −1

AC 代码

#includeusing 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 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 * 10 + (c - '0'); ret *= sgn; return 1;}templatevoid print(T x){ static char s[33], *s1; s1 = s; if (!x) *s1++ = '0'; if (x < 0) putchar('-'), x = -x; while(x) *s1++ = (x % 10 + '0'), x /= 10; while(s1-- != s) putchar(*s1);}templatevoid 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=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++) //出发 { 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 代表所有的城市都已经出现过了 { now=0; // 计算一个 now 代表当前花费 for(int i=1; i<=n; i++) now+=res[i]; a[nod]=now; } } else if(res[edge[i].f]>edge[i].c) { if(!cnt) // 试探以当前位置结尾的最小花费 { 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; //更新最小花费 } } } now=inf,cnt=n; for(int i=m; i>=1; i--) //返回 { 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); //按时间排序 solve(); return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:人类营销与AI营销,各有优势!(AI营销策略)
下一篇:51nod 1405 树的距离之和 (树形dp)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~