UVA 1349 Optimal Bus Route Design
来源:互联网 发布:java date sethour 编辑:程序博客网 时间:2024/05/01 16:33
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 40005int 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); }}
- Uva-1349-Optimal Bus Route Design
- UVa 1349 Optimal Bus Route Design
- UVA 1349 Optimal Bus Route Design
- Optimal Bus Route Design UVA 1349
- (beginer) UVA 最小费用流 1349Optimal Bus Route Design
- UVA 1349 - Optimal Bus Route Design(KM完美匹配)
- UVa #1349 Optimal Bus Route Design (例题11-10)
- uva 1349 - Optimal Bus Route Design(完美匹配)
- UVA 1349Optimal Bus Route Design(网络流)
- UVA 1349 Optimal Bus Route Design(KM判环)
- uva-1349 Optimal Bus Route Design(最小费用最大流)
- uva 3353 Optimal Bus Route Design(费用流)
- uva 3353 Optimal Bus Route Design (费用流/KM)
- UVA 1349 Optimal Bus Route Design 最小费用流+拆点
- UVA 1349 Optimal Bus Route Design(最小权完美匹配)
- UVA 1349 Optimal Bus Route Design (最小费用流 -- 二分图最小权完美匹配)
- UVA 1349 Optimal Bus Route Design——最小权完美匹配
- LA 3353 Optimal Bus Route Design
- cocos2d错误点
- NestedScrollingParent, NestedScrollingChild 详解
- QuickHit项目
- 数据结构顺序查找验证程序
- 学习python的第二十五天-老程序员的忠告
- UVA 1349 Optimal Bus Route Design
- PAT B1028
- Glide加载图片并保存到本地返回file,bitmap
- 谈谈Android 6.0运行时权限理解
- 南京邮电大学离散数学实验三偏序关系中盖住关系的求取及格论中有补格的判定
- 谈谈Android 6.0运行时权限理解
- PAT B1029
- PHP——CURL类
- PROCEDURE-12