UVa 1349 Optimal Bus Route Design

42 篇文章 0 订阅
39 篇文章 0 订阅

Description

Download as PDF

A big city wants to improve its bus transportation system. One of the improvement is to add scenic routes which go es through attractive places. Your task is to construct a bus-route-plan for sight-seeing buses in a city.

You are given a set of scenic lo cations. For each of these given lo cations, there should be only one bus route that passes this lo cation, and that bus route should pass this lo cation exactly once. The number of bus routes is unlimited. However, each route should contain at least two scenic lo cations.

From location i to location j , there may or may not be a connecting street. If there is a street from location i to location j , then we say j is an out-neighbor of i . The length of the street from i to j is d (ij) . The streets might be one way. So it may happen that there is a street from i to j , but no street from j to i . In case there is a street from i to j and also a street from j to i , the lengths d (ij) and d (ji) might be different. The route of each bus must follow the connecting streets and must be a cycle. For example, the route of Bus A might be from location 1 to location 2, from location 2 to location 3, and then from location 3 to location 1. The route of Bus B might be from location 4 to location 5, then from location 5 to location 4. The length of a bus route is the sum of the lengths of the streets in this bus route. The total length of the bus-route-plan is the sum of the lengths of all the bus routes used in the plan. A bus-route-plan is optimal if it has the minimum total length. You are required to compute the total length of an optimal bus-route-plan.

Input 

The input file consists of a number of test cases. The first line of each test case is a positive integer n , which is the number of locations. These n locations are denoted by positive integers 1, 2,..., n . The next n lines are information about connecting streets between these lo cations. The i -th line of these n lines consists of an even number of positive integers and a 0 at the end. The first integer is a lo cation j which is an out-neighbor of location i , and the second integer is d (ij) . The third integer is another locationj' which is an out-neighbor of i , and the fourth integer is d (ij') , and so on. In general, the (2k - 1) th integer is a location t which is an out-neighbor of location i , and the 2k th integer is d (it) .

The next case starts immediately after these n lines. A line consisting of a single ` 0' indicates the end of the input file.

Each test case has at most 99 locations. The length of each street is a positive integer less than 100.

Output 

The output contains one line for each test case. If the required bus-route-plan exists, then the output is a positive number, which is the total length of an optimal bus-route-plan. Otherwise, the output is a letter `N'.

Sample Input 

3 
2 2 3 1 0 
1 1 3 2 0 
1 3 2 7 0  
8 
2 3 3 1 0 
3 3 1 1 4 4 0
1 2 2 7 0 
5 4 6 7 0 
4 4 3 9 0 
7 4 8 5 0 
6 2 5 8 8 1 0
6 6 7 2 0
3 
2 1 0 
3 1 0 
2 1 0
0

Sample Output 

7 
25 
N

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

带权二分图匹配(费用流)+拆点~

对于一个点i,拆成两点i*2和i*2+1,然后如果k和j之间有边,则从2*k向2*j+1连边,然后从源点0向每个2*i连边,从每个2*i+1向汇点2*n+2连边,求费用流即可~


#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define inf 999999999

int n,a[401],dis[401],flow,cost,x,val,tot,ne[401];
bool b[401];

struct edge{
	int x,y,cap,flow,cost;
	edge(int u,int v,int k,int z,int kkz):x(u),y(v),cap(k),flow(z),cost(kkz) {}
};

vector<edge> ed;
vector<int> p[401];

void add(int u,int v,int k,int z)
{
	ed.push_back(edge(u,v,k,0,z));
	ed.push_back(edge(v,u,0,0,-z));
	int kkz=ed.size();
	p[u].push_back(kkz-2);
	p[v].push_back(kkz-1);
}

bool findd()
{
	for(int i=1;i<=tot;i++) dis[i]=inf;
	queue<int> q;q.push(0);dis[0]=0;a[0]=inf;b[0]=1;
	while(!q.empty())
	{
		int k=q.front();q.pop();b[k]=0;
		int kkz=p[k].size();
		for(int i=0;i<kkz;i++)
		{
			edge z=ed[p[k][i]];
			if(z.cap-z.flow && dis[z.y]>dis[k]+z.cost)
			{
				dis[z.y]=dis[k]+z.cost;
				ne[z.y]=p[k][i];
				a[z.y]=min(a[k],z.cap-z.flow);
				if(!b[z.y])
				{
					b[z.y]=1;q.push(z.y);
				}
			}
		}
	}
	if(dis[tot]==inf) return 0;
	flow+=a[tot];cost+=a[tot]*dis[tot];
	for(int i=tot;i;i=ed[ne[i]].x)
	{
		ed[ne[i]].flow+=a[tot];
		ed[ne[i]^1].flow-=a[tot];
	}
	return 1;
}

int cal()
{
	flow=cost=0;
	while(findd());
	return flow;
}

int main()
{
	while(scanf("%d",&n)==1 && n)
	{
		tot=(n<<1)+2;ed.clear();
		for(int i=0;i<=tot;i++) p[i].clear();
		for(int i=1;i<=n;i++)
		  while(scanf("%d",&x)==1 && x)
		  {
			scanf("%d",&val);
			add(i*2,x*2+1,1,val);
		  }
		for(int i=1;i<=n;i++) add(0,i*2,1,0),add(i*2+1,tot,1,0);
		if(cal()==n) printf("%d\n",cost);
		else printf("N\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值