Problem Description

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?

Input

There are several test cases in the input. You should process to the end of file (EOF).

The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).

Output

Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.

Sample Input

6 9

1 2 5

2 3 5

3 1 10

3 4 12

4 1 8

4 6 11

5 4 7

5 6 9

6 5 4

6 5

1 2 1

2 3 1

3 4 1

4 5 1

5 6 1

Sample Output

42

-1

Hint

In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.

Description(CHN)

给你一个 \(N\) 个点 \(M\) 条边的带权有向图,现在要你求这样一个值:

该有向图中的所有顶点正好被1个或多个不相交的有向环覆盖.

这个值就是 所有这些有向环的权值和. 要求该值越小越好.

Solution

有向图环覆盖,变成二分图匹配,经典套路

然后要求权最小,就是二分图最大匹配,跑遍费用流就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100+10,MAXM=MAXN*MAXN,inf=0x3f3f3f3f;
int n,m,e,beg[MAXN<<1],s,t,level[MAXN<<1],nex[MAXM<<1],to[MAXM<<1],cap[MAXM<<1],was[MAXM<<1],p[MAXN<<1],cur[MAXN<<1],vis[MAXN<<1],clk,answas;
std::queue<int> q;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z,int k)
{
to[++e]=y;
nex[e]=beg[x];
beg[x]=e;
cap[e]=z;
was[e]=k;
to[++e]=x;
nex[e]=beg[y];
beg[y]=e;
cap[e]=0;
was[e]=-k;
}
inline bool bfs()
{
for(register int i=1;i<=t;++i)level[i]=inf;
level[s]=0;
p[s]=1;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
p[x]=0;
for(register int i=beg[x];i;i=nex[i])
if(cap[i]&&level[to[i]]>level[x]+was[i])
{
level[to[i]]=level[x]+was[i];
if(!p[to[i]])p[to[i]]=1,q.push(to[i]);
}
}
return level[t]!=inf;
}
inline int dfs(int x,int maxflow)
{
if(x==t||!maxflow)return maxflow;
int res=0;
vis[x]=clk;
for(register int &i=cur[x];i;i=nex[i])
if((vis[x]^vis[to[i]])&&cap[i]&&level[to[i]]==level[x]+was[i])
{
int f=dfs(to[i],min(maxflow,cap[i]));
res+=f;
cap[i]-=f;
cap[i^1]+=f;
answas+=was[i]*f;
maxflow-=f;
if(!maxflow)break;
}
vis[x]=0;
return res;
}
inline int MCMF()
{
int res=0;
while(bfs())clk++,memcpy(cur,beg,sizeof(cur)),res+=dfs(s,inf);
return res;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
e=1;memset(beg,0,sizeof(beg));answas=0;
for(register int i=1;i<=m;++i)
{
int u,v,k;read(u);read(v);read(k);
insert(v,u+n,1,k);
}
s=n+n+1,t=s+1;
for(register int i=1;i<=n;++i)insert(s,i,1,0),insert(i+n,t,1,0);
if(MCMF()!=n)puts("-1");
else write(answas,'\n');
}
return 0;
}

【刷题】HDU 1853 Cyclic Tour的更多相关文章

  1. hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...

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

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

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

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

  4. HDU 1853 Cyclic Tour(最小费用最大流)

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

  5. hdu 1853 Cyclic Tour 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...

  6. 最大流增广路(KM算法) HDOJ 1853 Cyclic Tour

    题目传送门 /* KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 */ #include <cstdio> #include <cmath> #incl ...

  7. HDU(1853),最小权匹配,KM

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Other ...

  8. hdu 1853 最小费用流好题 环的问题

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

  9. HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)

    前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...

随机推荐

  1. 《杜增强讲Unity之Tanks坦克大战》7-坦克血条

    7 坦克血条 点击菜单GameObject->UI->Slider创建Slider   选中EventSystem,设置Horizontal Axis为HorzontalUI,Vertic ...

  2. MD5加密--项目案例

    在项目中最尝使用MD5这种非对称加密的就是用户信息登录了.下面我就以一个简单的登录案例来说明MD5的用法 首先来看几张图: 用户登录页:需要选择要登录的系统,同时输入用户的用户名和密码,验证码才能进入 ...

  3. 会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!

      我们都知道,Python 的设计哲学是「优雅」.「明确」.「简单」.这也许很多人选择 Python 的原因.但是我收到有些伙伴反馈,他写的 Python 并不优雅,甚至很臃肿,那可能是你的姿势不对 ...

  4. happybase(TSocket read 0 bytes)

    关于报错happybase 是使用python连接hbase的一个第三方库,目前基于thrift1 .在使用过程中经常碰到报错 TTransportException(type=4, message= ...

  5. Node.js开发入门—套接字(socket)编程

    Node.js的net模块提供了socket编程接口,方便我们利用较为底层的套接字接口来实现应用协议.这次我们看一个简单的回显服务器示例,包括服务端和客户端的代码. 代码 分服务器和客户端两部分来说吧 ...

  6. bash登录过程 其实还不太了解,先码后看

    在刚登录Linux时,首先启动 /etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile. ~/.bash_login或 ~/.profile文件中的其中一个,执行的顺序 ...

  7. ./configure的含义

    在实践安装nginx的时候,不知道./configure是什么意思,这里特地记录一下. 在linux中./代表当前目录,属于相对路径../代表上一级目录,属于相对路径/代表根目录,/开头的文件都是绝对 ...

  8. maven实战读书笔记(三)

    maven将一系列的步骤都封装为一系列的插件,运行命令后一系列的插件运行

  9. Linux基础入门--01~03

  10. Ubuntu下开启mysql远程访问

    1. 开启数据库3306端口 首先,使用如下指令查看3306端口是否对外开放. netstat -an | grep 3306 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LIS ...