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. Ubuntu 18.04 设置开机启动脚本 rc.local systemd

    ubuntu18.04不再使用initd管理系统,改用systemd. ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.l ...

  2. java NIO入门【原】

    server package com.server; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import jav ...

  3. 使用CROS解决跨域问题

    使用jq的话,设置ajax这两个属性即可. 第一个为使用crossDomain,第二个属性使其可以传输cookie $.ajaxSetup({ crossDomain: true, xhrFields ...

  4. 10.tesseract

    1.Tesseract-OCR简介  一个Google支持的开源的OCR图文识别开源项目.支持多种语言(我使用的是3.02 版本,支持包括英文,简体中文,繁体中文),支持Windows,Linux,M ...

  5. 微信小程序 TOP100 榜单

    8 月 12 日,阿拉丁数据统计平台发布了国内第一份小程序 TOP100 榜单,摩拜单车成为全榜第一! 该榜单数据来源于阿拉丁小程序统计平台检测.合作.如有赞等,并经过企业电话调研和实地走访企业等校准 ...

  6. EL11个内置对象

    EL是JSP内置的表达式语言 JSP2.0开始,让再使用Java脚本,而是使用EL表达式和动态标签来代替Java脚本 EL替代的是<%=... %>,也就是说EL只能做输出 EL可以输出的 ...

  7. 【解题报告】SRM-08

    A Description 给一个 01 串设为其 S,询问是否存在只出现两次的 01 串 T. 这里的出现定义为存在一串下标 ,满足  且 . Input 一行,一个 01 串 Output 一行, ...

  8. springboot04-mongodb

    1.搭建mongodb服务 在https://www.mongodb.com/下载mongodb安装包,解压到本地 cmd中执行命令,启动本地mongodb: cd D:\Program Files\ ...

  9. luogu P3240 [HNOI2015]实验比较

    传送门 首先根据题目条件,题目中如果是=的点可以缩起来,然后\(a<b\)连边\(a\rightarrow b\),而且所有点入度为最多1,那么判掉有环的不合法情况,题目中的依赖关系就是一颗外向 ...

  10. org.springframework.web.util.NestedServletException : Handler processing failed; nested exception is java.lang.StackOverflowError

    1 ,错误原因,循环冗余检查      result.setNearUsers(userList);            Page page = new Page();            pag ...