UVA - 1349          

                                   Optimal Bus Route Design

Time Limit: 3000MS

Memory Limit: Unknown

64bit IO Format: %lld & %llu

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.

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 (i, j) . The third integer is another location j' which is an out-neighbor of i , and the fourth integer is d (i, j') , 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 (i, t) .

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

【思路】

二分图最佳完美匹配。

“只要每个点有唯一的后继,每个点恰好属于一个圈”。拆点后问题转化为二分图的最佳完美匹配问题。

【代码】

 #include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = +;
const int INF = 1e9; struct Edge{ int u,v,cap,flow,cost;
}; struct MCMF {
int n,m,s,t;
int inq[maxn],a[maxn],d[maxn],p[maxn];
vector<int> G[maxn];
vector<Edge> es; void init(int n) {
this->n=n;
es.clear();
for(int i=;i<n;i++) G[i].clear();
}
void AddEdge(int u,int v,int cap,int cost) {
es.push_back((Edge){u,v,cap,,cost});
es.push_back((Edge){v,u,,,-cost});
m=es.size();
G[u].push_back(m-);
G[v].push_back(m-);
} bool SPFA(int s,int t,int& flow,int& cost) {
for(int i=;i<n;i++) d[i]=INF;
memset(inq,,sizeof(inq));
d[s]=; inq[s]=; p[s]=; a[s]=INF;
queue<int> q; q.push(s);
while(!q.empty()) {
int u=q.front(); q.pop(); inq[u]=;
for(int i=;i<G[u].size();i++) {
Edge& e=es[G[u][i]];
int v=e.v;
if(e.cap>e.flow && d[v]>d[u]+e.cost) {
d[v]=d[u]+e.cost;
p[v]=G[u][i];
a[v]=min(a[u],e.cap-e.flow); //min(a[u],..)
if(!inq[v]) { inq[v]=; q.push(v);
}
}
}
}
if(d[t]==INF) return false;
flow+=a[t] , cost+=a[t]*d[t];
for(int x=t; x!=s; x=es[p[x]].u) {
es[p[x]].flow+=a[t]; es[p[x]^].flow-=a[t];
}
return true;
}
int Mincost(int s,int t,int& cost) {
int flow=; cost=;
while(SPFA(s,t,flow,cost)) ;
return flow;
}
} mc; int n; int main() {
while(scanf("%d",&n)== && n) {
mc.init(n+n+);
int s=n+n,t=s+;
int u,v,w;
FOR(u,,n) {
while(scanf("%d",&v)== && v) {
scanf("%d",&w);
v--;
mc.AddEdge(u,n+v,,w);
}
mc.AddEdge(s,u,,); mc.AddEdge(u+n,t,,);
}
int flow,cost;
flow=mc.Mincost(s,t,cost);
if(flow<n) printf("N\n");
else printf("%d\n",cost);
}
return ;
}

UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)的更多相关文章

  1. Uva1349Optimal Bus Route Design(二分图最佳完美匹配)(最小值)

    题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #incl ...

  2. UVA1349 Optimal Bus Route Design 拆点法+最小费用最佳匹配

    /** 题目:UVA1349 Optimal Bus Route Design 链接:https://vjudge.net/problem/UVA-1349 题意:lrj入门经典P375 给n个点(n ...

  3. UVA - 1349 D - Optimal Bus Route Design

    4. D - Optimal Bus Route Design 题意:给出n(n<=100)个点的带权有向图,找出若干个有向圈,每个点恰好属于一个有向圈.要求权和尽量小. 注意即使(u,v)和( ...

  4. UVa 11383 少林决胜(二分图最佳完美匹配)

    https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...

  5. Ants(二分图最佳完美匹配)

    Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6904   Accepted: 2164   Special Ju ...

  6. UVA - 1045 The Great Wall Game(二分图最佳完美匹配)

    题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...

  7. 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】

    题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...

  8. UVa 1349 - Optimal Bus Route Design(二分图最佳完美匹配)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design

    题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi  ...

随机推荐

  1. OpenWrt的主Makefile工作过程

    OpenWrt是一个典型的嵌入式Linux工程,了解OpenWrt的Makefile的工作过程对提高嵌入式Linux工程的开发能力有极其重要意义. OpenWrt的主Makefile文件只有100行, ...

  2. HibernateTool的安装和使用(Eclipse中)

    http://blog.sina.com.cn/s/blog_919273e20101g1t7.html

  3. 十六、C# 常用集合类及构建自定义集合(使用迭代器)

    常用集合类及构建自定义集合 1.更多集合接口:IList<T>.IDictionary<TKey,TValue>.IComparable<T>.ICollectio ...

  4. Spring 创建bean的模式

    在默认情况下,spring创建bean是单例模式 scope="singleton ",还有一种方式为多例模式[prototype]     scope          sing ...

  5. cuda中时间用法

    转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...

  6. WHU 1572 Cyy and Fzz (AC自动机 dp )

    题意: 给出n个串,求任意长度为m的字符串包含串的个数的期望.(n<=8,m<=14,给定串的长度不超过12). Solution: 首先可以想到应该用概率DP,我们需要至少3维,dp[i ...

  7. poj 1087.A Plug for UNIX (最大流)

    网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化 ...

  8. linux搜索命令

    1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...

  9. Why Doesn’t Drag-and-Drop work when my Application is Running Elevated? – A: Mandatory Integrity Control and UIPI(转载)

    f you run notepad elevated (Right click | Run as Administrator), and you try and drag-and-drop a fil ...

  10. Apache与php的整合(经典版),花了一天去配置成功经验

    1.首先在官方下载php-7.0.7-Win32-VC14-x64.zip和httpd-2.4.20-win64-VC14.zip,也可以下载镜像版的apache:apache_2.4.4-x64-o ...