概述
Like most modern cities, Stockholm has a well-developed public transportation system. The heart of public transportation in Stockholm is the subway. A topological map of the subway system illustrates the different subway lines and how they are connected, as illustrated in Figure 8. For this problem you should assume that a subway map is always tree-shaped, even though this is not quite true for Stockholm because of the cycle formed by the green and blue lines.
A topological map says very little about the geometry of a subway system, such as the distances (and consequently travel times) between different subway stations. For instance, as most students in Stockholm know, the distance between ``Tekniska Högskolan" (The Royal Institute of Technology) and ``Universitetet(Stockholm University) is quite large, even though there is no indication of this on the map.
You must write a program that can augment a topological map by writing down the time required to travel between every pair of adjacent subway stations. Fortunately, those travel times are known, so you do not have to measure the times yourself. But the actual travel times are given in seconds, while the times must be written on the map as estimates of integral numbers of minutes.
A natural way of estimating times might be to simply round all the travel times to the nearest minute. However, this can cause huge cumulative errors. For instance, in the Stockholm map, this estimation method could result in an error as large as 15 minutes in the total travel time between some pairs of stations. In order to counter this, your program may choose to round some travel times up and round other travel times down. The rounding must be done in such a way that the largest cumulative error between any pair of stations is minimized.
Input
The input consists of several test cases. Each test case starts with an integer N (1N100), which is the number of subway stations. The N stations are identified using the integers from 1 to N. Each of the next N- 1 lines contains three integers a, b and t ( 1a, bN, and 1t300), indicating that stations a and bare adjacent and that it takes t seconds to travel between them. For simplicity, ignore the time a train spends standing still at a station.
The last test case is followed by the integer zero on a line by itself.
Output
For each test case, print the case number (starting with 1) then the largest rounding error in seconds of travel time between any pair of stations when the times for adjacent pairs of stations are rounded optimally. Follow the format of the sample output.
Sample Input
2 1 2 110 4 1 2 40 2 3 40 3 4 40 4 1 2 90 1 3 90 1 4 90 0
Sample Output
Case 1: 10 Case 2: 40 Case 3: 60
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int f[105][500],tmp[500];
int mid,l,r;
const int inf=1000000;
struct node
{
int v,c,next;
}edge[300];
int g[105];
int tt=0,n;
int addedge(int a,int b,int c)
{
edge[++tt].v=b;
edge[tt].c=c;
edge[tt].next=g[a];
g[a]=tt;
return 0;
}
int updata(int &a,int b)
{
if(b>a)
a=b;
return 0;
}
int check(int a)
{
if(a>=-mid&&a<=mid)
return 1;
return 0;
}
int dfs(int x,int fa)
{
for(int i=0;i<=mid;i++)
f[x][i]=-inf;
f[x][0]=0;
int y;
for(int i=g[x];i;i=edge[i].next)
if(edge[i].v!=fa)
{
dfs(edge[i].v,x);
for(int j=0;j<=mid;j++)
{
tmp[j]=f[x][j];
f[x][j]=-inf;
}
y=edge[i].v;
for(int j=0;j<=mid;j++) if(tmp[j]!=-inf)
for(int k=0;k<=mid;k++) if(f[y][k]!=-inf)
{
if(check(j+k-edge[i].c)&&check(tmp[j]+f[y][k]-edge[i].c))
updata(f[x][max(j,k-edge[i].c)],min(tmp[j],f[y][k]-edge[i].c));
edge[i].c-=60;
if(check(j+k-edge[i].c)&&check(tmp[j]+f[y][k]-edge[i].c))
updata(f[x][max(j,k-edge[i].c)],min(tmp[j],f[y][k]-edge[i].c));
edge[i].c+=60;
}
}
return 0;
}
int main()
{
int a,b,c,test=0;
while(scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
g[i]=0;
tt=0;
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
c=c%60;
addedge(a,b,c);
addedge(b,a,c);
}
l=-1;r=300;
while(l+1<r)
{
mid=(l+r)/2;
dfs(1,0);
int i;
for(i=0;i<=mid;i++)
if(f[1][i]>-inf)
break;
if(i==mid+1)
l=mid;
else
r=mid;
}
printf("Case %d: %dn",++test,r);
}
return 0;
}
最后
以上就是开放冰棍为你收集整理的1088 - Subway Timing的全部内容,希望文章能够帮你解决1088 - Subway Timing所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复