UVa1025/UVa437/UVa1347

原创 2016年08月30日 00:40:27

UVa1025 

A Spy in the Metro

Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. Afterseveral thrilling events we find her in the first station of Algorithms City Metro, examining the timetable. The Algorithms City Metro consists of a single line with trains running both ways, so its timetable is not complicated.Maria has an appointment with a local spy at the last station of Algorithms City Metro. Mariaknows that a powerful organization is after her. She also knows that while waiting at a station, she isat great risk of being caught. To hide in a running train is much safer, so she decides to stay in runningtrains as much as possible, even if this means traveling backward and forward. Maria needs to knowa schedule with minimal waiting time at the stations that gets her to the last station in time for herappointment. You must write a program that finds the total waiting time in a best schedule for Maria.The Algorithms City Metro system has N stations, consecutively numbered from 1 to N. Trainsmove in both directions: from the first station to the last station and from the last station back to thefirst station. The time required for a train to travel between two consecutive stations is fixed since alltrains move at the same speed. Trains make a very short stop at each station, which you can ignorefor simplicity. Since she is a very fast agent, Maria can always change trains at a station even if thetrains involved stop in that station at the same time.

Input

The input file contains several test cases. Each test case consists of seven lines with information asfollows.Line 1. The integer N (2 ≤ N ≤ 50), which is the number of stations.Line 2. The integer T (0 ≤ T ≤ 200), which is the time of the appointment.Line 3. N − 1 integers: t1, t2, . . . , tN−1 (1 ≤ ti ≤ 20), representing the travel times for the trainsbetween two consecutive stations: t1 represents the travel time between the first two stations, t2the time between the second and the third station, and so on.Line 4. The integer M1 (1 ≤ M1 ≤ 50), representing the number of trains departing from the firststation.Line 5. M1 integers: d1, d2, . . . , dM1 (0 ≤ di ≤ 250 and di < di+1), representing the times at whichtrains depart from the first station.Line 6. The integer M2 (1 ≤ M2 ≤ 50), representing the number of trains departing from the N-thstation.Line 7. M2 integers: e1, e2, . . . , eM2 (0 ≤ ei ≤ 250 and ei < ei+1) representing the times at whichtrains depart from the N-th station.The last case is followed by a line containing a single zero.

Output

For each test case, print a line containing the case number (starting with 1) and an integer representingthe total waiting time in the stations for a best schedule, or the word ‘impossible’ in case Maria isunable to make the appointment. Use the format of the sample output.

Sample Input

4

55

5 10 15

4

0 5 10 20

4

0 5 10 15

4

18

1 2 3

5

0 3 6 10 12

6

0 3 5 7 12 15

2

30

20

1

20

7

1 3 5 7 11 13 17

0

Sample Output

Case Number 1: 5

Case Number 2: 0

Case Number 3: impossible

code:

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
const int maxn=55;
const int maxt=255;
const int inf=100000000;
int N,T,M1,M2,tot;
int f[maxt][maxn],t[maxt],d1[maxn],d2[maxn];
bool train[maxt][maxn][2];
void init(){
int i,j;
memset(train,0,sizeof(train));
for(i=1;i<=N;i++){
for(j=1;j<=M1;j++){
if(d1[j]<=T)train[d1[j]][i][0]=1;
d1[j]+=t[i];
}
}
for(i=N;i>=1;i--){
for(j=1;j<=M2;j++){
if(d2[j]<=T)train[d2[j]][i][1]=1;
d2[j]+=t[i-1];
}
    }
}
void work(){
int i,j;
for(i=1;i<N;i++)f[T][i]=inf;
f[T][N]=0;
for(i=T-1;i>=0;i--){
for(j=1;j<=N;j++){
f[i][j]=f[i+1][j]+1;
if(j<N&&train[i][j][0]&&i+t[j]<=T){
f[i][j]=min(f[i][j],f[i+t[j]][j+1]);
}
if(j>1&&train[i][j][1]&&i+t[j-1]<=T){
f[i][j]=min(f[i][j],f[i+t[j-1]][j-1]);
}
}
}
printf("Case Number %d: ",++tot);
if(f[0][1]>=inf)printf("impossible\n");
else printf("%d\n",f[0][1]);
}
int main(){
int i;
scanf("%d",&N);
while(N!=0){
scanf("%d",&T);
for(i=1;i<N;i++)scanf("%d",&t[i]);
scanf("%d",&M1);
for(i=1;i<=M1;i++)scanf("%d",&d1[i]);
scanf("%d",&M2);
for(i=1;i<=M2;i++)scanf("%d",&d2[i]);
init();
work();
scanf("%d",&N);
}
return 0;
}

UVa437

The Tower of Babylon

Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this talehave been forgotten. So now, in line with the educational nature of this contest, we will tell you thewhole story:The babylonians had n types of blocks, and an unlimited supply of blocks of each type.Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block couldbe reoriented so that any two of its three dimensions determined the dimensions of the baseand the other dimension was the height.They wanted to construct the tallest tower possible by stacking blocks. The problem wasthat, in building a tower, one block could only be placed on top of another block as long asthe two base dimensions of the upper block were both strictly smaller than the correspondingbase dimensions of the lower block. This meant, for example, that blocks oriented to haveequal-sized bases couldn’t be stacked.Your job is to write a program that determines the height of the tallest tower the babylonians canbuild with a given set of blocks.
Input
The input file will contain one or more test cases. The first line of each test case contains an integer n,representing the number of different blocks in the following data set. The maximum value for n is 30.Each of the next n lines contains three integers representing the values xi, yi and zi.Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line containing the case number (they are numbered sequentially startingfrom 1) and the height of the tallest possible tower in the format‘Case case: maximum height = height’
Sample Input
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
code:
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
struct node{
    int x,y,z;
}num[200];
bool cmp(node a, node b){
    return a.x*a.y <b.x*b.y;
}
int n,m, x,y,z,f[200];
int main(){
    int flag =0;
    while(cin>>n&&n>0){
        m=0;
        int i,j;
        for( i=1; i<=n; i++){
        cin>>x>>y>>z;
            num[++m].x=x;num[m].y=y;num[m].z=z;
num[++m].x=x;num[m].y=z;num[m].z=y;
num[++m].x=y;num[m].y=x;num[m].z=z;
num[++m].x=y;num[m].y=z;num[m].z=x;
num[++m].x=z;num[m].y=x;num[m].z=y;
num[++m].x=z;num[m].y=y;num[m].z=x;
        }
        sort(num+1,num+m+1,cmp);
        int ans=0;
        for(i=1;i<=m;i++){
f[i]=num[i].z;
for(j=1;j<i;j++)
if(num[i].x>num[j].x&&num[i].y>num[j].y)
f[i]=max(f[i],num[i].z+f[j]);
if(f[i]>ans)ans=f[i];
}
        printf("Case %d: maximum height = %d\n",++flag,ans);
    }
    return 0;
}
UVa1347

Tour

John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi = < xiyi > . John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinctx -coordinates.

Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John's strategy.

The program input is from a text file. Each data set in the file stands for a particular set of points. For each set of points the data set contains the number of points, and the point coordinates in ascending order of the x coordinate. White spaces can occur freely in input. The input data are correct.

For each set of data, your program should print the result to the standard output from the beginning of a line. The tour length, a floating-point number with two fractional digits, represents the result.


Note: An input/output sample is in the table below. Here there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example, has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47 for the first data set in the given example).

Input

3 
1 1
2 3
3 1
4 
1 1 
2 3
3 1
4 2

Output

6.47
7.89


code:

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
int n;
inline int read(){
int x=0;char ch=getchar();
while(ch<'0'||ch>'9'){ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
struct point{
int x,y;
}p[maxn];
double dist[maxn][maxn];
double f[maxn][maxn];
bool cmp(point a,point b){
return a.x<b.x;
}
double dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void init(){
int i,j;
for(i=1;i<=n;i++)p[i].x=read(),p[i].y=read();
sort(p+1,p+1+n,cmp);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
dist[i][j]=dist[j][i]=dis(p[i],p[j]);
memset(f,0,sizeof(f));
}
double dfs(int a,int b){
if(f[a][b]!=0)return f[a][b];
if(a==n-1)return f[a][b]=dist[n-1][n]+dist[b][n];
return f[a][b]=min(dfs(a+1,b)+dist[a][a+1],dfs(a+1,a)+dist[b][a+1]);
}
int main(){
while(cin>>n){
init();
   printf("%.2lf\n",dist[2][1]+dfs(2,1));
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UVa1347 - Tour

题意:典型的动态规划例题。又叫做双调欧几里得旅行商问题。算法导论里面的题目。   思路: dp[i][j] 表示从 i 到 1,再从1到j的距离。在这个路径上,点 1 到 P...
  • a197p
  • a197p
  • 2015年10月13日 16:50
  • 666

UVa1025

UVa1025红书上的例题9-1,影响决策的只有当前的时间以及所在的站,所以用d(i,j)表示i时刻在j站。然后就是无非两种选择,一种是等一分钟,或是有车的话, 乘车到最近的一站。精妙之处在于has_...
  • pibaixinghei
  • pibaixinghei
  • 2015年07月22日 20:23
  • 885

例题9-3 旅行 UVa1347

1.题目描述:点击打开链接 2.解题思路:本题看似一道几何问题,实际上可以利用动态规划解决。走一圈周长最短可以等价为两个人同时从最左端出发,沿着不同的路径走到最右端。如果定义d(i,j)表示1~max...
  • u014800748
  • u014800748
  • 2015年02月11日 17:59
  • 1912

UVA1347 Tour (DP)

题目大意:有n个点,给出x、y坐标。找出一条路,从最左边的点出发,严格向右走到达最右点再严格向左回到最左点。问最短路径是多少?分析: 1.首先需要将原问题转化为,两个人A、B同时从最左边的点出发...
  • qq_28236309
  • qq_28236309
  • 2016年07月17日 12:46
  • 464

UVA437(DP)

题意:求所给的n种石头最高能组成多高的塔,其中组成塔的石头的两条边必须分别大于它上面的石头的两条边。每种石头有无限个并且可以随意翻转。 这道题容易迷惑人的一点是石头是无限的,但仔细考虑由于有大小的限制...
  • YanzheShi
  • YanzheShi
  • 2015年07月26日 15:30
  • 351

UVa1025 DP (还要再看看~)

题意 请参考紫书描述~ 思路 状态开始没有定好,还是参考了下紫书..     dp[i][j] 表示在第i秒,第j个站上,一共的最少等待时间             初始条件dp[T][n] = 0,...
  • luke2834
  • luke2834
  • 2015年03月16日 18:54
  • 974

uva437 - The Tower of Babylon(DAG上的DP)

题目:uva437 - The Tower of Babylon(DAG上的DP) 题目大意:给你一些立方体,给出长宽高XYZ。现在希望你将这些立方题叠起来,使得最后的高度最大,并且这些立方...
  • u012997373
  • u012997373
  • 2014年08月06日 20:55
  • 1272

UVa437

用二元组(idx,k)表示状态 idx为顶面立方体序号,k是高的序号。 状态(3,1)就是这个立方体在顶面,且高是b #include using namespace std; struct n...
  • a197p
  • a197p
  • 2015年10月11日 10:10
  • 197

uva1025 动态规划

这道题的边界是dp(T,N)=0,状态dp(i,j)表示在时间i、第j个车站最少等待时间,有三个决策:1、等1分钟 2、如果有向左的车,向左 3、若果有向右的车,向右  转移方程就是dp(i,j)=m...
  • flyawayl
  • flyawayl
  • 2016年10月09日 20:51
  • 116

UVA1347-Tour 双调欧几里得旅行商问题 - 递归记忆实现

题意: 平面上有n个坐标均为正数的点,按照x坐标从小到大一次给出。求一条最短路线,从最左边的点出发到最右边的点,再回到最左边的点。除了第一个和最右一个点其他点恰好只经过一次。分析: 可以等效为两...
  • fckai
  • fckai
  • 2015年08月27日 15:35
  • 305
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVa1025/UVa437/UVa1347
举报原因:
原因补充:

(最多只允许输入30个字)