The Chinese Postman Problem HIT - 2739(有向图中国邮路问题)
无向图的问题,如果每个点的度数为偶数,则就是欧拉回路,而对于一个点只有两种情况,奇数和偶数,那么就把都为奇数的一对点 连一条 边权为原图中这两点最短路的值 的边 是不是就好了
无向图中国邮路问题:

有向图的问题,如果每个点的入度和出度相同,则就是欧拉回路,而这个情况就多了,相同、入度少一、入度少俩·····、出度少1、出度少俩,
呐 如果我们把入度少的 和 出度少的连起来是不是就是欧拉回路了,比如说点x的出度为7,入度为3;点y的出度为2,入度为4;点z的出度为2,入度为4;
那么x是连点y还是点z,当然是先连距离最小的那个,假设是y,那么x <- y 连两条边之后,x入度为7,入度为5,y的入度和出度相同,
那么x就开始连z,仔细想一想 这是不是就是费用流,先使路的费用小的满流,然后次小,然后次次小,所以费用流可以完美解决这个问题
有向图的中国邮路问题:

咳咳。。。反正wrong 交网上的代码也wrong
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m, s, t;
int head[maxn], d[maxn], vis[maxn], p[maxn], f[maxn], fi[maxn];
int in[maxn], out[maxn];
int cnt, flow, value; struct node
{
int u, v, c, w, next;
}Node[maxn << ]; void add(int u, int v, int c, int w)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].w = w;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} int spfa()
{
queue<int> Q;
mem(vis, );
mem(p, -);
for(int i = ; i < maxn; i++) d[i] = INF;
Q.push(s);
d[s] = ;
vis[s] = ;
p[s] = , f[s] = INF;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
vis[u] = ;
for(int i = head[u]; i != -; i = Node[i].next)
{
node e = Node[i];
if(d[e.v] > d[u] + e.w && e.c > )
{
d[e.v] = d[u] + e.w;
p[e.v] = i;
f[e.v] = min(f[u], e.c);
if(!vis[e.v])
{
Q.push(e.v);
vis[e.v] = ;
}
}
}
}
if(p[t] == -) return ;
flow += f[t]; value += f[t] * d[t];
for(int i = t; i != s; i = Node[p[i]].u)
{
Node[p[i]].c -= f[t];
Node[p[i]^].c += f[t];
}
return ;
} void max_flow()
{
while(spfa());
} void init()
{
mem(head, -);
mem(in, );
mem(out, );
cnt = value = flow = ;
} int find(int x)
{
return fi[x] == x ? fi[x] : (fi[x] = find(fi[x]));
} int main()
{
int T;
int u, v, w;
cin >> T;
while(T--)
{
for(int i = ; i < maxn; i++) fi[i] = i;
int flag = , ans = ;
init();
int edge_sum = ;
cin >> n >> m;
s = n + , t = n + ;
for(int i = ; i < m; i++)
{
cin >> u >> v >> w;
int l = find(u);
int r = find(v);
if(l != r) fi[l] = r;
edge_sum += w;
add(u, v, INF, w);
in[v]++;
out[u]++;
}
for(int i = ; i < n; i++)
if(fi[i] == i) ans++;
if(ans > )
{
puts("-1");
continue;
}
int tot_flow = ;
for(int i = ; i < n; i++)
{
if(in[i] == && out[i] == )
{
flag = ;
break;
}
if(out[i] > in[i]) add(i, t, out[i] - in[i], ), tot_flow += out[i] - in[i];
else if(in[i] > out[i]) add(s, i, in[i] - out[i], );
}
if(flag)
{
puts("-1");
continue;
} max_flow();
if(tot_flow != flow)
{
puts("-1");
continue;
}
cout << edge_sum + value << endl;
} return ;
}
The Chinese Postman Problem HIT - 2739(有向图中国邮路问题)的更多相关文章
- HIT 2739 - The Chinese Postman Problem - [带权有向图上的中国邮路问题][最小费用最大流]
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2739 Time limit : 1 sec Memory limit : 64 M A Chinese ...
- HITOJ 2739 The Chinese Postman Problem(欧拉回路+最小费用流)
The Chinese Postman Problem My Tags (Edit) Source : bin3 Time limit : 1 sec Memory limit : 6 ...
- Chinese Postman Problem Aizu - DPL_2_B(无向图中国邮路问题)
题意: 带权无向图上的中国邮路问题:一名邮递员需要经过每条边至少一次,最后回到出发点,一条边多次经过权值要累加,问最小总权值是多少.(2 <= N <= 15, 1 <= M < ...
- HIT2739 The Chinese Postman Problem(最小费用最大流)
题目大概说给一张有向图,要从0点出发返回0点且每条边至少都要走过一次,求走的最短路程. 经典的CPP问题,解法就是加边构造出欧拉回路,一个有向图存在欧拉回路的充分必要条件是基图连通且所有点入度等于出度 ...
- FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)
Chinese Postman Problem is a very famous hard problem in graph theory. The problem is to find a shor ...
- Problem E: 穷游中国在统题 优先队列 + 模拟
http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=4 Problem E: 穷游中国在统题 Description Travel ...
- LightOJ1086 Jogging Trails(欧拉回路+中国邮递员问题+SPFA)
题目求从某点出发回到该点经过所有边至少一次的最短行程. 这个问题我在<图论算法理论.实现及应用>中看过,是一个经典的问题——中国邮递员问题(CPP, chinese postman pro ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
- 贪心算法:旅行商问题(TSP)
TSP问题(Traveling Salesman Problem,旅行商问题),由威廉哈密顿爵士和英国数学家克克曼T.P.Kirkman于19世纪初提出.问题描述如下: 有若干个城市,任何两个城市之间 ...
随机推荐
- LOJ2687 BOI2013 Vim 线头DP
传送门 多图警告!!! 一种很新奇的\(DP\),全网似乎只有一两篇题解-- 首先,序列中的一段\(e\)等价于在跳的过程中这一段\(e\)之后的一个字符必须要经过,并且在最后的答案中加上$2 \ti ...
- Luogu3209 HNOI2010 平面图判定 平面图、并查集
传送门 题意:$T$组数据,每组数据给出一个$N$个点,$M$条边,并存在一个$N$元环的图,试判断其是否为一个可平面图(如果存在一种画法,使得该图与给出的图同构且边除了在顶点处以外互相不相交,则称其 ...
- sun.misc.BASE64Decoder 限制取消
sun.misc.BASE64Decoder Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -&g ...
- Luogu P2279 [HNOI2003]消防局的设立
这真的是一道SB题.去你的树形DP 我们看到题目就开始考虑贪心,怎么搞? 一个显然的思路,每次找出一个深度最大且未被覆盖的点,然后建一个消防局? 但这样的话,动用简单的人类思维就可以知道:我TM的还不 ...
- [Spark][Python]spark 从 avro 文件获取 Dataframe 的例子
[Spark][Python]spark 从 avro 文件获取 Dataframe 的例子 从如下地址获取文件: https://github.com/databricks/spark-avro/r ...
- Mybatis中 collection 和 association 的区别?
public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...
- SqlBulkCopy简单封装,让批量插入更方便
关于 SqlServer 批量插入的方式,前段时间也有大神给出了好几种批量插入的方式及对比测试(http://www.cnblogs.com/jiekzou/p/6145550.html),估计大家也 ...
- Unity 敌人波次设计
一.平均时间随机敌人 将所有种类敌人预制物体放在一个列表里面,每隔时间T从列表中随机选出一个生成在场景中. 二.时间加权紧迫度随机敌人 在随机情况下每种敌人出现的概率近似相等,当敌人种类较多时,有可能 ...
- Centos下安装破解Jira7的操作记录
Jira是一个集项目计划.任务分配.需求管理.错误跟踪于一体的工具,可以作为一个bug管理系统,可以将在测试过程中所发现的bug录入.分配给开发人员.前面介绍了Confluence在Centos下的安 ...
- shell脚本之特殊符号总结性梳理
# 井号 (comments) 这几乎是个满场都有的符号#!/bin/bash 井号也常出现在一行的开头,或者位于完整指令之后,这类情况表示符号后面的是注解文字,不会被执行. # This line ...