UVA 1349 Optimal Bus Route Design

Description
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 (i, j) . 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 (i, j) and d (j, i) 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.


【题目分析】
网络流。二分带权匹配处理唯一后继。


【代码】

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define inf (0x3f3f3f3f)
#define maxn 40005

int h[maxn],to[maxn],ne[maxn],fl[maxn],cost[maxn],en=0,S,T;
int n,flow=0;

void add(int a,int b,int r,int c)
{
    to[en]=b; ne[en]=h[a]; fl[en]=r; cost[en]=c; h[a]=en++;
    to[en]=a; ne[en]=h[b]; fl[en]=0; cost[en]=-c;h[b]=en++;
}

int dis[maxn],minn[maxn],inq[maxn],with[maxn];

bool tell()
{
    queue <int> q;
    while (!q.empty()) q.pop();
    memset(dis,0x3f,sizeof dis);
    memset(minn,0x3f,sizeof minn);
    memset(inq,0,sizeof inq);
    memset(with,0,sizeof with);
    q.push(S); dis[S]=0; inq[S]=1;
    while (!q.empty())
    {
        int x=q.front(); q.pop(); inq[x]=0;
        for (int i=h[x];i>=0;i=ne[i])
        {
            if (fl[i]>0&&dis[to[i]]>dis[x]+cost[i])
            {
                dis[to[i]]=dis[x]+cost[i];
                minn[to[i]]=min(minn[x],fl[i]);
                with[to[i]]=i;
                if (!inq[to[i]]) inq[to[i]]=1,q.push(to[i]);
            }
        }
    }
    if (dis[T]==inf) return false;
    return true;
}

int zeng()
{
    flow+=minn[T];
    for (int i=T;i!=S;i=to[with[i]^1])
    {
        fl[with[i]]-=minn[T];
        fl[with[i]^1]+=minn[T];
    }
    return minn[T]*dis[T];
}

int main()
{
    while (scanf("%d",&n)!=EOF&&n)
    {
        flow=0;
        memset(h,-1,sizeof h); en=0;
        S=0;T=2*n+1;
        for (int i=1;i<=n;++i)
        {
            int a=i,b,c;
            while (scanf("%d",&b)!=EOF&&b)
            {
                scanf("%d",&c);
//              printf("%d to %d is %d\n",a,b,c);
                add(a*2-1,b*2,1,c);
            }
        }
        for (int i=1;i<=n;++i) add(S,i*2-1,1,0);
        for (int i=1;i<=n;++i) add(i*2,T,1,0);
        int ans=0;
        while (tell())
        {
            ans+=zeng();
//          printf("ans now is %d\n",ans);
        }
        if (flow!=n) printf("N\n");
        else printf("%d\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值