新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> We choose to study algorithmic problems,  not because they are easy,  but because they are hard.
    [返回] 计算机科学论坛计算机理论与工程『 算法理论与分析 』 → 求解"加油问题" 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7352 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 求解"加油问题" 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     北纬26度 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:0
      积分:52
      门派:XML.ORG.CN
      注册:2006/10/24

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给北纬26度发送一个短消息 把北纬26度加入好友 查看北纬26度的个人资料 搜索北纬26度在『 算法理论与分析 』的所有贴子 引用回复这个贴子 回复这个贴子 查看北纬26度的博客楼主
    发贴心情 求解"加油问题"

    假定你要在I-80公路上驱车从旧金山到纽约市.你的汽车装有C加仑汽油,可以行驶m英里.I-80公路上n个加油站机器所售汽油的价格的一览表,设di为第i个加油站距离旧金山的距离,ci是在第i个加油站加油的开销.假设对于任意两个加油站i和j,它们之间的距离di-dj为的m倍数.你从加油站1出发时,油箱为空.你的最终目的地为加油站n.在你到达目的地n时,油箱油料至少为0.试设计一多项式时间动态规划算法,输出穿过这段公路所需的最小汽油量.并根据n和C,分析你所设计的算法的运算时间.
      需注意的是,当油箱中的汽油量小于0时,汽车不能行驶;当你决定在某站加油时,也不必加满油箱.

       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/10/24 22:15:00
     
     vingo555 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:11
      积分:105
      门派:XML.ORG.CN
      注册:2006/10/11

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给vingo555发送一个短消息 把vingo555加入好友 查看vingo555的个人资料 搜索vingo555在『 算法理论与分析 』的所有贴子 引用回复这个贴子 回复这个贴子 查看vingo555的博客2
    发贴心情 
    没有调试过。

    #define N (10)  //set the total station
    #define OIL_PER_KM (1)
    #define INIT_OIL (5)
    #define MAX_CONTAINER (100)
    struct station
    {
     float dist;
     float price;
    };


    float curr;
    struct station st[N];

    void init_station()
    {
     //int i;
     curr = INIT_OIL;

     //initilize the station information
     st[0].dist = 0;
     st[N].price = MAX_LONG;
     //otherinformation should be filled according to the actually.
    }
    // 0 -- for increment always
    // 1 == exist decrement,but the low state's prices is higher the the from state
    // -1 -- the last station
    int check_state(int from,int to,int &ret)
    {
     int i ;
     int retValue = 0;
     if(from == to) {
      return -1;
     }
     for(i = from ; i < to ; i ++) {
      if(st[i].price > st[i + 1].price) {
       if(st[i + 1].price > st[from].price) {
        retValue = 1;   
       } else {
        retValue = 2;    
       }
       ret = i + 1;
       break;
      }
     }

     return retValue;
    }
    float get_oli(int from,int to)
    {
     int i;
     float total = 0.0;
     if(from == to) {
      return total;
     }
     for(i =from ; i <= to ; i ++) {
      total += st[i].dist;
     }

     return (total * OIL_PER_KM);
    }
    float try_search()
    {
     int i ,j, retValue,newIndex;
     float tmp, cost = 0;
      do{
      curr -= st[i].dist * OIL_PER_KM;

      if(st[i].price >= st[i + 1].price) {   
       tmp = get_oli(i,i+1);
       cost += (tmp - curr) * st[i].price;
       curr = tmp;
       i ++;
      } else {
       for(j = i + 1; j < N - 1; j ++) {
        tmp = MAX_CONTAINER;
        tmp -= st[j].dist * OIL_PER_KM;

        if(tmp < 0) {
         break;
        }
       }
       //this is the station we can reach,if the
       j --;
       retValue = check_state(i,j,newIndex);
       if(retValue == 0) {    
        cost += (MAX_CONTAINER - curr) * st[i].price;
        curr = MAX_CONTAINER ;
        i ++;
       } else if(retValue == 1) {
        tmp = get_oli(i,newIndex);
        cost += (tmp - curr) * st[i].price;
        curr = tmp;
        i = newIndex;
       } else {
        tmp = ( st[i +1].dist * OIL_PER_KM - curr);
        if(tmp > 0 ) {
         cost += tmp * st[i].price;
        }
        i ++;
       }
      }
     }while(i < N -1);
      return cost;
    }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/10/31 12:25:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 算法理论与分析 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/15 15:23:52

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    328.125ms