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
#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;
}
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 = < xi, yi > . 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;
}