poj 3080 Flying Right(贪心+优先队列)

来源:互联网 发布:蚁群算法解决tsp问题 编辑:程序博客网 时间:2024/06/06 19:22

【题目大意】:有一架坐位固定的飞机,每天早上从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 longstruct 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;}


原创粉丝点击