# UVa1025/UVa437/UVa1347

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;
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;
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;
}

#### uva 1347 - Tour(双调欧几里得旅行商问题）

2014-03-26 09:37:29

#### 例题9-3 旅行 UVa1347

2015-02-11 17:59:22

#### UVa1347 - Tour

2015-10-13 16:50:57

#### uva1347 旅行 不回头旅行商

2016-08-06 11:56:04

#### UVa1025 DP （还要再看看~）

2015-03-16 18:54:06

#### UVA 1347 - Tour - ACM/ICPC SEERC 2005

2018-03-30 10:06:15

#### #UVA1025#A Spy in the Metro（Dp经典）

2017-07-17 20:38:35

#### UVA 1025 A Spy in the Metro DP

2014-11-11 00:52:39

#### uva1347 Tour

2016-11-24 00:08:45

#### 例题9-2 巴比伦塔 UVa437

2015-02-11 15:53:21