题意:给定一个 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 (最小费用流)的更多相关文章

  1. uvalive 3353 Optimal Bus Route Design

    题意: 给出n个点,以及每个点到其他点的有向距离,要求设计线路使得每一个点都在一个环中,如果设计的线路拥有最小值,那么这个线路就是可选的.输出这个最小值或者说明最小线路不存在. 思路: 在DAG的最小 ...

  2. UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)

    UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

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

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

  4. UVA - 1349 D - Optimal Bus Route Design

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

  5. UVA 1349 Optimal Bus Route Design 最优公交路线(最小费用流,拆点)

    题意: 给若干景点,每个景点有若干单向边到达其他景点,要求规划一下公交路线,使得每个景点有车可达,并且每个景点只能有1车经过1次,公车必须走环形回到出发点(出发点走2次).问是否存在这样的线路?若存在 ...

  6. UVa 1349 Optimal Bus Route Design (最佳完美匹配)

    题意:给定一个有向图,让你找出若干个图,使得每个点恰好属于一个圈,并且总的权和最小. 析:每个点都有唯一的一个圈,也就是说每一点都有唯一的后继,那么我们就可以转换成求一个图的最小权的最佳完全匹配,可以 ...

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

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

  8. UVA1349:Optimal Bus Route Design

    题意:给定一个有向带权图,找若干个环,使得每个点属于且仅属于一个环,要求使得环权值之和最小 题解:发现这题中每个点属于且仅属于一个环,这时候"仅"这种恰好的含义,让我们想到了匹配问 ...

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

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

随机推荐

  1. Mysql 表名大写 找不到表

    原来Linux下的MySQL默认是区分表名大小写的,通过如下设置,可以让MySQL不区分表名大小写:1.用root登录,修改 /etc/my.cnf:2.在[mysqld]节点下,加入一行: lowe ...

  2. jdk升级到9,eclipse打不开

    jdk从1.8到1.9之后删除了不少之前Deprecated的类. eclipse 版本oxygen和neon对应jdk1.8 eclipse 版本luna和mars对应jdk1.7,1.6 在打开e ...

  3. xcode显示行号show gutter

    要在每一个代码编辑窗口中的边线里显示行号: 使用Xcode > Preferences 菜单命令,点击 Text Editing,然后选择Editing 然后点击选择 “Line numbers ...

  4. DELL服务器iDRAC相关设置

    iDRAC又称为Integrated Dell Remote Access Controller,也就是集成戴尔远程控制卡 iDRAC卡相当于是附加在服务器上的一台小电脑,通过与服务器主板上的管理芯片 ...

  5. HTML meta 文本 格式排版 链接图表 列表 表单 frame后台布局实例

    meta标签 content属性必须和http-equiv或者name属性一起使用 http-equiv属性,就是http当量,用于和服务器发送数据前的提交交互使用.(另层含义这个当量在浏览器和服务器 ...

  6. How a non-windowed component can receive messages from Windows

    Why do it? Sometimes we need a non-windowed component (i.e. one that isn't derived fromTWinControl) ...

  7. makefile 中的赋值

    1. 在makefile 中可以使用后面定义的变量,未定义的变量值为空 = 使用变量时执行赋值操作 := 立即执行赋值操作 ?= 如果没有赋值过,就赋予后面的值 += 将后面的值追加到原来的值后面 参 ...

  8. print 不换行

    [print 不换行] 参考:http://zhidao.baidu.com/link?url=-qC2RyT5_GWzW_N-SyqJYgegVt2sSXwmMWGvHfk_4MjErhm_Pj23 ...

  9. 杨辉三角(生成器generator)

    生成器:(Python中,这种一边循环一边计算的机制,称为生成器:generator) 创建generator的方法: 1.把列表生成式的[]变为(),就创建了一个generator 例: 可以通过n ...

  10. dreamwave基础

    WEBcs架构需要在客户段安装程序, 需要安装程序, 工作量会比较大, 需要安装和维护, 比如以后系统升级, 会很麻烦. 优点是一些业务逻辑可以在客户端, 可以减少服务器的一些压力, 客户端的界面操作 ...