UVA 1349(二分图匹配)
1349 - Optimal Bus Route Design
Time limit: 3.000 seconds
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

题意:有n个景点,每个景点之间有一些带权有向边,要求你设计一些线路,要求线路是一个圈,并且每个景点只能有一条线路经过,并且只能经过一次(起始点除外),问你,若存在,求这些线路权值的最小和,否则输出"N"
法一:
KM
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define pfi(n) printf("%d\n", n)
#define sfi2(n, m) scanf("%d%d", &n, &m)
#define pfi2(n, m) printf("%d %d\n", n, m)
#define pfi3(a, b, c) printf("%d %d %d\n", a, b, c)
#define MAXN 105
#define V 55 /* KM算法
* 复杂度O(nx*nx*ny)
* 求最大权匹配
* 若求最小权匹配,可将权值取相反数,结果取相反数
* 点的编号从0开始
*/
const int N = ;
const int INF = 0x3f3f3f3f;
int nx,ny;//两边的点数
int g[N][N];//二分图描述
int linker[N],lx[N],ly[N];//y中各点匹配状态,x,y中的点标号
int slack[N];
bool visx[N],visy[N]; bool DFS(int x)
{
visx[x] = true;
for(int y = ; y < ny; y++)
{
if(visy[y])continue;
int tmp = lx[x] + ly[y] - g[x][y];
if(tmp == )
{
visy[y] = true;
if(linker[y] == - || DFS(linker[y]))
{
linker[y] = x;
return true;
}
}
else if(slack[y] > tmp)
slack[y] = tmp;
}
return false;
}
void KM()
{
memset(linker,-,sizeof(linker));
memset(ly,,sizeof(ly));
for(int i = ;i < nx;i++)
{
lx[i] = -INF;
for(int j = ;j < ny;j++)
if(g[i][j] > lx[i])
lx[i] = g[i][j];
}
for(int x = ;x < nx;x++)
{
for(int i = ;i < ny;i++)
slack[i] = INF;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(DFS(x))break;
int d = INF;
for(int i = ;i < ny;i++)
if(!visy[i] && d > slack[i])
d = slack[i];
for(int i = ;i < nx;i++)
if(visx[i])
lx[i] -= d;
for(int i = ;i < ny;i++)
{
if(visy[i])ly[i] += d;
else slack[i] -= d;
}
}
} //return res;
}
//HDU 2255
int main()
{
int n;
while(scanf("%d",&n) && n)
{
int tn = * n;
int x, y, c;
repu(i, , n) repu(j, , n) g[i][j] = -INF;
for(int i = ;i < n;i++)
{
while()
{
sfi(y);
if(y == ) break;
y--;
sfi(c);
g[i][y] = max(-c, g[i][y]);
}
}
nx = ny = n;
KM(); int res = , flag = ;
for(int i = ;i < ny;i++)
{
if(g[linker[i]][i] == -INF) {flag = ; break;}
res += g[linker[i]][i];
}
if(flag)
printf("N\n");
else printf("%d\n", -res);
}
return ;
}
UVA 1349(二分图匹配)的更多相关文章
- UVA 12549 - 二分图匹配
		题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ... 
- UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
		题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi ... 
- 紫书 例题11-10 UVa 1349 (二分图最小权完美匹配)
		二分图网络流做法 (1)最大基数匹配.源点到每一个X节点连一条容量为1的弧, 每一个Y节点连一条容量为1的弧, 然后每条有向 边连一条弧, 容量为1, 然后跑一遍最大流即可, 最大流即是最大匹配对数 ... 
- 【uva 1349】Optimal Bus Route Design(图论--网络流 二分图的最小权完美匹配)
		题意:有一个N个点的有向带权图,要求找若干个有向圈,使得每个点恰好属于一个圈.请输出满足以上条件的最小权和. 解法:有向圈?也就是每个点有唯一的后继.这是一个可逆命题,同样地,只要每个点都有唯一的后继 ... 
- UVa 二分图匹配 Biginners
		UVa 1045 - The Great Wall Game 最小权匹配 题意:给你一个n*n的棋盘,上面有n个棋子,要求通过移动各个棋子使得棋子在同一行或者同一列或者对角线上,求最小移动次数. 思路 ... 
- UVa 二分图匹配 Examples
		这些都是刘汝佳的算法训练指南上的例题,基本包括了常见的几种二分图匹配的算法. 二分图是这样一个图,顶点分成两个不相交的集合X , Y中,其中同一个集合中没有边,所有的边关联在两个集合中. 给定一个二分 ... 
- POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)
		POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ... 
- uva 12083 Guardian of Decency (二分图匹配)
		uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ... 
- UVA 1663 Purifying Machine (二分图匹配,最大流)
		题意: 给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1.模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己.问:模板串可以缩减为几个,同样可以匹配原 ... 
- UVA 11045-My T-shirt suits me(二分图匹配)
		题意:有N件T恤,N是6的倍数,因为有6种型号,每种件数相同,有M个人,每个人有两种型号的T恤适合他,每个人可以挑其中的一种,问能否所有的人都能分配到T恤. 解析:典型的二分图匹配,每N/6为同种T恤 ... 
随机推荐
- lua语言三则特性
			pack和unpack 对于一个函数, 要将其入参转换为一个表, 则pack函数合适. 对于一个表要将其转换为 一个函数的入参, 则 lua原生提供的 unpack函数可以实现. do arrayDa ... 
- 使用MapReduce实现一些经典的案例
			在工作中,很多时候都是用hive或pig来自动化执行mr统计,但是我们不能忘记原始的mr.本文记录了一些通过mr来完成的经典的案例,有倒排索引.数据去重等,需要掌握. 一.使用mapreduce实现倒 ... 
- Oracle EBS R12 电子技术参考手册 - eTRM (电子文档)
			http://etrm.oracle.com/pls/etrm/etrm_search.search 
- Java反射深入浅出(一)
			在JVM中对一个类实例的创建,有两种方式,一种是编译时,一种是运行时.两种方式在开发过程中都是十分重要的.在Java中无时无刻无处不在的Java对象,实例化的过程也就变得尤为引人瞩目.我们经常用new ... 
- Java之美[从菜鸟到高手演变]之Spring源码学习 - 环境搭建
			准备工作 1.下载安装STS(Spring Tool Suite),在eclipse market里直接搜索.下载.安装.2.下载安装gradle, Spring源码使用gradle构建,下载后解压到 ... 
- Winform   主窗体更换 构造函数传值
			制作登录窗体: 制作一个登陆窗体,实现点击按钮关闭此窗体并打开另一个窗体 直接在按钮点击事件中,实例化一个想要打开的窗体 使用show方法打开,并把登陆窗体的visible属性改为false Form ... 
- 【转】lonekight@xmu·ACM/ICPC 回忆录
			转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ... 
- POJ - 1245 Programmer, Rank Thyself
			POJ - 1245 Programmer, Rank Thyself Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d ... 
- Openstack的keystone的user-role-list命令的使用
			直接在shell模式下执行 [root@node-5 ~]# keystone user-role-list WARNING: Bypassing authentication using a tok ... 
- Java通过继承thread类与实现Runnable接口实现多线程的区别
			Java中线程的创建有两种方式: 1. 通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2. 通过实现Runnable接口,实例化Thread类 一.通过继承T ... 
