POJ 1700(过河问题)

此题讲的是N个人过河,每个人都有自己的过河时间,一条船只能承受2个人,所用时间为其中过河时间最多的,所以呢,想到有两种情况,第一种:过河时间最少的人来回接送其他人,第二种:过河时间最少和次少的人来回接送其他人,刚开始就觉得第一种时间必然是最少的,但是仔细想想,不然。因为第一种情况虽然单次过河时间少,但送人的次数要多,如第1个人(过河时间最少)接送第i人
dp[i]=dp[i-1]+time[0]+time[i]; 而第二种情况呢,虽然来回次数多,但是接送的人要多,如第1个人接第i-1个和第i个人,然后让第i-1个和第i个人一块过河,第2个人再接送第1个人。dp[i]=dp[i-2]+time[0]+time[i]+time[1]*2。所以每次计算出这两个值都应该进行比较,从而AC!//(转)

#include <iostream>
#include <algorithm>

using namespace std;

int dp[1050];

int cmp(int a, int b)
{
	return a<b;
}
int main ()
{
	 int t;
	 cin>>t;
	 while(t--)
	 {
		int n;
		cin>>n;
		int a[1050];
		int i;
		for(i=0; i<n; i++)
			cin>>a[i];
		sort(a,a+n,cmp);
		dp[0]=a[0];
		dp[1]=a[1];
		for(i=2; i<n; i++)
		{
			int min1=dp[i-1]+a[i]+a[0];
			int min2=dp[i-2]+a[i]+2*a[1]+a[0];
			if(min1>min2)
				dp[i]=min2;
			else
				dp[i]=min1;
		}
		cout<<dp[n-1]<<endl;
	 }
//	 system("pause");
	 return 0;
}

时间: 2024-08-12 03:47:49

POJ 1700(过河问题)的相关文章

POJ 1700 Crossing River(贪心)

V - Crossing River Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1700 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. There

POJ 1700 经典过河问题(贪心)

POJ题目链接:http://poj.org/problem?id=1700 N个人过河,船每次最多只能坐两个人,船载每个人过河的所需时间不同,问最快的过河时间. 思路: 当n=1,2,3时所需要的最小时间很容易求得,现在由n>=4,假设n个人单独过河所需要的时间存储在数组t中,将数组t按升序排序,那么 这时将单独过河所需要时间最多的两个旅行者送到对岸去,有两种方式: 1> 最快的(即所用时间t[0])和次快的过河,然后最快的将船划回来,再次慢的和最慢的过河,然后次快的将船划回来. 即所需时间

[ACM] poj 1700 Crossing River (经典过河问题)

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10212   Accepted: 3855 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arr

poj 1700

http://poj.org/problem?id=1700 题目大意就是一条船,有N个人需要过河,求N个人最短过河的时间 #include <stdio.h> int main() { int t[1001],T,j,i,n,sum; scanf("%d",&T); for(i=0;i<T;i++) { scanf("%d",&n); for(j=0;j<n;j++) scanf("%d",&t[

poj 1700 Crossing River C++/Java

http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最短时间. 思路: 贪心. 对于每次过河的,有两种情况: //最快和最慢过去,然后最快回来.在和次慢过去.最快回来 int action1=a[i-1] + a[0] + a[i-2] +a[0]; //最快和次慢过去,然后最快回来,在次慢和最慢过去,次慢回来 int action2=a[1] +a[

poj - 1700 题解

大概题意:N个人要过河,一个船最多承载两人,且过河速度等于船上划船速度最慢的人的速度,求过河的最短时间. 题目解法:首先对题目进行分析,可以发现:船过了河还要有一个人再把船送回来. 假设把人按过河速度从大到小排序并编号1-4,则会发现有两种过河方式:1.13一组,1回来,24一组,2回来,12一组,完成:2.12一组,1回来,13一组,1回来,14一组,完成. 因此贪心策略就是:把人按过河速度从大到小排序,然后按上述方式过河,每次选取最优方式. 代码如下: 1 #include<iostream

poj 1700 Crossing River

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12585   Accepted: 4787 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arr

POJ 1700 cross river (数学模拟)

 Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10311   Accepted: 3889 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle

poj 1700 Crossing River(贪心)

分析:题意 源岸数量<=3  很好判断 3:num[0]+num[1]+num[2] 2:num[1] ,1:num[0] 源岸数量>3 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main(void) { int n,t ; int i,j; int num[1001]; cin>>