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. php常用的魔术方法

    __construct:构造函数,一旦创建对象都就会自动调用 __call:当调用了未定义的方法时会自动触发 __set:当给类外部不可访问的属性设置值时会自动触发 __get:当获取类外部不可访问的 ...

  2. php-7.1.11-64位

    php-7.1.11-Win32-VC14-x64.zip 链接:https://pan.baidu.com/s/1w8-fJo8-oWrriHyWpU5Fpg 提取码:bd0e 复制这段内容后打开百 ...

  3. JavaScript判断对象是否是NULL(转)

    写js经常会遇到非空判断,看了你不就像风一样的文章 自己没有做总结,特地转载.很有帮助 function isEmpty(obj) { // 检验 undefined 和 null if (!obj ...

  4. 高级PHP工程师所应该具备的专业素养

    初次接触PHP,就为他的美所折服,于是一发不可收拾. 很多面试,很多人员能力要求都有“PHP高级工程师的字眼”,如果您真心喜欢PHP,并且您刚起步,那么我简单说说一个PHP高级工程师所应该具备的,希望 ...

  5. gcc 与 g++的区分较

    一:gcc与g++比较 误区一:gcc只能编译c代码,g++只能编译c++代码两者都可以,但是请注意:1.后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序:后缀为.cpp的,两者都会认为 ...

  6. Python之并发编程-IO模型

    目录 一.IO模型介绍二.阻塞IO(blocking IO)三.非阻塞IO(non-blocking IO)四.多路复用IO(IO multiplexing)五.异步IO(Asynchronous I ...

  7. HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)

    中文题,题意就不说了 一开始接触树状数组时,只知道“单点更新,区间求和”的功能,没想到还有“区间更新,单点查询”的作用. 树状数组有两种用途(以一维树状数组举例): 1.单点更新,区间查询(即求和) ...

  8. “北航Clubs” Alpha版发布!

    一.功能 1.获取活动信息: 用户进入网站后,第一眼就可以查看到近期活动 2.查看活动详情 点击活动标题,可以进入活动详情页面 3.注册功能 首页点击注册,输入学号.密码.姓名.手机号即可完成注册 4 ...

  9. 第二阶段每日站立会议Third Day

    昨天对图片显示的大小进行调整 今天对于程序中的字体显示进行细化修改,使界面更美观 遇到的问题:当图片太小时,显示一块灰色区域,不美观

  10. SpringMVC(四)-- springmvc的系统学习之文件上传、ajax&json处理

    资源:尚学堂 邹波 springmvc框架视频 一.文件上传 1.步骤: (1)导入jar包 commons-fileupload,commons-io (2)在springmvc的配置文件中配置解析 ...