UVaLive 3353 Optimal Bus Route Design (最小费用流)
题意:给定一个 n 个点的有向带权图,让你找若干个圈,使得每个结点恰好属于一个圈,并且总长度尽量小。
析:一开始想的是先缩点,先用DP,来求。。。
题解给的是最小费用流或者是最佳完全匹配,其实都是一样的,因为每个点都只属于一个圈,那么对于每个点的入度和出度都应该是一样的,然后就是把每个点都拆成两个点,然后如果有边相连,就加一条费用该权值,容量为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>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#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 fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#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 LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200 + 10;
const int maxm = 3e5 + 10;
const ULL mod = 3;
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, cost;
}; struct MinCostMaxFlow{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
bool inq[maxn]; void init(int n){
this-> n = n;
for(int i = 0; i < n; ++i) G[i].cl;
edges.cl;
} void addEdge(int from, int to, int cap, int cost){
edges.pb((Edge){from, to, cap, 0, cost});
edges.pb((Edge){to, from, 0, 0, -cost});
m = edges.sz;
G[from].pb(m - 2);
G[to].pb(m - 1);
} bool bellman(int &flow, int &cost){
ms(d, INF); d[s] = 0; ms(inq, 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].sz; ++i){
Edge &e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost){
p[e.to] = G[u][i];
d[e.to] = d[u] + e.cost;
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 += a[t] * d[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 mincostmaxflow(int s, int t, int &flow){
this-> s = s;
this-> t = t;
int cost = 0;
while(bellman(flow, cost));
return cost;
}
};
MinCostMaxFlow mcmf; int main(){
while(scanf("%d", &n) == 1 && n){
int s = 0, t = n * 2 + 2;
mcmf.init(t + 2);
for(int i = 1; i <= n; ++i){
int u, cost;
mcmf.addEdge(s, i<<1, 1, 0);
mcmf.addEdge(i<<1|1, t, 1, 0);
while(scanf("%d", &u) == 1 && u){
scanf("%d", &cost);
mcmf.addEdge(i<<1, u<<1|1, 1, cost);
}
}
int flow = 0;
int ans = mcmf.mincostmaxflow(s, t, flow);
if(flow != n) puts("N");
else printf("%d\n", ans);
}
return 0;
}
UVaLive 3353 Optimal Bus Route Design (最小费用流)的更多相关文章
- uvalive 3353 Optimal Bus Route Design
题意: 给出n个点,以及每个点到其他点的有向距离,要求设计线路使得每一个点都在一个环中,如果设计的线路拥有最小值,那么这个线路就是可选的.输出这个最小值或者说明最小线路不存在. 思路: 在DAG的最小 ...
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- UVA1349 Optimal Bus Route Design 拆点法+最小费用最佳匹配
/** 题目:UVA1349 Optimal Bus Route Design 链接:https://vjudge.net/problem/UVA-1349 题意:lrj入门经典P375 给n个点(n ...
- UVA - 1349 D - Optimal Bus Route Design
4. D - Optimal Bus Route Design 题意:给出n(n<=100)个点的带权有向图,找出若干个有向圈,每个点恰好属于一个有向圈.要求权和尽量小. 注意即使(u,v)和( ...
- UVA 1349 Optimal Bus Route Design 最优公交路线(最小费用流,拆点)
题意: 给若干景点,每个景点有若干单向边到达其他景点,要求规划一下公交路线,使得每个景点有车可达,并且每个景点只能有1车经过1次,公车必须走环形回到出发点(出发点走2次).问是否存在这样的线路?若存在 ...
- UVa 1349 Optimal Bus Route Design (最佳完美匹配)
题意:给定一个有向图,让你找出若干个图,使得每个点恰好属于一个圈,并且总的权和最小. 析:每个点都有唯一的一个圈,也就是说每一点都有唯一的后继,那么我们就可以转换成求一个图的最小权的最佳完全匹配,可以 ...
- UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi ...
- UVA1349:Optimal Bus Route Design
题意:给定一个有向带权图,找若干个环,使得每个点属于且仅属于一个环,要求使得环权值之和最小 题解:发现这题中每个点属于且仅属于一个环,这时候"仅"这种恰好的含义,让我们想到了匹配问 ...
- UVa 1349 - Optimal Bus Route Design(二分图最佳完美匹配)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- 子元素margin-top后,跟父元素一起下沉
在一个<div>元素中嵌套一个子div,同时设置子div的margin-top,结果,父元素和子元素一起下沉,留出来个空白区域. 原因就是: 一个盒子如果没有上补白(padd ...
- python 之九九乘法表
for i in range(1,10): for j in range(1,i+1): print(f"{j}*{i}={i*j}",end='\t') print() 运行结果 ...
- ios localization
1. 在工程文件中选择支持的语言,“Localizations” 2. 添加资源文件 Localizable.strings, (app默认从该资源文件读取字符串) 3. 通过 NSLocalized ...
- python2限制函数传入的关键字参数
在Python2 中,可以通过使用**kwargs,在函数中配合使用kwargs.pop(key, False)实现获取限制关键字参数值,如果未传入则设置默认值,当所有需要的关键字参数都pop完毕,如 ...
- TEXT 5 Stuff of dreams
TEXT 5 Stuff of dreams 梦想的精粹 Feb 16th 2006 | CORK AND LONDON From The Economist print edition (译者注:本 ...
- WEB性能测试工具
做Web开发,难免要对自己开发的页面进行性能检测,自己写工具检测,工作量太大.网上有几款比较成熟的检测工具,以下就介绍一下,与大家分享. 互联网现有工具 基于网页分析工具: 1. 阿里测 ...
- HTML转义
HTML转义 模板对上下文传递的字符串进行输出时,会对以下字符自动转义 小于号< 转换为< 大于号> 转换为> 单引号' 转换为' 双引号" 转换为 " 与 ...
- yum 操作
一.使用yum安装和卸载软件,有个前提是yum安装的软件包都是rpm格式的. 安装的命令是,yum install ~,yum会查询数据库,有无这一软件包,如果有,则检查其依赖冲突关系,如果没有依赖冲 ...
- string类小结
要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...
- ScrollView listView gridView 之间的冲突问题
在ScrollView中的listView gridView添加适配器之后添加//设置gridView整体的高度 public void setListViewHeightBasedOnChildre ...