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& ...
随机推荐
- php, postgresql 安装
sudo yum install postgresql84-server postgresql84-contrib ubuntu下面安装的问题解决: Postgresql installation o ...
- HttpClient获取返回类型为JSON或XML的数据
Java_HttpClient获取返回类型为JSON或XML的数据 原创 2017年04月06日 17:38:29 706 HttpClient 获取返回类型为JSON或XML的数据 使用httpco ...
- eclipse 自动生成json格式的toString()方法
文本代码 {"${member.name()}":"${member.value}", "${otherMembers}"}
- java 包的命名规范
- spark 创建稀疏向量和矩阵
http://blog.csdn.net/canglingye/article/details/41316193 [相互转换]:http://stackoverflow.com/questions/3 ...
- 条件语句;for循环 嵌套复习
//打印数字,0,1,8,10,12,每一个数单独占一行 //在全部数字打印完毕之后在打印数字的个数和所有数的和 int count = 0; int sum = 0; for (int i = 0; ...
- Fb,tw等emoji相关
最近处理fb emoji,查了下相关的资料.记录于此 twitter blog 关于:https://blog.twitter.com/developer/en_us/a/2014/open-sou ...
- Cannot create inner bean '(inner bean)#67f903b5' of typ
严重: Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error crea ...
- VB 共享软件防破解设计技术初探(二)
VB 共享软件防破解设计技术初探(二) ×××××××××××××××××××××××××××××××××××××××××××××× 其他文章快速链接: VB 共享软件防破解设计技术初探(一)http ...
- 每月IT摘录201808--201809
一.技术 海量数据的解决方案: 使用缓存: 页面静态化技术: 数据库优化: 分离数据库中活跃的数据: 批量读取和延迟修改: 读写分离: 使用NoSQL和Hadoop等技术: 分布式部署数据库: 应用服 ...