杂题_POJ上的过桥问题

2024-02-29 03:38
文章标签 问题 poj 杂题 过桥

本文主要是介绍杂题_POJ上的过桥问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文出自:http://blog.csdn.net/svitter


过桥问题解释:一条船可以坐两个人,但是有很多人要过河,所以送过一个人去,另一个人还要回来接。使所有人过河之后时间最短,如何求?

此问题曾作为阿里巴巴题目

初看此题想的太过简单,直接让跑的最快的送过去,自己再跑回来即可。其实不然。


函数g(a,b)表示过河,b(a)表示回来。如果过河时间分别为1,2,5,10

那么两种过河方案:

1.初想方案:

    g(1,2)=2

    g (1, 10) = 10

    g (1, 5 ) =  5

    b(1) * 2 = 2

    sum  =19

2.另一种方案:. g(1,2) =2

    b(1) =1

    g(5,10)=10

    b(2)=2

    g(1,2)=2

    sum = 17

另一种方案就是让两个跑的快的先跑过去,然后一个带回来船,然后两个慢的跑过去,另一个跑的快的带回来船。

如此两种方案的速度分别为:

设跑的快的分别为a, b,跑的慢的为c, d

那么c, d过河需要的时间分别为:

1.a + a + c + d

2.a +b+b+d

如此一来,求得b+b与a+c的大小关系,则知道取哪种方案最佳。


题目有poj2573, poj1700

1700解题代码:(也有dfs,dp写法,但是我没理解)

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>using namespace std;int main()
{int n, sec;int time[1010];int t;int temp;int i, j, k;//freopen("test", "r", stdin);scanf("%d", &t);while(t--){scanf("%d", &n);memset(time, 0, sizeof(time));for(i = 0; i < n; i++)scanf("%d", &time[i]);if(n == 1)printf("%d\n", time[0]);else if(2 == n)printf("%d\n", time[0] > time[1] ? time[0] : time[1]);else if(3 == n)printf("%d\n", time[0] + time[1] + time[2]);else{sec = 0;sort(time ,time + n);temp = time[1]*2;while(n >=4){sec += time[0];sec += time[n-1];i = time[1] * 2;j = time[0] + time[n-2];if(i > j)sec += j;else sec += i;n -= 2;}if(2 == n)printf("%d\n", sec + time[1]);else if(3 == n)printf("%d\n", sec + time[0] + time[1] + time[2]);}}return 0;
}

2573:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>using namespace std;struct outPut
{int front, end;outPut(int a, int b):front(a), end(b){}outPut(int a):front(a), end(-1){}
};void output(outPut *a)
{if(a->end != -1)printf("%d %d\n", a->front, a->end);elseprintf("%d\n", a->front);
}int main()
{int temp;int pe[1010];int i, j;int n;// cost timeint sec;//freopen("test", "r", stdin);queue <outPut> Queue;while(~scanf("%d", &n)){//printf("\nCase: \n");for(i = 0; i < n; i++)scanf("%d", &pe[i]);if(1 == n){printf("%d\n", pe[0]);printf("%d\n", pe[0]);}//..else if(2 == n){if(pe[0] > pe[1]){i = pe[0];j = pe[1];}else{i = pe[1];j = pe[0];}printf("%d\n", i);printf("%d %d\n", j, i);}else if(3 == n){sort(pe, pe+3);printf("%d\n", pe[0]+pe[1]+pe[2]);printf("%d %d\n", pe[0], pe[2]);printf("%d\n", pe[0]);printf("%d %d\n", pe[0], pe[1]);}else{sort(pe, pe+n);temp = pe[1] * 2;sec = 0;while(n >= 4){sec += pe[n-1];sec += pe[0];if(temp < pe[0] + pe[n-2]){Queue.push(outPut(pe[0], pe[1]));Queue.push(outPut(pe[0]));Queue.push(outPut(pe[n-2], pe[n-1]));Queue.push(outPut(pe[1]));sec += temp;}        else{Queue.push(outPut(pe[0], pe[n-1]));Queue.push(outPut(pe[0]));Queue.push(outPut(pe[0], pe[n-2]));Queue.push(outPut(pe[0]));sec += pe[0] + pe[n-2];}n -= 2;}if(2 == n){outPut *tmp;printf("%d\n", sec + pe[1]);while(!Queue.empty()){tmp = &Queue.front();output(tmp);Queue.pop();}printf("%d %d\n", pe[0], pe[1]);}else if(3 == n){outPut *tmp;printf("%d\n", sec + pe[0] + pe[1] + pe[2]);while(!Queue.empty()){tmp = &Queue.front();output(tmp);Queue.pop();}printf("%d %d\n", pe[0], pe[2]);printf("%d\n", pe[0]);printf("%d %d\n", pe[0], pe[1]);}}}return 0;
}


这篇关于杂题_POJ上的过桥问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/757465

相关文章

使用easy connect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题

《使用easyconnect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题》:本文主要介绍使用easyconnect之后,maven无法... 目录使用easGWowCy connect之后,maven无法使用,原来需要配置-DJava.net.pr

解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException: org.junit.Test问题

《解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException:org.junit.Test问题》:本文主要介绍解决tomcat启动时报Junit相... 目录tomcat启动时报Junit相关错误Java.lang.ClassNotFoundException

解决Maven项目报错:failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题

《解决Maven项目报错:failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题》这篇文章主要介... 目录Maven项目报错:failed to execute goal org.apache.maven.pl

MySQL主从同步延迟问题的全面解决方案

《MySQL主从同步延迟问题的全面解决方案》MySQL主从同步延迟是分布式数据库系统中的常见问题,会导致从库读取到过期数据,影响业务一致性,下面我将深入分析延迟原因并提供多层次的解决方案,需要的朋友可... 目录一、同步延迟原因深度分析1.1 主从复制原理回顾1.2 延迟产生的关键环节二、实时监控与诊断方案

SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法

《SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法》在SQLyog中执行存储过程时出现的前置缩进问题,实际上反映了SQLyog对SQL语句解析的一个特殊行为,本文给大家介绍了详... 目录问题根源正确写法示例永久解决方案为什么命令行不受影响?最佳实践建议问题根源SQLyog的语句分

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊