zoukankan      html  css  js  c++  java
  • bzoj1577 [USACO09FEB] Fair Shuttle

    题目大意:n个站点,有m群奶牛,第i群奶牛有mi只,要从si站点出发,直到ti站点下车。

    对于一群奶牛,可以不全部上车。同时在车上的奶牛数不能超过c,求最多能满足多少头奶牛的要求。

    注意到可以不全部上车,那么贪心的思路就比较明确了

    尽量让下车下得早的多上车,这样利益可以最大化

    某些细节还是挺磨人的

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<vector>
     6 #include<set>
     7 #define be *S.begin()
     8 #define ed *S.rbegin()
     9 #define vv G[i][j].first
    10 #define ww G[i][j].second
    11 using namespace std;
    12 const int N=50010;
    13 int k,n,c;
    14 vector<pair<int,int> > G[N];
    15 set<int> S;
    16 int t[N];
    17 int main(){
    18     #ifndef ONLINE_JUDGE
    19         freopen("fs.in","r",stdin);freopen("fs.out","w",stdout);
    20     #endif
    21     scanf("%d%d%d",&k,&n,&c);
    22     while(k--){
    23         int u,v,w;
    24         scanf("%d%d%d",&u,&v,&w);
    25         G[u].push_back(make_pair(v,w));
    26     }
    27     for(int i=1;i<=n;++i)sort(G[i].begin(),G[i].end());
    28     int ans=0,in_car=0;
    29     for(int i=1;i<=n;++i){
    30         int j=0,j_end=G[i].size();
    31         if(!S.empty()&&be==i){
    32             ans+=t[be];in_car-=t[be];
    33             S.erase(be);
    34         }//calc the number of the satisfy cow
    35         for(;j<j_end&&in_car<c;++j){
    36             int go_into=min(c-in_car,ww);
    37             t[vv]+=go_into;in_car+=go_into;ww-=go_into;
    38             S.insert(vv);
    39             if(in_car==c)break;
    40         }//get on the red bus or blablabla,if the bus can be filled,fill it
    41         for(;j<j_end&&vv<ed;++j)
    42             while(!S.empty()&&ww){
    43                 int go_into=min(ww,t[ed]);
    44                 t[ed]-=go_into,t[vv]+=go_into;
    45                 S.insert(vv);
    46                 if(!t[ed])S.erase(ed);
    47                 ww-=go_into;
    48             }//sacrifice others' advantage to get the best advantage
    49     }
    50     printf("%d
    ",ans);
    51     return 0;
    52 }
  • 相关阅读:
    让你的网站在移动端健步如飞
    select元素javascript常用操作 转
    网站CSS写在html里面的好处
    Javascript 严格模式详解
    mac下网页中文字体优化
    js作用域相关知识总结
    【待填坑】 undefined和not defined的区别
    【待填坑】js构造函数和内置对象的区别
    echarts入门1【柱状图/饼图】
    echarts在miniUI和ajax下动态渲染数据
  • 原文地址:https://www.cnblogs.com/ndqzhang1111/p/6881703.html
Copyright © 2011-2022 走看看