【題目大意】:有一架坐位固定的飛機,每天早上從1號點飛到N號點,晚上從N號點飛回上號點,中途有些點會有人上飛機,在保證不超載的情況下求一天下來,能載的最多乘客數。
【解題思路】:對於每一個起飛站點,儘可能的放入人,遇到放不下的情況就踢除掉最遠的人。枚舉站點並枚舉每個站點爲起點可到達的點進行人數的修改。
當人數超過規定值,則利用優先隊列的性質貪心去掉最遠的點。
【代碼】:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
struct Node{
int x,y,v;
Node (){}
Node (int _x,int _y,int _v){
x=_x,y=_y,v=_v;
}
const bool operator <(const Node &b)const{
return y<b.y;
}
};
int n,c,k;
vector<Node> a[10010],b[10010];
int aa[10010],bb[10010];
int solve_ns(){
priority_queue<Node> que;
int now=0,ans=0;
for(int i=1; i<=n; i++){
ans+=aa[i];
now-=aa[i];
for (int j=0; j<a[i].size(); j++){
now+=a[i][j].v;
que.push(a[i][j]);
}
while (now>c){
Node tmp=que.top();
que.pop();
if(now-c>=tmp.v){
now-=tmp.v;
aa[tmp.y]-=tmp.v;
}
else {
aa[tmp.y]-=(now-c);
tmp.v-=now-c;
que.push(tmp);
now=c;
}
}
}
return ans;
}
int solve_sn(){
priority_queue<Node> que;
int now=0,ans=0;
for(int i=1; i<=n; i++){
ans+=bb[i];
now-=bb[i];
for (int j=0; j<b[i].size(); j++){
now+=b[i][j].v;
que.push(b[i][j]);
}
while (now>c){
Node tmp=que.top();
que.pop();
if(now-c>=tmp.v){
now-=tmp.v;
bb[tmp.y]-=tmp.v;
}
else {
bb[tmp.y]-=(now-c);
tmp.v-=now-c;
que.push(tmp);
now=c;
}
}
}
return ans;
}
int main() {
while (~scanf("%d%d%d",&k,&n,&c)){
int u,v,w;
for(int i=1; i<=k; i++){
scanf("%d%d%d",&u,&v,&w);
if(v>u){
a[u].push_back(Node(u,v,w));
aa[v]+=w;
}
else {
b[n-u+1].push_back(Node(n-u+1,n-v+1,w));
bb[n-v+1]+=w;
}
}
int ans=0;
ans+=solve_ns();
ans+=solve_sn();
printf("%d\n",ans);
}
return 0;
}