贪心。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<utility>
#include<bitset>
#include<complex>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define REP(i,a,b) for(int i=a; i<=b; ++i)
#define FOR(i,a,b) for(int i=a; i<b; ++i)
#define MP make_pair
typedef long long LL;
typedef pair<int,int> pii;
const int maxn = 1e5;
const int maxd = 1e6;
struct Node {
int day, val, wi;
Node(): day(0), val(0), wi(0) {}
Node(int _day, int _val, int _wi) {
day = _day, val = _val, wi = _wi;
}
bool operator<(const Node &a) const {
return day < a.day;
}
};
int n, m, kk;
vector<Node> in, out;
LL f1[maxd + 5], f2[maxd + 5];
int dist[maxn + 5];
int main() {
scanf("%d%d%d", &n, &m, &kk);
for(int i = 1; i <= m; ++i) {
int d, f, t, c;
scanf("%d%d%d%d", &d, &f, &t, &c);
if(t == 0)
in.push_back( Node(d,f,c) );
else out.push_back( Node(d,t,c) );
}
sort(in.begin(), in.end());
sort(out.begin(), out.end());
int cnt = n, pt = 0;
LL sum = 0;
for(int i = 1; i <= maxd; ++i) {
while(pt < in.size() && in[pt].day <= i) {
if(dist[in[pt].val] == 0) {
--cnt;
dist[in[pt].val] = in[pt].wi;
sum += in[pt].wi;
}
else if(dist[in[pt].val] > in[pt].wi) {
sum = sum - dist[in[pt].val] + in[pt].wi;
dist[in[pt].val] = in[pt].wi;
}
++pt;
}
if(cnt == 0) f1[i] = sum;
}
cnt = n, pt = out.size() - 1, sum = 0;
mem(dist, 0);
for(int i = maxd; i >= 1; --i) {
while(pt >= 0 && out[pt].day >= i) {
if(dist[out[pt].val] == 0) {
--cnt;
dist[out[pt].val] = out[pt].wi;
sum += out[pt].wi;
}
else if(dist[out[pt].val] > out[pt].wi) {
sum = sum - dist[out[pt].val] + out[pt].wi;
dist[out[pt].val] = out[pt].wi;
}
--pt;
}
if(cnt == 0) f2[i] = sum;
}
LL ans = -1;
for(int i = 1, k = 1 + kk + 1; k <= maxd; ++i, ++k) {
if(f1[i] > 0 && f2[k] > 0) {
if(ans == -1) ans = f1[i] + f2[k];
else ans = min(ans, f1[i] + f2[k]);
}
}
printf("%lld\n", ans);
return 0;
}
我的代码:
#include<bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a));
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define MP make_pair
#define ULL unsigned long long
#define LL long long
#define inf 0x3f3f3f3f
#define md ((ll+rr)>>1)
#define ls (i<<1)
#define rs (ls|1)
#define eps 1e-8
#define ree freopen("in.txt","r",stdin);
#define bug pf("----------------");
#define N 1000010
#define mod 475
#define double long double
#define pii pair<int,int>
#define MP make_pair
#define lson ll,md,ls
#define rson md+1,rr,rs
#define B 250
#define pii pair<int,int>
//2017年09月08日13:42:44
LL m1[N],m2[N];
int vis[N];
vector<pii>in[N],out[N];
int last[N];
int main(){
//ree
int n,m,k;
sf("%d%d%d",&n,&m,&k);
int ma=0;
rep(i,1,m){
int d,u,v,c;sf("%d%d%d%d",&d,&u,&v,&c);
if(v==0)in[d].push_back(MP(u,c));
else out[d].push_back(MP(v,c));
ma=max(ma,d);
}
mem(m1,inf);mem(m2,inf);
mem(vis,0);
int cnt=n;
//pf("%d\n",ma);
LL tol=0;
for(int i=1;i<=ma;++i){
int len=in[i].size();
for(int j=0;j<len;++j){
int v=in[i][j].first;
//pf("%d\n",v);
if(!vis[v]){vis[v]=1;cnt--;tol+=in[i][j].second;last[v]=in[i][j].second;}
else if(vis[v]&&last[v]>in[i][j].second){
tol-=last[v];tol+=in[i][j].second;
last[v]=in[i][j].second;
}
}
if(cnt==0)m1[i]=tol;
}
cnt=n;mem(vis,0);tol=0; mem(last,0);
for(int i=ma;i>=1;--i){
int len=out[i].size();
for(int j=0;j<len;++j){
int v=out[i][j].first;
//pf("%d\n",v);
if(!vis[v]){vis[v]=1;cnt--,tol+=out[i][j].second;last[v]=out[i][j].second;}
else if(vis[v]&&last[v]>out[i][j].second){
tol-=last[v];tol+=out[i][j].second;
last[v]=out[i][j].second;
}
}
if(cnt==0)m2[i]=tol;
}
//rep(i,1,ma){
//cout<<m1[i]<<' '<<m2[i]<<'\n';
//}
LL ans=100LL*inf;
//cout<<ans<<'\n';
for(int i=1;i+k+1<=ma;++i){
ans=min(ans,1LL*m1[i]+1LL*m2[i+k+1]);
}
if(ans>=100LL*inf)puts("-1");
else cout<<ans<<'\n';
}