1. 程式人生 > >Codeforces853B. Jury Meeting

Codeforces853B. Jury Meeting

傳送門

預處理出所有人在第i天之前到達0所需要的最小花費sumT0[]
以及 所有人在第j天后回去所需要的最小花費sumF0[]

列舉開會時間i
ans=min(sumT0[i1]+sumF0[i+k]

#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))
using namespace std; const ll inf=1e18; //const int INF = inf; const int N = 1e5 + 5; const int C = 1e6+5; struct P{ int d,t,c; }; vector<P>to0,fr0; bool cmp1(const P&a,const P&b){ return a.d<b.d; } bool cmp2(const P&a,const P&b){ return a.d>b.d; } ll sumF0[C],sumT0[C]; bool
vis[N]; ll cost[N]; void updateSum(ll*sum,vector<P>&vec,int n){ fill(vis,vis+n+1,0); fill(cost,cost+n+1,inf); fill(sum,sum+C,inf); int num=0; ll tmp=0; for(int i=0;i<vec.size();++i){ P&p=vec[i]; if(!vis[p.t]){ ++num; vis[p.t]=1
; tmp+=p.c; } else{ tmp=min(tmp,tmp-cost[p.t]+p.c); } if(num==n){ sum[p.d]=min(sum[p.d],tmp); } cost[p.t]=min(cost[p.t],(ll)p.c); } } ll slove(int n,int k){ sort(to0.begin(),to0.end(),cmp1); sort(fr0.begin(),fr0.end(),cmp2); updateSum(sumF0,fr0,n); updateSum(sumT0,to0,n); for(int i=2;i<C;++i){ sumT0[i]=min(sumT0[i-1],sumT0[i]); } for(int i=C-2;i>=1;--i){ sumF0[i]=min(sumF0[i+1],sumF0[i]); } ll ans=inf; for(int i=2;i+k<C;++i){ ans=min(sumT0[i-1]+sumF0[i+k],ans); } return ans>=inf?-1:ans; } int main(){ //freopen("/home/lu/code/r.txt","r",stdin); //freopen("/home/lu/code/w.txt","w",stdout); int n,m,k; while(~scanf("%d%d%d",&n,&m,&k)){ int d,f,t,c; to0.clear(); fr0.clear(); while(m--){ scanf("%d%d%d%d",&d,&f,&t,&c); if(f==0){ fr0.push_back({d,t,c}); } else{ to0.push_back({d,f,c}); } } printf("%lld\n",slove(n,k)); } return 0; }