UVa 1349 Optimal Bus Route Design (最佳完美匹配)
题意:给定一个有向图,让你找出若干个图,使得每个点恰好属于一个圈,并且总的权和最小。
析:每个点都有唯一的一个圈,也就是说每一点都有唯一的后继,那么我们就可以转换成求一个图的最小权的最佳完全匹配,可以用最小费用流来求,
先把每个结点拆成两个点,假设是x,y,然后建立一个源点,向每个点的x连一条容量为1的边,建立一个汇点,每个点的y向汇点连一条容量为1的边,
每条边u,v,也连接一条容量为1,费用为权值的边,最小求一个最小费用流即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 2000 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Edge{
int from, to, cap, flow;
LL cost;
}; struct MCMF{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
LL d[maxn];
int p[maxn];
int a[maxn]; void init(int n){
this->n = n;
for(int i = 0; i < n; ++i) G[i].clear();
edges.clear();
} void addEdge(int from, int to, int cap, LL cost){
edges.push_back((Edge){from, to, cap, 0, cost});
edges.push_back((Edge){to, from, 0, 0, -cost});
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
} bool BellmanFord(int s, int t, int &flow, LL &cost){
for(int i = 0; i < n; ++i) d[i] = INF;
memset(inq, 0, sizeof inq);
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].size(); ++i){
Edge &e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost){
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap-e.flow);
if(!inq[e.to]) q.push(e.to), inq[e.to] = 1;
}
}
}
if(d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while(u != s){
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
} int minCost(int s, int t, LL &cost){
int flow = 0; cost = 0;
while(BellmanFord(s, t, flow, cost));
return flow;
}
};
MCMF mcmf; int main(){
while(scanf("%d", &n) == 1 && n){
mcmf.init(n+n+2);
int v; LL val;
int s = 0, t = n+n+1;
for(int i = 1; i <= n; ++i){
// mcmf.addEdge(i, i+n, 1, 0);
mcmf.addEdge(s, i, 1, 0);
mcmf.addEdge(i+n, t, 1, 0);
while(scanf("%d", &v) == 1 && v){
scanf("%lld", &val);
mcmf.addEdge(i, v+n, 1, val);
}
} LL ans;
if(mcmf.minCost(s, t, ans) == n) printf("%lld\n", ans);
else printf("N\n");
}
return 0;
}
UVa 1349 Optimal Bus Route Design (最佳完美匹配)的更多相关文章
- UVa 1349 - Optimal Bus Route Design(二分图最佳完美匹配)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1349 Optimal Bus Route Design (二分图最小权完美匹配)
恰好属于一个圈,那等价与每个点有唯一的前驱和后继,这让人想到了二分图, 把一个点拆开,点的前驱作为S集和点的后继作为T集,然后连边,跑二分图最小权完美匹配. 写的费用流..最大权完美匹配KM算法没看懂 ...
- UVA 1349 Optimal Bus Route Design 最优公交路线(最小费用流,拆点)
题意: 给若干景点,每个景点有若干单向边到达其他景点,要求规划一下公交路线,使得每个景点有车可达,并且每个景点只能有1车经过1次,公车必须走环形回到出发点(出发点走2次).问是否存在这样的线路?若存在 ...
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- UVA - 1349 D - Optimal Bus Route Design
4. D - Optimal Bus Route Design 题意:给出n(n<=100)个点的带权有向图,找出若干个有向圈,每个点恰好属于一个有向圈.要求权和尽量小. 注意即使(u,v)和( ...
- UVA1349 Optimal Bus Route Design 拆点法+最小费用最佳匹配
/** 题目:UVA1349 Optimal Bus Route Design 链接:https://vjudge.net/problem/UVA-1349 题意:lrj入门经典P375 给n个点(n ...
- UVa 11383 少林决胜(二分图最佳完美匹配)
https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...
- UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi ...
- 【uva 1349】Optimal Bus Route Design(图论--网络流 二分图的最小权完美匹配)
题意:有一个N个点的有向带权图,要求找若干个有向圈,使得每个点恰好属于一个圈.请输出满足以上条件的最小权和. 解法:有向圈?也就是每个点有唯一的后继.这是一个可逆命题,同样地,只要每个点都有唯一的后继 ...
随机推荐
- 运维基础-Linux发展史、安装、基本操作
Linux是目前互联网运维.大数据.云计算方向首选操作系统平台,能够在物理服务器Dell.hp.等server,以及当前主流的云平台,阿里云,腾讯云上面部署 发展史 . . .略过..... 物理服务 ...
- ios开发动物园管理 继承多态的实现
// // main.m // 继承 // // #import <Foundation/Foundation.h> #import "Animal.h" #impor ...
- React机制浅析
在写React代码时,避免不了提前想想页面的结构,当然这也属于HTML布局了,比如哪些组件里需要包含哪些组件.遂突发奇想,如果试着把子组件的render内容替换原组件,会是个啥? 比如拿 https: ...
- OpenCV 的四大模块
前言 我们都知道 OpenCV 是一个开源的计算机视觉库,那么里面到底有哪些东西?本文将为你解答这个问题. 模块一:CV 这个模块是 OpenCV 的核心,它包含了基本的图像处理函数和高级的计算机视觉 ...
- UITableView基础入门
新建一个Single View Application 添加一个空类如下: using System; using UIKit; using Foundation; namespace BasicTa ...
- Bullet Physics OpenGL 刚体应用程序模板 Rigid Simulation in Bullet
利用Bullet物理引擎实现刚体的自由落体模拟的模板 Bullet下载地址 Main.cpp #include <GLUT/glut.h> #include <cstdlib> ...
- 提高sqlite 的运行性能(转载)
原文地址: https://blog.devart.com/increasing-sqlite-performance.html One the major issues a developer en ...
- asp.net 列表控件
web空间类都被放置在System.Web.UI.WebControls命名空间下1.ListBox ListBox控件用于创建多选的下拉列表,而可选项是通过ListItem元素来定义的.示例代码如 ...
- TControl,TWinControl和TGraphicControl的显示函数
-------------------------- 显示隐藏刷新 -------------------------- TControl = class(TComponent)procedure S ...
- BZOJ2759: 一个动态树好题
BZOJ2759: 一个动态树好题 Description 有N个未知数x[1..n]和N个等式组成的同余方程组:x[i]=k[i]*x[p[i]]+b[i] mod 10007其中,k[i],b[i ...