http://acm.hdu.edu.cn/showproblem.php?pid=3488

给一个无源汇的,带有边权的有向图

让你找出一个最小的哈密顿回路

可以用KM算法写,但是费用流也行

思路

1. 哈密顿回路对于每个点的流量有限制,因此$V$拆开为$V$和$V'$

2. 我们建立附加源点$S$和附加汇点$T$哈密顿回路中的每个点有其唯一的后继和前驱,换句话说,对于任意一个点$V$,它满足$in(V)=out(V)$

  为了满足该条件,从源点向$V$ 连接容量为1,费用为0的边,从$V'$向汇点连接容量为1,费用为0的边,其余点按照所给图建立即可,容量均为1,费用为边权

3. 跑一边费用流,如果所有指向$T$的边均满载,则答案存在,该费用即为答案

4. 实际上,如果不满载,是找到了有向图的最小路径覆盖

#include <bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pp pair<int,int>
#define rep(ii,a,b) for(int ii=a;ii<=b;ii++)
#define per(ii,a,b) for(int ii=a;ii>=b;ii--)
#define show(x) cout<<#x<<"="<<x<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define showa(a,b) cout<<#a<<'['<<b<<"]="<<b[a]<<endl
using namespace std;
const int maxn=567+10;
const int maxm=1e6+10;
const int INF=0x3f3f3f3f;
int casn,n,m,k;
int vis[234][234];
struct node {
int pre,to,cap,cost,next;
}e[maxm];
int head[maxn],nume,inq[maxn],sum,ans;
int q[maxn],pre[maxn],dis[maxn];
int num[maxn],ss,tt;
inline void addx(int a,int b,int c,int d){
e[++nume]={a,b,c,d,head[a]};
head[a]=nume;
}
inline void add(int a,int b,int c,int d){
addx(a,b,c,d);addx(b,a,0,-d);
}
bool spfa(int s=ss,int t=tt){
for(int i=0;i<=t;i++)dis[i]=INF;
dis[s]=q[0]=s;
int top=0,end=1;
while(top!=end){
int now=q[top++];top%=maxn;
for(int i=head[now];i;i=e[i].next){
int to=e[i].to;
if(e[i].cap&&dis[to]>dis[now]+e[i].cost){
pre[to]=i;
dis[to]=dis[now]+e[i].cost;
if(!inq[to]){
inq[to]=true;
if(dis[to]<dis[q[top]]){
top=(top==0)?maxn-1:top-1;
q[top]=to;
}else{
q[end++]=to;end%=maxn;
}
}
}
}
inq[now]=false;
}
return dis[t]!=INF;
}
void dfs(int s=ss,int t=tt){
int flow=INF;
for(int i=pre[t];i;i=pre[e[i].pre]) flow=min(flow,e[i].cap);
for(int i=pre[t];i;i=pre[e[i].pre]) {
e[i].cap-=flow;
e[i^1].cap+=flow;
ans+=e[i].cost*flow;
}
}
int main(){
//#define test
#ifdef test
freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif
scanf("%d",&casn);
while(casn--){
nume=1;
scanf("%d%d",&n,&m);
ss=0,tt=2*n+1;
memset(head,0,sizeof head);
rep(i,1,m){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b+n,1,c);
}
rep(i,1,n){
add(ss,i,1,0);
add(i+n,tt,1,0);
}
ans=0;
while(spfa()){
dfs();
}
printf("%d\n",ans);
}
#ifdef test
fclose(stdin);fclose(stdout);system("out.txt");
#endif
return 0;
}

  

Tour HDU - 3488 有向环最小权值覆盖 费用流的更多相关文章

  1. HDU 1853 Cyclic Tour[有向环最小权值覆盖]

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  2. HDU 3488 Tour(最小费用流:有向环最小权值覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意: 给出n个点和m条边,每条边有距离,把这n个点分成1个或多个环,且每个点只能在一个环中,保证有解. ...

  3. ZOJ-2342 Roads 二分图最小权值覆盖

    题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...

  4. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  5. POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你 ...

  6. POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)

    题目链接: http://poj.org/problem?id=1797 Background Hugo Heavy is happy. After the breakdown of the Carg ...

  7. POJ-1797.HeavyTransportation(最长路中的最小权值)

    本题思路:最短路变形,改变松弛方式即可,dist存的是源结点到当前结点的最长路的最小权值. 参考代码: #include <cstdio> #include <cstring> ...

  8. POJ 3565 Ants 【最小权值匹配应用】

    传送门:http://poj.org/problem?id=3565 Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: ...

  9. POJ 2195 Going Home 【二分图最小权值匹配】

    传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. 人工神经网络入门(4) —— AFORGE.NET简介

    范例程序下载:http://files.cnblogs.com/gpcuster/ANN3.rar如果您有疑问,可以先参考 FAQ 如果您未找到满意的答案,可以在下面留言:) 0 目录人工神经网络入门 ...

  2. 基于Asp.net C#实现HTML转图片(网页快照)

    一.实现方法 //WebSiteThumbnail.cs文件,在BS项目中需要添加对System.Windows.Forms的引用 using System; using System.Data; u ...

  3. CSS3笔记2

    1.CSS样式表分类 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  4. 在 CentOS6 上安装 Zabbix2.4 Server

    #!/bin/bash # # .配置无人值守的安装,定义安装过程中需要用到的一些信息 # mysql_root_pw=root_pw mysql_zabbix_pw=zabbix_pw DBPass ...

  5. Python 连接MongoDB并比较两个字符串相似度的简单示例

    本文介绍一个示例:使用 pymongo 连接 MongoDB,查询MongoDB中的 字符串 记录,并比较字符串之间的相似度. 一,Python连接MongoDB 大致步骤:创建MongoClient ...

  6. Android获取版本号

    public static String getVersionName(Context context) { PackageManager manager = context.getPackageMa ...

  7. 使用phpexcel上传下载excel文件

    1. 下载 <?php /** * Created by lonm.shi. * Date: 2012-02-09 * Time: 下午4:54 * To change this templat ...

  8. angularjs路由path方式实现原理探究

    angularjs路由 https://angular.io/guide/router 通过URL解释, 来定位客户端生成的浏览器端视图. 你可绑定路由到页面的链接上, 当用户点击链接, 可以浏览到相 ...

  9. 【深入理解JVM】:Java类继承关系中的初始化顺序

    尝试着仔细阅读thinking in java 看到一篇很好的文章http://blog.csdn.net/u011080472/article/details/51330114

  10. IEEE signal processing letters 投稿经验

    转自:http://emuch.net/t.php?tid=6226942 前段时间比较幸运地中了一篇spl,把自己浅薄的经验写出来,直接从自己博客上转过来,分享给大家,望抛砖引玉吧~~~ 从投稿到录 ...