1088 - Subway Timing

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.

\epsfbox{p4454.eps}

Figure 8: Stockholm subway map

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 (1$ \le$N$ \le$100), 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 ab and t ( 1$ \le$ab$ \le$N, and 1$ \le$t$ \le$300), 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: %d\n",++test,r);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值